ValueError: Shapes (None, 1) and (None, 90) are incompatible
ValueError: Shapes (None, 1) and (None, 90) are incompatible
我想在我的 x_train 和我的 y_train 中构建一个 deep RNN
。当我执行下面的代码时:
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)
结果:
(10510, 10) (10510,)
(10510, 10, 1) (10510, 1)
和
model = Sequential([
LSTM(90, input_shape=(10,1)),
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
当我拟合模型时
history = model.fit(X_train_res, y_train_res,epochs=5)
我得到了
ValueError: Shapes (None, 1) and (None, 90) are incompatible
看起来 y_train_res
包含整数索引而不是单热向量。如果是这样,您必须使用 sparse_categorical_crossentropy
:
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
并将其形状更改为一维:
y_train_res = np.reshape(y_train_fea.to_numpy(),(-1,))
我想在我的 x_train 和我的 y_train 中构建一个 deep RNN
。当我执行下面的代码时:
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)
结果:
(10510, 10) (10510,)
(10510, 10, 1) (10510, 1)
和
model = Sequential([
LSTM(90, input_shape=(10,1)),
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
当我拟合模型时
history = model.fit(X_train_res, y_train_res,epochs=5)
我得到了
ValueError: Shapes (None, 1) and (None, 90) are incompatible
看起来 y_train_res
包含整数索引而不是单热向量。如果是这样,您必须使用 sparse_categorical_crossentropy
:
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
并将其形状更改为一维:
y_train_res = np.reshape(y_train_fea.to_numpy(),(-1,))