简体   繁体   English

使用 Keras 和 Tensorflow 的 model.predict 问题

[英]problem with model.predict , using Keras and Tensorflow

Here i give a test_input of 28 cols and +1000 row the trained model should accept it but i'm getting an error of shape compatibility在这里,我给出了 28 列和 +1000 行的 test_input 训练模型应该接受它,但我得到了形状兼容性错误在此处输入图片说明

here is my code:这是我的代码:

my_data = genfromtxt('test_rgb.csv', delimiter=',',skip_header=1)
test_data=my_data[0:,1:-1]

for test_row in test_data:
    predictions = model.predict(test_row)
    print(predictions)

When you use predict you must supply your model with tensor (list of vectors) which means that if you want to make a prediction from only one piece of data you must reshepe it like (1,(INPUT_SHAPE))当您使用 predict 时,您必须为您的模型提供张量(向量列表),这意味着如果您只想从一个数据中进行预测,您必须像 (1,(INPUT_SHAPE))

in your case try to use if在你的情况下尝试使用 if

model.predict(np.array(test_row).reshape((1,a,b,c)))

you specified input_shape=(a,b,c,)您指定 input_shape=(a,b,c,)

It says right in the error, the input shape your model needs (28,), the data you are providing has a shape of (1,)它在错误中说正确,您的模型需要的输入形状 (28,),您提供的数据的形状为 (1,)

You can try printing the shape of your data prior to your predict method to verify.您可以在使用预测方法之前尝试打印数据的形状以进行验证。 You probably just need to reshape your data.您可能只需要重塑数据。

Thanks all, the solution is to reshape the row_test谢谢大家,解决方案是重塑 row_test

test_row=test_row.reshape(1,28) test_row=test_row.reshape(1,28)

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

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