When running the code below I get an error, saying there is a shape mismatch, after searching on google I found this is commonly caused by using cross_entropy as a loss function when the target is not one-hot encoded. Since mine isn't one-hot encoded I used sparse_categorical_crossentropy instead, but the error is still there.
Here is the error I recieved :
Train on 60000 samples
Epoch 1/100
32/60000 [..............................] - ETA: 1:18
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-123-4ce1d4d047d4> in <module>()
----> 1 model.fit(x_train, y_train, batch_size=32, epochs=100, verbose=1)
25 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/nn_ops.py in sparse_softmax_cross_entropy_with_logits(_sentinel, labels, logits, name)
3391 "should equal the shape of logits except for the last "
3392 "dimension (received %s)." % (labels_static_shape,
-> 3393 logits.get_shape()))
3394 # Check if no reshapes are required.
3395 if logits.get_shape().ndims == 2:
ValueError: Shape mismatch: The shape of labels (received (32,)) should equal the shape of logits except
for the last dimension (received (5408, 10)).
This is my code:
import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import fashion_mnist
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0
x_train = np.reshape(x_train, (-1,28,28,1))
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=2, input_shape=[28,28,1], activation='relu'))
model.add(tf.keras.layers.Dense(units = 10, activation='softmax'))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'])
model.fit(x_train, y_train, batch_size=32, epochs=100, verbose=1)