Conv2D图层在Keras中的输出形状 [英] Conv2D layer output shape in keras

查看:80
本文介绍了Conv2D图层在Keras中的输出形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想手动将输入提供给keras Conv2D层.

I want to manually give input to keras Conv2D layer.

我获取MNIST数据集.

I take MNIST data set.

Conv2D仅接受张量,因此我使用keras的Input命令将x_train更改为x_train_tensor.

Conv2D accepts only tensors, so I change x_train to x_train_tensor using the Input command of keras.

我的输入采用的是keras说明中给出的格式

My input is in the format given in the keras instructions

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

推荐答案

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

The number of samples is not part of the input_shape, in your case you are making two mistakes. First is having the wrong input shape, and second is specifying two input shapes, once in the Input constructor, and second in the Conv2D instance:

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

这篇关于Conv2D图层在Keras中的输出形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆