确定Keras的Mnist输入形状

3

我有一个xtrain.shape,它的形状为

(60000, 28, 28)

这意味着有60000个通道,图像大小为28 * 28。

我想创建一个Keras Sequential模型。

指定模型形状

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个维度?如何从代码中解决这个问题?
3个回答

2

I have xtrain.shape as

(60000, 28, 28)

It means 60000 channels with image size 28 * 28

好的,它肯定不是那个意思;它的意思是60000个样本,而不是通道(MNIST是一个单通道数据集)。

在这种情况下没有必要重新发明轮子 - 可以看看Keras中的MNIST CNN示例

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

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

1
第一个,

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)))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接