ValueError: 层顺序的输入 0 与层不兼容:

时间:2021-03-02 10:59:56

标签: tensorflow keras neural-network callback artificial-intelligence

model = keras.models.Sequential([
keras.layers.Dense(30, activation = "relu", input_shape=[8]),
keras.layers.Dense(100, activation = "relu"),
keras.layers.Dense(1)])

model.compile(loss="mse", optimizer=keras.optimizers.SGD(lr=1e-3))

checkpoint_cb = keras.callbacks.ModelCheckpoint("Model-{epoch:02d}.h5")

history = model.fit(X_train, y_train, epochs=10,
             validation_data=(X_valid,y_valid),
               callbacks=[checkpoint_cb])

我正在尝试使用回调拟合模型,但出现以下错误:

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape [None, 28, 28]

可能的错误是什么?

1 个答案:

答案 0 :(得分:0)

您的 X_train 的形状是 (None,28,28),但您将形状 (None,8) 输入到密集层。

重塑你的 X_train

X_train = X_train.reshape(-1, 28*28)

模型应该是

model = keras.models.Sequential([
keras.layers.Dense(30, activation = "relu", input_shape=(784,)),
keras.layers.Dense(100, activation = "relu"),
keras.layers.Dense(1)])