齿轮Conv1D上的负尺寸尺寸

时间:2018-06-28 06:38:31

标签: python keras convolutional-neural-network

我正在使用Keras的模型api将1D卷积应用于大小为20的输入1d向量。我想要每个大小为3的五个内核。输入的形状为(None, 1,20)(可变数量的大小为20的一维矢量)。

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)

模型的摘要是-

_________________________________________________________________
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的形状为(None, 1, 20)train_labels的形状为(None, 1)

错误来自卷积层-

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

当我将padding="same"添加到卷积层时,一切似乎都工作正常。这种行为的原因是什么?

2 个答案:

答案 0 :(得分:0)

您的输入形状是(1,20),它被解释为1宽度,20通道的数组。您可能想要相反的选择,即宽度20和1通道。由于数组只有一个元素,因此在不使用SAME填充的情况下执行卷积将导致负数维,从而产生错误。

请注意,卷积始终在空间维度上执行,对于Conv1D,这是形状数组中倒数第二个维度。最后一个维度代表渠道。

答案 1 :(得分:0)

在官方文档中,其写道:“当将此层用作模型的第一层时,请提供input_shape参数(整数元组或None,不包括批处理轴)”。我很困惑,为什么它在输入层中声明了conv1D的输入形状