【发布时间】: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