1

I followed the tutorial correctly so I don't know what i did wrong. This is my code:

model = Sequential([
                    Conv2D(32, (3,3), padding='same', activation='relu', input_shape=(28,28,1)),
                    MaxPooling2D((2,2)),
                    Conv2D(64,(3,3), activation='relu'),
                    MaxPooling2D((2,2)),
                    Flatten(),
                    Dense(128, activation='relu'),
                    Dense(10)
])

#Compile the model
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])

#Train the model
history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))


ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [32, 28, 28]
  • Does this answer your question? [ValueError: Input 0 is incompatible with layer lstm\_13: expected ndim=3, found ndim=4](https://stackoverflow.com/questions/44583254/valueerror-input-0-is-incompatible-with-layer-lstm-13-expected-ndim-3-found-n) – Ben Jul 02 '20 at 17:16

2 Answers2

0

your train_images array seems to have this format (n_images, height, width). in order to apply convolution operation, you have to explicit a features dims. in case of black and white images the channel is 1. so u can simply solve modifying your images adding the channel dimension before fitting

train_images = train_images[:,:,:,None]
test_images = test_images[:,:,:,None]

the input shape of the model is correctly defined input_shape=(28,28,1).

I also suggest u apply softmax as the last activation of your output layer because your is a multiclassification problem

Marco Cerliani
  • 21,233
  • 3
  • 49
  • 54
0
mnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
training_images = training_images.reshape(training_images.shape[0],28,28,1)
test_images = test_images.reshape(test_images.shape[0],28,28,1)

After loading training and test images, you need to resize train and test image