Regina Kang
Regina Kang

Reputation: 151

Keras model. fit function has value error

I am trying to train a model with cifar-10. However, a value error occurs when I call the function fit. Pleas help me with what I have done wrong.

import tensorflow as tf 
from tensorflow import keras

from tensorflow.keras.datasets.cifar10 import load_data

import numpy as np
import matplotlib.pyplot as plt

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

class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
           'dog', 'frog', 'horse', 'ship', 'truck']

x_train.shape
len(y_train)
y_train
x_test.shape
len(y_test)


x_train=x_train/255.0
x_test=x_test/255.0

  model=keras.Sequential([
   keras.layers.Flatten(input_shape=(28, 28)),
   keras.layers.Dense(128, activation='relu'),
   keras.layers.Dense(10, activation='softmax')
])

   model.compile(optimizer='adam',
           loss='sparse_categorical_crossentropy',
            metrics=['accuracy'])

 model.fit(x_train,y_train, epochs=5,batch_size=32)

The below is the error message.

 ValueError: Error when checking input: expected flatten_6_input to 
have 3 dimensions, but got array with shape (50000, 32, 32, 3)

Upvotes: 0

Views: 235

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36604

I'm guessing you took a model designed for the MNIST and tried it on the CIFAR-10? There is just one adjustment to be made. You need to change the input shape.

Use:

input_shape=(32, 32, 3)

Because that's the picture size of the CIFAR-10 pictures. More generally, you can use:

input_shape=(x_train.shape[1:])

This specifies the input shape, omitting the batch dimension.

Upvotes: 1

Related Questions