Keras 负维度大小 Conv2D

我一直在玩内核大小和通道安排一段时间,但没有运气。我不完全确定如何计算 Conv2D 层的校正器参数,我不确定这些参数的更改会在多大程度上影响与论文中模型的相似性。


任何帮助将不胜感激。


我尝试根据文献中的设计构建的模型


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

我收到的错误消息


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



拉莫斯之舞
浏览 125回答 2
2回答

狐的传说

另一个答案在诊断中是正确的:卷积后,您的图像会缩小,并且在某些时候内核变得比图像大。尝试1)降低内核大小或2)添加, padding='same'到你的卷积层。使用计算卷积层中的输出大小来计算输出大小。

不负相思意

你有这个错误是因为你的内核和步幅对于你的输入来说太大了,一个常见的开始是使用形状(3, 3)和步幅的内核(1, 1)。尝试阅读如何计算卷积,让您直观了解如何设置正确的内核/步幅大小:http ://cs231n.github.io/convolutional-networks/此外,您有一个输入channel first,因此您将第一个 conv 设置为channel first,这很好,但是您对所有卷积都执行此操作,因为默认情况下 keras 卷积将使用channel last.例如,这是有效的: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'))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python