如何解决使用ConvLSTM2D时,“ ValueError:步幅应为1、1或3,但为2”
我有一个预训练模型,其输出形状为(20,7,256)
我使用(None,20,256)
将输出重塑为tf.keras.layers.Reshape((20,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,"dilations")
961
~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\ops\nn_ops.py in _get_sequence(value,name)
73 value = list(value)
74 else:
---> 75 raise ValueError("{} should be of length 1,{} or {} but was {}".format(
76 name,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,return_sequences = True,kernel_initializer=tf.keras.initializers.he_normal(seed=16))(x)
x = ConvLSTM2D(filters = 256,return_sequences = False,kernel_initializer=tf.keras.initializers.he_normal(seed=16))(x)
解决方法
分辨率由用户指定。在答案部分中提及解决方案是为了社区的利益-
该错误不在此
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,return_sequences = False,kernel_initializer=tf.keras.initializers.he_normal(seed=16))(x)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。