【问题标题】:"ValueError: strides should be of length 1, 1 or 3 but was 2" when using ConvLSTM2D使用 ConvLSTM2D 时,“ValueError:步幅的长度应为 1、1 或 3,但为 2”
【发布时间】:2020-12-29 06:26:23
【问题描述】:

我有一个预训练模型,其输出形状为 (20,7,7,256)

我使用tf.keras.layers.Reshape((20,7,7,256)) 将此输出重塑为(None,20,7,7,256),然后将其提供给ConvLSTM2D 层

x = ConvLSTM2D(filters = 256,kernel_size = 3,strides=(1,1),padding='same',
               data_format = 'channels_last',return_state = True,
               kernel_initializer=tf.keras.initializers.he_normal(seed=16))(x)

但每次运行上述代码时都会出现此错误。

~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\ops\nn_ops.py in convolution_internal(input, filters, strides, padding, data_format, dilations, name, call_from_convolution)
    957     channel_index = 1 if data_format.startswith("NC") else n + 1
    958 
--> 959   strides = _get_sequence(strides, n, channel_index, "strides")
    960   dilations = _get_sequence(dilations, n, channel_index, "dilations")
    961 

~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\ops\nn_ops.py in _get_sequence(value, n, channel_index, name)
     73     value = list(value)
     74   else:
---> 75     raise ValueError("{} should be of length 1, {} or {} but was {}".format(
     76         name, n, n + 2, current_n))
     77 

ValueError: strides should be of length 1, 1 or 3 but was 2

是什么导致了我不明白的问题,即使我给了strides = (1,1)??而这个问题的解决方法是什么?

编辑

错误不在此 ConvLSTM2D 层中,而是在我在此层之后添加的下一个 ConvLSTM2D 层中。我在这一层使用了return_state = True,但我的意图是使用return_sequences = True,这导致了下一个ConvLSTM2D层的错误。

将return_state改为return_sequences后的原代码是这样的

x = ConvLSTM2D(filters = 256,kernel_size = 3,
               strides=(1,1),padding='same',return_sequences = True,
               kernel_initializer=tf.keras.initializers.he_normal(seed=16))(x)
x = ConvLSTM2D(filters = 256,kernel_size = 3,
               strides=(1,1),padding='same',return_sequences = False,
               kernel_initializer=tf.keras.initializers.he_normal(seed=16))(x)

【问题讨论】:

    标签: python tensorflow keras tensorflow2.0 tf.keras


    【解决方案1】:

    分辨率由用户指定。为了社区的利益,在答案部分中提及解决方案 -

    错误不在这个ConvLSTM2D层,但在下一个 我在此之后添加的ConvLSTM2D 层。我在这一层使用了return_state = True,但我的意图是使用return_sequences = True 这导致了下一个ConvLSTM2D 层中的错误。

    将return_state更改为后的原始代码是这样的 return_sequences

    x = ConvLSTM2D(filters = 256,kernel_size = 3,
                   strides=(1,1),padding='same',return_sequences = True,
                   kernel_initializer=tf.keras.initializers.he_normal(seed=16))(x)
    x = ConvLSTM2D(filters = 256,kernel_size = 3,
                   strides=(1,1),padding='same',return_sequences = False,
                   kernel_initializer=tf.keras.initializers.he_normal(seed=16))(x)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-25
      • 2020-09-17
      • 1970-01-01
      • 2016-02-17
      • 2021-05-24
      • 2020-08-01
      • 2018-12-13
      • 1970-01-01
      相关资源
      最近更新 更多