[英]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.