简体   繁体   中英

ValueError: Error when checking input: expected embedding_1_input to have shape (256,) but got array with shape (1,)

I get some errors when I run the code in this tutorial . I want to predict on some test data. When I run the following it works:

res = model.predict(test_data[0:2], verbose=1)   # this works
[[0.25896776]
 [0.9984256 ]]

However, when I run the following piece of code:

res = model.predict(test_data[0], verbose=1)     # this does not work 

It gives me the following error:

ValueError: Error when checking input: expected embedding_1_input to have shape (256,) but got array with shape (1,)

This is the test_data[0] shape and details. How can I fix this issue?

Short answer: Use test_data[0:1] instead of test_data[0] .

Long answer: The Keras/TF models works on batch of input samples. Therefore, when you give them only one input sample, it should still have a shape of (1, sample_shape) . However, when you slice the test_data array like test_data[0] it would give you the first element with the first axis/dimension removed, ie with the shape of (sample_shape,) (in this case (256,) ). To resolve this, use test_data[0:1] in order to preserve the first axis/dimension (ie shape would be (1, 256) ).

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