[SOLVED] Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 250, 3), found shape=(None, 3) in trained transformer model

Issue

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)

Solution

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).

Answered By – paul-shuvo

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *