0
$\begingroup$

I am working on the mnist classification code. Such errors continue to occur in the code 코드 below.

import tensorflow as tf

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
print(x_train.shape) # (60000, 28, 28)
print(y_train.shape)

import matplotlib.pyplot as plt

print("Y[0] : ",  y_train[0])
plt.imshow(x_train[0], cmap=plt.cm.gray_r, interpolation = "nearest")

x_train = x_train.reshape(-1,28*28)
x_test = x_train / 255.0
x_test / 255.0

y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)

This is the error occurrence code.

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(units=10, input_dim=784, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=tf.optimizers.Adam(learning_rate=0.001), metrics=['accuracy'])
model.summary()

model.fit(x_train, y_train, batch_size=100, epochs=10, validation_data=(x_test, y_test))


ValueError: Shapes (100, 10, 10) and (100, 10) are incompatible

This is my error message.

Initially, a reshape error occurred, so x_trial.reshape (-1,28*28) was added to the code. Then, this error occurs. How should I change the shape?

$\endgroup$

2 Answers 2

0
$\begingroup$

When I ran the code you provided I do not get the mentioned error, but an error related to an differing number of samples for the x and y variables. This was caused by the line in your code where you are normalizing the data from x_train but assign the output to x_test, leaving you with 60000 observations in x_test but only 10000 in y_test. After changing this the code runs fine:

import tensorflow as tf
import matplotlib.pyplot as plt

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

x_train = x_train.reshape(-1, 28 * 28)
x_train = x_train / 255.0 # changed to save output to x_train instead of x_test
x_test = x_test.reshape(-1, 28 * 28) # reshaped x_test to get the correct dimensions
x_test = x_test / 255.0 # also apply normalization to the test data

y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(units=10, input_dim=784, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=tf.optimizers.Adam(learning_rate=0.001), metrics=['accuracy'])
model.summary()

model.fit(x_train, y_train, batch_size=100, epochs=10, validation_data=(x_test, y_test))
$\endgroup$
0
$\begingroup$

The error occurs because of the x_test shape. In your code, you set it actually to x_train. [x_test = x_train / 255.0] Furthermore, if you feed the data as a vector of 784 you also have to transform your test data. So change the line to x_test = (x_test / 255.0).reshape(-1,28*28).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.