简体   繁体   中英

ValueError: Shapes (None, 1) and (None, 90) are incompatible

I want to build a deep RNN where my x_train and my y_train. When I execute the code below:

print(X_train_fea.shape, y_train_fea.shape)
X_train_res = np.reshape(X_train_fea,(10510,10,1))
y_train_res = np.reshape(y_train_fea.to_numpy(),(-1,1))
print(X_train_res.shape, y_train_res.shape)

result:

(10510, 10) (10510,)
(10510, 10, 1) (10510, 1)

and

model = Sequential([
    LSTM(90, input_shape=(10,1)), 
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()

型号汇总

When I fit the model

history = model.fit(X_train_res, y_train_res,epochs=5)

I got

ValueError: Shapes (None, 1) and (None, 90) are incompatible

Looks like y_train_res comprise of integer indices not one-hot vectors. If so you have to use sparse_categorical_crossentropy :

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')

and change its shape to 1D:

y_train_res = np.reshape(y_train_fea.to_numpy(),(-1,))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM