Search code examples
pythontensorflowkerasvalueerror

ValueError: Error when checking target: expected (keras Sequence model layer) to have n dimensions, but got array with shape


I have loaded images to train my model on recognizing one feature in those images.

  • Xtrain is a numpy ndarray of shape (1380,200,200,3 ) containing 1380 images sized 200 by 200pixels in RGB format
  • Ytrain has targets. shape (1380,2)

    shape and model layers

When I train my model (model.fit(Xtrain,Ytrain)) I seem to get a value error on everyone of the layers. As if the input was both Xtrain then Ytrain...

ValueError: Error when checking target: expected batch_normalization_24 to have 4 dimensions, but got array with shape (1380, 2)

Image:

error message


Solution

  • The shape of Keras's batch normalizer layer's output is the same as its input. Since you have only two labels, your final layer in a Sequential model should generate two outputs. You can consider adding a Dense layer like:

    model.add(Dense(2), activation='relu')
    

    I also recommend to check your model's architecture using print(model.summary()) and make sure that inputs and outputs match with your dataset and vice versa.