ValueError: Data cardinality is ambiguous in colab

Initially, only x_train was reshape, and an error occurred, so x_test was also reshape. Then, I have another error. It seems to be an error caused by inconsistency with y data, but modifying the code does not solve the error.

This is my code

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) # (60000, )
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 = x_test.reshape(-1, 28 * 28)
x_test = x_test / 255.0
y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)

and This is the code where the error occurs.

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))

this is error message

ValueError: Data cardinality is ambiguous: x sizes: 60000 y sizes: 10000 Make sure all arrays contain the same number of samples.

Topic colab mnist tensorflow

Category Data Science

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.