我和DL一起学习DL。遵循MNIST教程,但在调用model.fit
时收到以下错误。代码:
import keras
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Activation, Dropout, Dense, Flatten, Convolution2D, MaxPool2D, MaxPooling2D
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], 1, 28, 28)
x_test = x_test.reshape(x_test.shape[0], 1, 28, 28)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
Y_train = keras.utils.np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)
model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,28,28)))
model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile ( loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(x_train, Y_train, batch_size=32, nb_epoch=10, verbose=1)
score = model.evaluate(x_test, Y_test, verbose=0)
错误:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-41-d2e69a06c966> in <module>()
2 optimizer='adam',
3 metrics=['accuracy'])
----> 4 model.fit(x_train, Y_train, batch_size=32, epochs=10)
/anaconda/lib/python3.6/site-packages/keras/models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch,
**kwargs)
861 class_weight=class_weight,
862 sample_weight=sample_weight,
--> 863 initial_epoch=initial_epoch)
864
865 def evaluate(self, x, y, batch_size=32, verbose=1,
/anaconda/lib/python3.6/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, **kwargs) 1356 class_weight=class_weight, 1357 check_batch_axis=False,
-> 1358 batch_size=batch_size) 1359 # Prepare validation data. 1360 if validation_data:
/anaconda/lib/python3.6/site-packages/keras/engine/training.py in
_standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size) 1232 self._feed_input_shapes, 1233 check_batch_axis=False,
-> 1234 exception_prefix='input') 1235 y = _standardize_input_data(y, self._feed_output_names, 1236 output_shapes,
/anaconda/lib/python3.6/site-packages/keras/engine/training.py in
_standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
138 ' to have shape ' + str(shapes[i]) +
139 ' but got array with shape ' +
--> 140 str(array.shape))
141 return arrays
142
ValueError: Error when checking input: expected conv2d_11_input to have shape (None, 28, 28, 1) but got array with shape (60000, 1, 28, 28)
我做错了什么?
答案 0 :(得分:2)
更改此行:
x_train = x_train.reshape(x_train.shape[0], 1, 28, 28)
到
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
顺便提一句x_test
。