I need to trie coding fashion mnist with Keras and TensorFlow , and I got this error when I tried to fit model with print train_image_model, train_lables_catg, test_image_model, test_lables_catg, I saw several questions similar to the error shown to me, but I couldn't find the exact error in my code.
(train_and_validation_images, train_and_validation_labels), (test_images, test_labels) = fashion_mnist.load_data()
validation_images = train_and_validation_images[-10000:, :, :]
validation_labels = train_and_validation_labels
# Construct a training set from the first 50000 images and labels.
train_images = train_and_validation_images[:50000, :, :]
train_labels = train_and_validation_labels
# Flatten
train_images_flatten = train_images.reshape((train_images.shape[0], 28, 28, 1))
validation_images_flatten = train_images_flatten
test_images_flatten = test_images.reshape(test_images.shape[0], 784)
# Normalize
train_images_model = train_images_flatten.astype("float")/255.
validation_images_model = train_images_model
test_images_model = test_images_flatten.astype("float")/255.
# convert class vectors to binary class matrices
from keras.utils import np_utils
train_labels_categ = keras.utils.np_utils.to_categorical(train_labels, 10)
validation_labels_categ = keras.utils.np_utils.to_categorical(validation_labels, 10)
test_labels_categ = keras.utils.np_utils.to_categorical(test_labels, 10)
batch_size = 128
num_classes = 10
epochs = 40
# Create a sequential model here
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(32,32,3)),
tf.keras.layers.Dropout(.2, input_shape=(256,)),
tf.keras.layers.Conv2D(32, (3, 3), padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.summary()
# End Code
model.compile(loss='categorical_crossentropy',optimizer=RMSprop(),metrics=['accuracy'])
history = model.fit(train_images_model, train_labels_categ,
batch_size=batch_size,
epochs=epochs,
verbose=0,
validation_data=(test_images_model, test_labels_categ)) #here i have error
score = model.evaluate(test_images_model, test_labels_categ, verbose=0)
print('Test accuracy:', score[1])
CodePudding user response:
Your train_images
array has dimension 50000 on first axis. But train_labels
probably has full shape, 60000 according to the error.
You have to make them have the same shape on first axis. For example you could slice train_labels
:
train_labels = train_and_validation_labels[:50000]
Or you could have not sliced train_images
.