简体   繁体   English

ValueError:层“顺序”的输入0与层不兼容:预期形状=(无,90),发现形状=(无,2,90)

[英]ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 90), found shape=(None, 2, 90)

Can anybody help with the following problem when using Keras predict function the input shape for the prediction dataset seems to be changing (predict seems to add 'none' to the first dimension).使用 Keras 预测 function 时,任何人都可以帮助解决以下问题吗?预测数据集的输入形状似乎正在改变(预测似乎在第一维中添加了“无”)。

scaler = MinMaxScaler()
scaler2 = MinMaxScaler()

normalized_data = scaler.fit_transform(dataset)
normalized_predict_data = scaler2.fit_transform(predict_dataset)

x = normalized_data[:, 0:90]
y = normalized_data[:, 90]

z = normalized_predict_data[:, 0:90]
print(z.shape)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=10)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

model = Sequential()
model.add(Dense(4, input_dim=90, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

history = model.fit(x_train, y_train, validation_split=0.33, epochs=50, batch_size=100, verbose=0)

loss, accuracy = model.evaluate(x_test, y_test, verbose=0)
print("Model loss: %.2f, Accuracy: %.2f" % ((loss * 100), (accuracy * 100)))

Xnew = z
ynew = model.predict(array([Xnew]))

for item in Xnew:
    print("X=%s, Predicted=%s" % (item, ynew[0]))

When calling the print function to show the shape of the prediction dataset this prints out (2, 90) as expected (2 rows of data and 90 inputs)当调用 print function 来显示预测数据集的形状时,它会按预期打印出 (2, 90)(2 行数据和 90 个输入)

When trying to use the predict function this instead prints the following error:当尝试使用预测 function 时,它会打印以下错误:

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 90), found shape=(None, 2, 90)

The error is caused by this code line ynew = model.predict(array([Xnew])) .该错误是由此代码行ynew = model.predict(array([Xnew]))引起的。

Please remove the array from this line and use this: ynew = model.predict(Xnew)请从此行中删除数组并使用: ynew = model.predict(Xnew)

I have replicated the similar code with an abalone dataset and attached this gist for your reference.我已经用鲍鱼数据集复制了类似的代码,并附上了这个要点供您参考。

Either of the following work for me (my model was trained to take 2D input):以下任一项对我有用(我的 model 被训练接受 2D 输入):

X_new = [[-1.0, -1.0]]
model.predict(X_new)

or或者

X_new = [-1.0, -1.0]
model.predict([X_new])

Hope that helps!希望有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:“顺序”层的输入 0 与层不兼容:预期形状 =(无,223461,5),找到形状 =(无,5) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(None, 455, 30),找到的形状 =(None, 30) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 455, 30), found shape=(None, 30) ValueError: 层“sequential_1”的输入 0 与层不兼容:预期形状=(None, 60, 1),发现形状=(None, 59, 1) - ValueError: Input 0 of layer "sequential_1" is incompatible with the layer: expected shape=(None, 60, 1), found shape=(None, 59, 1) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(无,33714,12),找到形状 =(无,12) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 33714, 12), found shape=(None, 12) | ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(None, 28, 28),找到的形状 =(None, 28, 3) - | ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 28, 28), found shape=(None, 28, 3) ValueError: 层“sequential_32”的输入 0 与层不兼容:预期形状=(None, 3, 1),发现形状=(32, 0, 1) - ValueError: Input 0 of layer "sequential_32" is incompatible with the layer: expected shape=(None, 3, 1), found shape=(32, 0, 1) Keras LSTM ValueError:层“顺序”的输入 0 与层不兼容:预期形状 =(无,478405,33),找到形状 =(1、33) - Keras LSTM ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 478405, 33), found shape=(1, 33) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状=(无,128,128,3),找到形状=(32,128,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(32, 128, 3) ValueError:层“顺序”的输入 0 与层不兼容:预期形状=(无,32,32,3),找到形状=(32,32,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3) ValueError: 层序的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。 收到的完整形状:(无,1) - ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM