I am following a Keras mnist example for beginners. I have tried to change the labels to suit my own data which has 3 distinct text classifications. I am using "to_categorical" to achieve this. The shape looks right to me, but "fit" gets an error:
train_labels = keras.utils.to_categorical(train_labels, num_classes=3)
print(train_images.shape)
print(train_labels.shape)
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(3, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5)
(7074, 28, 28)
(7074, 3)
Blockquote
Blockquote
Traceback (most recent call last): File
"C:/Users/lawrence/PycharmProjects/tester2019/KeraTest.py", line 131,
in
model.fit(train_images, train_labels, epochs=5) File "C:\Users\lawrence\PycharmProjects\tester2019\venv\lib\site-packages\tensorflow\python\keras\engine\training.py",
line 1536, in fit
validation_split=validation_split) File "C:\Users\lawrence\PycharmProjects\tester2019\venv\lib\site-packages\tensorflow\python\keras\engine\training.py",
line 992, in _standardize_user_data
class_weight, batch_size) File "C:\Users\lawrence\PycharmProjects\tester2019\venv\lib\site-packages\tensorflow\python\keras\engine\training.py",
line 1154, in _standardize_weights
exception_prefix='target') File "C:\Users\lawrence\PycharmProjects\tester2019\venv\lib\site-packages\tensorflow\python\keras\engine\training_utils.py",
line 332, in standardize_input_data
' but got array with shape ' + str(data_shape)) ValueError: Error when checking target: expected dense_1 to have shape (1,) but got
array with shape (3,)
解决方案
You need to use categorical_crossentropy instead of sparse_categorical_crossentropy as loss since your labels are one hot encoded.
Alternatively, you can use sparse_categorical_crossentropy if you do not one hot encode the labels. In that case, the labels should have shape (batch_size, 1).