简体   繁体   English

齿轮Conv1D的负尺寸

[英]negative dimension size on kears Conv1D

I'm using Keras 's model api to apply a 1D convolution to an input 1d vector of size 20. I want five kernels of size 3 each. 我正在使用Keras的模型api将1D卷积应用于大小为20的输入1d向量。我想要每个大小为3的五个内核。 The input will be of shape (None, 1,20) (a variable number of 1D vectors of size 20). 输入将具有(None, 1,20)形状(None, 1,20)大小为20的可变数量的1D向量)。

input = Input(shape=(1, 20))
conv = Conv1D(filters=5, kernel_size=3, activation=keras.activations.relu, input_shape=(None,20, 1))(input)
dense =dense(1)(conv)
model = Model(inputs=input, outputs=dense)

model.compile(loss=nn.customLoss, optimizer='adam')

history = model.fit(train_X, train_labels, batch_size=50,
                    epochs=15, validation_split=0.2)

the summary of the model is - 该模型的摘要是-

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, None, 20)          0         
_________________________________________________________________
conv1d_1 (Conv1D)            (None, None, 5)           305       
_________________________________________________________________
dense_1 (Dense)              (None, None, 1)           6         
=================================================================
Total params: 311
Trainable params: 311
Non-trainable params: 0

train_x is of shape (None, 1, 20) , train_labels is of shape (None, 1) . train_x的形状为(None, 1, 20) train_labels (None, 1, 20)train_labels的形状为(None, 1)

The error comes from the convolution layer - 错误来自卷积层-

    Caused by op 'conv1d_1/convolution/Conv2D', defined at:
  File "/home/user/Desktop/hack/imlhack2018/conv_nn.py", line 72, in <module>
    main()
  File "/home/user/Desktop/hack/imlhack2018/conv_nn.py", line 42, in main
    conv = Conv1D(filters=5, kernel_size=3, activation=keras.activations.relu, input_shape=(None,20, 1))(input)
  File "/home/user/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 596, in __call__
    output = self.call(inputs, **kwargs)
  File "/home/user/anaconda3/lib/python3.6/site-packages/keras/layers/convolutional.py", line 156, in call
    dilation_rate=self.dilation_rate[0])
  File "/home/user/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 3116, in conv1d
    data_format=tf_data_format)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 670, in convolution
    op=op)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 338, in with_space_to_batch
    return op(input, num_spatial_dims, padding)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 662, in op
    name=name)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 116, in _non_atrous_convolution
    name=scope)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 2010, in conv1d
    data_format=data_format)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 399, in conv2d
    data_format=data_format, name=name)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): computed output size would be negative
     [[Node: conv1d_1/convolution/Conv2D = Conv2D[T=DT_FLOAT, data_format="NHWC", padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/cpu:0"](conv1d_1/convolution/ExpandDims, conv1d_1/convolution/ExpandDims_1)]]

When I add padding="same" to the convolutional layer everything seems to be working just fine. 当我在卷积层中添加padding="same" ,一切似乎都工作正常。 What is the reason for this behavior? 这种行为的原因是什么?

Your input shape is (1, 20), which is interpreted as a 1 width, 20 channel array. 您的输入形状为(1,20),它被解释为1宽度,20通道的数组。 You probably want the opposite, that is, width 20 and 1 channel. 您可能想要相反的选择,即宽度20和1通道。 As your array has a single element, performing convolution without padding of SAME will lead to a negative dimension, which produces the error. 由于数组只有一个元素,因此在不使用SAME填充的情况下执行卷积将导致负数维,从而产生错误。

Note that convolution is always performed on the spatial dimensions, which for Conv1D the second to last dimension in the shape array. 请注意,卷积始终在空间维度上执行,对于Conv1D,该维度是形状数组中倒数第二个维度。 The last dimension represents the channels. 最后一个维度代表渠道。

In the official documents,it writes “When using this layer as the first layer in a model, provide an input_shape argument (tuple of integers or None, does not include the batch axis)”. 在官方文档中,它写道:“当将此层用作模型的第一层时,请提供input_shape参数(整数元组或None,不包括批处理轴)”。 I am confused that why it declared the input shape of conv1D after it had in the Input layer 我很困惑,为什么它在输入层中声明了conv1D的输入形状之后

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM