Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 因'的1减去3而导致的负尺寸;conv2/卷积';_Python_Tensorflow_Neural Network_Keras_Keras Layer - Fatal编程技术网

Python 因'的1减去3而导致的负尺寸;conv2/卷积';

Python 因'的1减去3而导致的负尺寸;conv2/卷积';,python,tensorflow,neural-network,keras,keras-layer,Python,Tensorflow,Neural Network,Keras,Keras Layer,我在Keras中声明输入层时收到此错误消息 ValueError:从1中减去3导致尺寸标注大小为负 “conv2d_2/卷积”(op:“conv2d”)与输入形状:[?、1,28,28], [3,3,28,32] 我的代码是这样的 model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,28,28))) 示例应用程序:默认情况下,Convolution2D()希望输入的格式为(样本、行、列、通道),即“通道最后一个

我在Keras中声明输入层时收到此错误消息

ValueError:从1中减去3导致尺寸标注大小为负 “conv2d_2/卷积”(op:“conv2d”)与输入形状:[?、1,28,28], [3,3,28,32]

我的代码是这样的

model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,28,28)))
示例应用程序:

默认情况下,Convolution2D()希望输入的格式为(样本、行、列、通道),即“通道最后一个”。您的数据的格式似乎是(样本、通道、行、列)。在声明卷积2D层时,您应该能够使用可选关键字
data\u format='channels\u first'
来修复此问题

model.add(Convolution2D(32, (3, 3), activation='relu', input_shape=(1,28,28), data_format='channels_first'))

我也有同样的问题,但是这个线程中提供的解决方案对我没有帮助。 在我的例子中,是另一个问题导致了这个错误:


代码
错误 图像大小是32乘32。在第一个卷积层之后,我们将其减少到30乘30。(如果我正确理解卷积)

然后池层将其除以15

然后另一个卷积层将其减少到13乘13

我希望你能看到这是怎么回事: 最后,我的特征映射太小,以至于我的池层(或卷积层)太大,无法覆盖它,这导致了错误


解决方案
解决此错误的简单方法是增大图像大小或使用较少的卷积层或池层。

Keras具有以下后端兼容性:

TensorFlow:由谷歌提供, Theano:由LISA实验室开发, CNTK:微软公司

每当您看到[?,X,X,X],[X,Y,Z,X]出现错误时,解决此Keras使用自动模式的通道问题:

进口

from keras import backend as K
K.set_image_dim_ordering('th')
“tf”格式意味着卷积核将具有以下形状(行、列、输入深度、深度)


这将始终有效…

您可以通过将值设置为“相同”来保留卷的空间尺寸,以便输出卷大小与输入卷大小匹配。 使用padding='same'

使用以下命令:

from keras import backend
backend.set_image_data_format('channels_last')
根据您的偏好,您可以使用
'channels\u first'
'channels\u last'
设置图像数据格式。()

如果这不起作用,并且你的图像尺寸很小,请尝试缩小你的CNN的架构,正如前面的海报所提到的那样

希望有帮助

    # define the model as a class
class LeNet:

  '''
      In a sequential model, we stack layers sequentially. 
      So, each layer has unique input and output, and those inputs and outputs 
      then also come with a unique input shape and output shape.

  '''

  @staticmethod                ## class can instantiated only once 
  def init(numChannels, imgRows, imgCols , numClasses, weightsPath=None):

    # if we are using channel first we have update the input size
    if backend.image_data_format() == "channels_first":
      inputShape = (numChannels , imgRows , imgCols)
    else: 
      inputShape = (imgRows , imgCols , numChannels)

    # initilize the model
    model = models.Sequential()

    # Define the first set of CONV => ACTIVATION => POOL LAYERS

    model.add(layers.Conv2D(  filters=6,kernel_size=(5,5),strides=(1,1), 
                              padding="valid",activation='relu',kernel_initializer='he_uniform',input_shape=inputShape))
    model.add(layers.AveragePooling2D(pool_size=(2,2),strides=(2,2)))
我希望这会有所帮助:)


请参阅代码:

我认为您需要使用3x3内核。在这种情况下,您应该编写
(3,3)
,而不是
3,3
。如何找出使用(3,3)或(4,4)或(5,5)的区别@ml4294i将首先尝试。请注意,它可以在~/.keras/keras.json中全局设置:“image_data_格式”:“channels_first”“最简单的解决方案几乎总是最好的。”这个答案让我可以在自己的环境中使用该解决方案,但在以后的keras版本中似乎已经重命名了一些内容:这非常重要!我也处于同样的处境,太棒了!我也有同样的问题,现在我知道为什么了!
    # define the model as a class
class LeNet:

  '''
      In a sequential model, we stack layers sequentially. 
      So, each layer has unique input and output, and those inputs and outputs 
      then also come with a unique input shape and output shape.

  '''

  @staticmethod                ## class can instantiated only once 
  def init(numChannels, imgRows, imgCols , numClasses, weightsPath=None):

    # if we are using channel first we have update the input size
    if backend.image_data_format() == "channels_first":
      inputShape = (numChannels , imgRows , imgCols)
    else: 
      inputShape = (imgRows , imgCols , numChannels)

    # initilize the model
    model = models.Sequential()

    # Define the first set of CONV => ACTIVATION => POOL LAYERS

    model.add(layers.Conv2D(  filters=6,kernel_size=(5,5),strides=(1,1), 
                              padding="valid",activation='relu',kernel_initializer='he_uniform',input_shape=inputShape))
    model.add(layers.AveragePooling2D(pool_size=(2,2),strides=(2,2)))