简体   繁体   English

Keras 负维度 size Conv2D

[英]Keras negative dimension size Conv2D

I have been playing around with the kernel sizes and channel arrangements for a while with no luck.我一直在玩 kernel 大小和频道安排一段时间,但没有运气。 I am not entirely sure of how to calculate the corrector parameters for the Conv2D layers and I am unsure how much changing these parameters will affect the similarity to the model in the paper.我不完全确定如何计算 Conv2D 层的校正器参数,我不确定这些参数的变化有多大会影响与论文中 model 的相似性。

Any help would be greatly appreciated.任何帮助将不胜感激。

The model that I attempting to build based on the design in the literature我尝试根据文献中的设计构建的model

input_shape = (4, 30, 180)
model = Sequential()
model.add(Convolution2D(32, (8, 8), strides=(4,4), activation='relu', input_shape=(4,30,180), data_format='channels_first'))
model.add(Activation('relu'))
model.add(Convolution2D(64, (4, 4), strides=(2, 2)))
model.add(Activation('relu'))
model.add(Convolution2D(64, (3, 3), strides=(1, 1)))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dense(2))
model.add(Activation('linear'))

The error messages that I recieved我收到的错误信息

Traceback (most recent call last):
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1659, in _create_c_op
    c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Negative dimension size caused by subtracting 3 from 2 for 'conv2d_3/convolution' (op: 'Conv2D') with input shapes: [?,15,2,64], [3,3,64,64].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "stock_env.py", line 101, in <module>
    model.add(Convolution2D(64, (3, 3), strides=(1, 1)))
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/keras/engine/sequential.py", line 181, in add
    output_tensor = layer(self.outputs[0])
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py", line 457, in __call__
    output = self.call(inputs, **kwargs)
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/keras/layers/convolutional.py", line 171, in call
    dilation_rate=self.dilation_rate)
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 3650, in conv2d
    data_format=tf_data_format)
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 851, in convolution
    return op(input, filter)
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 966, in __call__
    return self.conv_op(inp, filter)
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 591, in __call__
    return self.call(inp, filter)
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 208, in __call__
    name=self.name)
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 1026, in conv2d
    data_format=data_format, dilations=dilations, name=name)
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
    op_def=op_def)
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1823, in __init__
    control_input_ops)
  File "/Users/zacharyfrederick/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1662, in _create_c_op
    raise ValueError(str(e))
ValueError: Negative dimension size caused by subtracting 3 from 2 for 'conv2d_3/convolution' (op: 'Conv2D') with input shapes: [?,15,2,64], [3,3,64,64].

You have this error because your Kernels and strides are too big for your inputs, a common start is to use kernels of shape (3, 3) and strides (1, 1) .你有这个错误是因为你的 Kernels 和 strides 对于你的输入来说太大了,一个常见的开始是使用形状为(3, 3)和 strides (1, 1)的内核。

Try reading on how a convolution is computed to give you intuition on how to set the correct kernel/stride size: http://cs231n.github.io/convolutional.networks/尝试阅读如何计算卷积,让您直观地了解如何设置正确的内核/步幅大小: http://cs231n.github.io/convolutional.networks/

Moreover you have an input with channel first , so you setted your first conv with channel first , it's great but you to do this for all your convolutions, because by default keras convolution's will use channel last .此外,你有一个channel first的输入,所以你首先设置你的第一个 conv channel first ,这很好,但是你对所有的卷积都这样做,因为默认情况下 keras 卷积将channel last

For exemple, this is working:例如,这是有效的:

input_shape = (4, 30, 180)
model = Sequential()
model.add(Conv2D(32, (8, 8), strides=(4, 4), activation='relu', input_shape=(4, 30, 180), data_format='channels_first'))
model.add(Activation('relu'))
model.add(Conv2D(64, (4, 4), strides=(1, 1), data_format='channels_first'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3), strides=(1, 1), data_format='channels_first'))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dense(2))
model.add(Activation('linear'))

The other answer is correct in diagnosis: after convolutions your image gets reduced and at some point the kernel becomes bigger than image.另一个答案在诊断中是正确的:卷积后你的图像变小,在某个时候 kernel 变得比图像大。 Try尝试

1) lowering your kernel sizes or 1) 降低您的 kernel 尺寸或

2) adding , padding='same' to your convolution layer. 2)添加, padding='same'到你的卷积层。

use Calculate the Output size in Convolution layer to calculate your outputs sizes.使用计算卷积层中的 Output 大小来计算输出大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CNN Keras:ValueError:负数尺寸是由&#39;conv2d的2减去3引起的 - CNN Keras: ValueError: Negative dimension size caused by subtracting 3 from 2 for 'conv2d &#39;Conv2D&#39; 从 1 中减去 3 导致的负尺寸大小 - Negative dimension size caused by subtracting 3 from 1 for 'Conv2D' '{{node conv2d_3/Conv2D} 从 1 中减去 3 导致的负维度大小 - Negative dimension size caused by subtracting 3 from 1 for '{{node conv2d_3/Conv2D} Keras-输入尺寸为[?,4,80,64],[5,5,64,64]的&#39;conv2d_5 / convolution&#39;(op:&#39;Conv2D&#39;)的4中减去5引起的负尺寸大小 - Keras - Negative dimension size caused by subtracting 5 from 4 for 'conv2d_5/convolution' (op: 'Conv2D') with input shapes: [?,4,80,64], [5,5,64,64] “ Encoder / conv6 / Conv2D”的2减去3导致的负尺寸大小 - Negative dimension size caused by subtracting 3 from 2 for 'Encoder/conv6/Conv2D' 多个Conv1D图层:由于&#39;conv1d_2 / convolution / Conv2D从1中减去8而导致的负尺寸大小 - Multiple Conv1D Layers: Negative dimension size caused by subtracting 8 from 1 for 'conv1d_2/convolution/Conv2D 齿轮Conv1D的负尺寸 - negative dimension size on kears Conv1D Keras Conv2D 解码器 - Keras Conv2D decoder Keras Conv2D 内核 - Keras Conv2D kernel 输入到tf.keras的Conv2D层的大小不正确 - Input to tf.keras Conv2D layer not of appropriate size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM