Skip to content Skip to sidebar Skip to footer

Keras Input Shape Throws Value Error Expected 4d But Got An Array With Shape (60000, 28,28)

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data() x_train = x_train.astype('float32') / 255 x_test = x_test.astype('float32') / 255 x_train.shape

Solution 1:

Yes, this is correct the parameter input_shape is prepared to take 3 values. However the function Conv2D is expecting a 4D array as input, covering:

  1. Number of samples
  2. Number of channels
  3. Image width
  4. Image height

Whereas the function load_data() is a 3D array consisting of width, height and number of samples.

You can expect to solve the issue with a simple reshape:

train_X = train_X.reshape(-1, 28,28, 1)
test_X = test_X.reshape(-1, 28,28, 1)

A better defitinion from keras documentation:

Input shape: 4D tensor with shape: (batch, channels, rows, cols) if data_format is "channels_first" or 4D tensor with shape: (batch, rows, cols, channels) if data_format is "channels_last".

Solution 2:

You are missing the channels dimension (with a value of one), it can be easily corrected by reshaping the array:

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

Post a Comment for "Keras Input Shape Throws Value Error Expected 4d But Got An Array With Shape (60000, 28,28)"