close

[Solved] ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28]

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error alueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28] in Python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How alueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28] Error Occurs?

Today I get the following error alueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28] in Python.

How To Solve alueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28] Error ?

  1. How To Solve alueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28] Error ?

    To Solve alueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28] Error This means that you have to reshape your training set with .reshape(n_images, 286, 384, 1). Now you have added an extra dimension without changing the data and your model is ready to run.

Solution 1

The input layers of the model you created needs a 4 dimension tensor to work with but the x_train tensor you are passing to it has only 3 dimensions

This means that you have to reshape your training set with .reshape(n_images, 286, 384, 1). Now you have added an extra dimension without changing the data and your model is ready to run.

you need to reshape your x_train tensor to a 4 dimension before training your model. for example:

x_train = x_train.reshape(-1, 28, 28, 1)

Solution 2

You need to add a channel dimension. Keras expects this data format:

(n_samples, height, width, channels)

For instance this, if your images are greyscale, they have 1 channel, and so they need to be given to Keras in this format:

(60000, 28, 28, 1)

Unfortunately, grayscale pictures will often be given/downloaded without a channel dimension, for instance in tf.keras.datasets.mnist.load_data, which will be (60000, 28, 28), which is problematic.

Solution:

You can use tf.expand_dims to add a dimension

xtrain = tf.expand_dims(xtrain, axis=-1)

Now your input shape will be:

(60000, 28, 28, 1)

There are other alternatives that do the same:

xtrain = xtrain[..., np.newaxis]
xtrain = xtrain[..., None]
xtrain = xtrain.reshape(-1, 28, 28, 1)
xtrain = tf.reshape(xtrain, (-1, 28, 28, 1))
xtrain = np.expand_dims(xtrain, axis=-1)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment