【问题标题】:Conv2D layer output shape in keraskeras中的Conv2D层输出形状
【发布时间】:2017-09-12 11:03:07
【问题描述】:

我想手动输入 keras Conv2D 层。

我采用 MNIST 数据集。

Conv2D 只接受张量,所以我使用 keras 的 Input 命令将 x_train 更改为 x_train_tensor

我的输入是 keras 指令中给出的格式

(samples,rows, cols,channels) 

示例输入:

(60000,128,128,1) 

我希望输出类似于:

(None, 26, 26, 32) 

我得到:

shape=(?, 59998, 26, 32)

我做错了什么?

我的代码:

import keras
from keras.datasets import mnist
from keras.layers import Conv2D
from keras import backend as K
from keras.layers import Input
batch_size = 128
num_classes = 10
epochs = 1
# 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':
    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:
    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)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

x_train_tensor=Input(shape=(60000,28,28), name='x_train')
A=Conv2D(32, kernel_size=(3, 3),
       activation='relu',
       input_shape=input_shape)(x_train_tensor)

【问题讨论】:

    标签: python-3.x keras


    【解决方案1】:

    样本数量不是input_shape 的一部分,在您的情况下,您犯了两个错误。首先是输入形状错误,其次是指定两个输入形状,一次在 Input 构造函数中,第二次在 Conv2D 实例中:

    x_train_tensor=Input(shape=(28, 28, 1), name='x_train')
    A=Conv2D(32, kernel_size=(3, 3), activation='relu')(x_train_tensor)
    

    【讨论】:

      猜你喜欢
      • 2020-04-29
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多