Skip to content Skip to sidebar Skip to footer

Error Defining An Input Shape In Keras For (60000, 28, 28) Array

I am setting up my first neural network with keras and tensorflow. I got my input into an array of shape (60000, 28, 28), but when I try and feed it to the model I get an error tha

Solution 1:

Keras Conv2D layer performs the convolution operation. It requires its input to be a 4-dimensional array. We have to reshape the input to ( , 1, 28, 28) or possibly to ( , 28, 28, 1), depending on your setup and backend (theano or tensorlow image layout convention).

from keras import backend as K
if K.image_data_format() == 'channels_first' :
   input_shape = (1, 28, 28)
   X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
   X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
else:
   input_shape = (28, 28, 1)
   X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
   X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)

So, you should reshape your data to (60000, 28, 28, 1) or (60000, 1, 28, 28)

Solution 2:

Two corrections are required.

  1. TF and Keras expects image dimension as (Width, Height, Channels), channels being 3 for RGB images and 1 for greyscale images.
model.add(InputLayer(input_shape=(28, 28, 1)))
  1. The training input to fit() method must be of dimension (Number of samples, Width, Height, Channels).
assert images_array.shape == (60000, 28, 28, 1)

Post a Comment for "Error Defining An Input Shape In Keras For (60000, 28, 28) Array"