Skip to content
Advertisement

Keras model fits on data with the wrong shape

I’ve created the following model:

JavaScript

and the following dummy data:

JavaScript

with the shapes of (4, None, 2) and (4, 3).
Looking at the model structure one can see that the model has 3 outputs of shape (None, 1).

model structure


I was wondering how come the fit works, when I expected they to be of shape (4, 3, 1) and not (4, 3).

JavaScript

So I added one output to the target and tested the same model with a y of shape (4, 4) and the fit works …. I’m lost.

Question: How should I shape my y to fit the model and what actually happened when I gave it the wrong y shape?

Code on Colab

Advertisement

Answer

Both are correct. Take a look at this and this. As you can see that this says ‘Squeeze or expand last dimension if needed’ and so after doing that if the dimensions match then it’s all good.

First of all remember that everything depends on your loss function. Below I will show one example:

JavaScript

And hence both are working fine in this case.

Advertisement