0 votes
1 view
in Python by (7.4k points)

I'm testing simple deep learning code on the MNIST data, but I'm getting an error I'm not sure why. The following code is from the book Deep learning with Python by Francois Chollet:

from keras.datasets import mnist

from keras import models

from keras import layers

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

train_images = train_images.reshape((60000, 28 * 28))

train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28*28))

test_images = test_images.astype('float32') / 255 

network = models.Sequential()

network.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))

network.add(layers.Dense(10, activation = 'softmax'))

network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

network.fit(train_images, train_labels, epochs=5, batch_size=128)

I'm getting the following error:

ValueError                                Traceback (most recent call last)

<ipython-input-9-fb9fd206ece1> in <module>

     18 network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

     19 

---> 20 network.fit(train_images, train_labels, epochs=5, batch_size=128)

~/.local/lib/python3.7/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)

    950             sample_weight=sample_weight,

    951             class_weight=class_weight,

--> 952             batch_size=batch_size)

    953         # Prepare validation data.

    954         do_validation = False

~/.local/lib/python3.7/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)

    787                 feed_output_shapes,

    788                 check_batch_axis=False,  # Don't enforce the batch size.

--> 789                 exception_prefix='target')

    790 

    791             # Generate sample-wise weight values given the `sample_weight` and

~/.local/lib/python3.7/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)

    136                             ': expected ' + names[i] + ' to have shape ' +

    137                             str(shape) + ' but got array with shape ' +

--> 138                             str(data_shape))

    139     return data

    140 

ValueError: Error when checking target: expected dense_9 to have shape (10,) but got array with shape (1,)

1 Answer

0 votes
by (14.6k points)

Your label arrays has got a shape (something, 1) where as your model need arrays of shape (something, 10). You need to convert your label arrays to categorical using keras.utils.to_categorical for examples. Like this :

from keras.utils import to_categorical

train_labels = to_categorical(train_labels)

test_labels = to_categorical(test_labels)

...