确定Keras的Mnist输入形状

问题描述 投票:1回答:3

我有xtrain.shape

(60000, 28, 28)

它意味着60000个通道,图像大小为28 * 28

我想制作一个keras顺序模型。

specifying the model shape

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个尺寸?以及如何修复表单代码?

machine-learning keras neural-network conv-neural-network mnist
3个回答
1
投票

尝试将数据重塑为(60000,28,28,1)或(60000,1,28,28)。


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(并且最有可能在最终密集层之前添加一些池和展平层)。


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)))
© www.soinside.com 2019 - 2024. All rights reserved.