1
$\begingroup$

After building up the mlp using

## building a mlp model
model=Sequential()
model.add(Dense(25,input_shape=(10,),activation='relu'))
model.add(Dense(100,input_shape=(10,),activation='relu'))
model.add(Dense(150,input_shape=(16,),activation='relu'))
model.add(Dense(10,input_shape=(10,),activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='Adam',metrics=['accuracy'])

When I'm trying to fit the model using:

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

I'm getting this error:

ValueError Traceback (most recent call last) in 1 # Training the MLP on the 2D data ----> 2 model.fit(x_train, y_train, epochs=10,validation_data=(x_test,y_test))

~\anaconda\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs) 950 sample_weight=sample_weight, 951 class_weight=class_weight, --> 952 batch_size=batch_size) 953 # Prepare validation data. 954 do_validation = False

~\anaconda\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size) 749 feed_input_shapes, 750 check_batch_axis=False, # Don't enforce the batch size. --> 751 exception_prefix='input') 752 753 if y is not None:

~\anaconda\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix) 136 ': expected ' + names[i] + ' to have shape ' + 137 str(shape) + ' but got array with shape ' + --> 138 str(data_shape)) 139 return data 140

ValueError: Error when checking input: expected dense_29_input to have shape (10,) but got array with shape (3072,)

Can someone please help me find the mistake?

$\endgroup$

1 Answer 1

2
$\begingroup$

The problem here is the input_shape argument you are using, firstly that is the wrong shape and you should only provide an input shape for your first layer.

For example

Let's import the CIFAR 10 data from Keras

from __future__ import print_function
import keras
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
import os

num_classes = 10

# The data, split between train and test sets:
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

input_shape = x_train.shape[1:]
print('input_shape: ', input_shape)

x_train shape: (50000, 32, 32, 3)
50000 train samples
10000 test samples
input_shape: (32, 32, 3)

Now we can define our model. Note that I only use the input_shape in the first layer and furthermore, if you want to use a Dense layer as your first layer then you should flatten your inputs first.

model=Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(25,activation='relu'))
model.add(Dense(100,activation='relu'))
model.add(Dense(150,activation='relu'))
model.add(Dense(10,activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='Adam',metrics=['accuracy'])

You can use this to see your model

model.summary()

Now you can fit your model

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

Since CIFAR 10 is comprised of image data I would not recommend you use Dense layers early in your model. You should rather use a Convolutional Neural Network (CNN). These layers act as a filter which extracts features from a neighborhood region of the image. This reduces the number of model parameters which will lead to better performance. From the Keras docs found here:

model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
                 input_shape=x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
$\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.