0

I have a keras transformer model trained with tensorflow 2.7.0 and python 3.7 with input shape: (None, 250, 3) and a 2D array input with shape: (250, 3)(not an image)

When making a prediction with:

prediction = model.predict(state)

I get ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 250, 3), found shape=(None, 3)

project code: https://github.com/MikeSifanele/TT

This is how state looks like:

state = np.array([[-0.07714844,-0.06640625,-0.140625],[-0.140625,-0.1650391,-0.2265625]...[0.6376953,0.6005859,0.6083984],[0.7714844,0.7441406,0.7578125]], np.float32)

4
  • Could you share any code?
    – paul-shuvo
    Mar 16, 2022 at 20:31
  • @paul-shuvo I've uploaded the project Mar 17, 2022 at 6:31
  • Could you try adding a new axis like state = np.expand_dims(state, axis=0) and run the code and see if that works?
    – paul-shuvo
    Mar 17, 2022 at 7:10
  • 1
    Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example.
    – Community Bot
    Mar 17, 2022 at 8:19

1 Answer 1

0

Some explanation:

For input shape to the model i.e. (None, 250, 3), the first axis (represented by None) is the "sample" axis, while the rest i.e. 250,3 denotes the input dimension. Thus, when the input shape is (250, 3) it assumes the first axis as the "sample" axis and the rest as the input dimension i.e. just 3. So, to make it consistent we need to add a dimension at the beginning described in the following:

state = np.expand_dims(state, axis=0)

The shape of state then becomes (1, 250, 3) ~(None, 250, 3).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.