首页 文章

确定Keras的Mnist输入形状

提问于
浏览
1

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

3 回答

  • 1

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

  • 0

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

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

相关问题