我有xtrain.shape
(60000, 28, 28)
这意味着60000个通道,图像大小为28 * 28
我想制作一个keras顺序模型。
model = Sequential()
model.add(Convolution2D(32,3,activation='relu',input_shape=(????)))
model.add(Dense(10, activation='relu'))
model.summary()
input_shape应该是什么样的?
model = Sequential()
model.add(Dense(64,input_shape=(1,28,28)))
当我提出这个时,我收到了以下错误
Error when checking input: expected dense_31_input to have 4 dimensions, but got array with shape (60000, 28, 28)
为什么这需要4个尺寸?以及如何通过代码修复它?
答案 0 :(得分:1)
尝试将数据重塑为(60000,28,28,1)或(60000,1,28,28)。
答案 1 :(得分:1)
我有xtrain.shape
(60000, 28, 28)
这意味着60000个通道,图像大小为28 * 28
嗯,这当然不代表;它表示60000 样本,而非通道(MNIST是单通道数据集)。
在这种情况下无需重新发明轮子 - 看看Keras的MNIST CNN example:
from keras import backend as K
# input image dimensions
img_rows, img_cols = 28, 28
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first': # Theano backend
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else: # Tensorflow backend
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
# normalise:
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# 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)
# your model:
model = Sequential()
model.add(Convolution2D(32,3,activation='relu',input_shape=input_shape))
model.add(Dense(10, activation='softmax')) # change to softmax in the final layer
你还应该将最后一层的激活更改为softmax
(最有可能在最后一层之前添加一些合并和展平的图层)。
答案 2 :(得分:0)
第一个,
model = Sequential()
model.add(Convolution2D(32,3,activation='relu',input_shape=(60000,28,28)))
model.add(Dense(10, activation='relu'))
model.summary()
第二个,
model = Sequential()
model.add(Dense(64,input_shape=(None,60000,28,28)))