python-3.x - ValueError:传递了与“cell.state_size”不兼容的“initial_state”
问题描述
我试图在我的模型中添加一个自定义层,但我遇到了这个不合逻辑的错误,我需要改变什么?如果需要自定义层的代码,请告诉我,如果您帮助我了解我做错了什么,这对我来说意味着世界。以及如何解决它?
from keras.models import Sequential , Model
from keras.layers.convolutional_recurrent import ConvLSTM2D
from keras.layers.normalization import BatchNormalization
from keras.layers import Input ,Activation, Dense , Flatten ,RNN
import tensorflow as tf
from layers.se3 import SE3CompositeLayer # this is the layer that i want to add
batch_size = 500
seq = Sequential()
seq.add(ConvLSTM2D(filters=128, kernel_size=(3, 3),
batch_input_shape=(500,4,34, 17, 768),
padding='same', return_sequences=True,stateful=True,dropout=0.2))
seq.add(BatchNormalization())
seq.add(Activation('relu'))
seq.add(ConvLSTM2D(filters=32, kernel_size=(3, 3),padding='same',dropout=0.2))
seq.add(BatchNormalization())
seq.add(Activation('relu'))
output1 = Flatten()(seq.layers[11].output)
branch1= Dense(64,activation ='relu')(output1)
branch1= Dense(4)(branch1)
branch2= Dense(64,activation='relu')(output1)
branch2= Dense(3)(branch2)
output_conv_lstm =tf.concat([branch2,branch1] ,1)
output_conv_lstm = tf.expand_dims(output_conv_lstm , axis=0)
init_s=tf.placeholder_with_default(tf.eye(4, batch_shape=[batch_size]) , shape=(None, 4 ,4))
l_se3comp = SE3CompositeLayer()
se3_outputs, se3_state =keras.layers.RNN(cell=l_se3comp , dtype=tf.float32 ,unroll=True)(output_conv_lstm ,initial_state=init_s)
model = Model(inputs= seq.layers[0].input ,outputs=se3_outputs )
model.summary()
File "main.py", line 59, in <module>
se3_outputs, se3_state =keras.layers.RNN(cell=l_se3comp , dtype=tf.float32 ,unroll=True)(output_conv_lstm ,initial_state=init_s)
File "/home/ridha/.local/lib/python3.6/site-packages/keras/layers/recurrent.py", line 574, in __call__
return super(RNN, self).__call__(inputs, **kwargs)
File "/home/ridha/.local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 431, in __call__
self.build(unpack_singleton(input_shapes))
File "/home/ridha/.local/lib/python3.6/site-packages/keras/layers/recurrent.py", line 508, in build
'{}'.format(self.state_spec, self.cell.state_size))
ValueError: An `initial_state` was passed that is not compatible with `cell.state_size`. Received `state_spec`=[InputSpec(shape=(None, 4, 4), ndim=3)]; however `cell.state_size` is (?, 4, 4) ```
解决方案
不是真正的答案,但可能是一种解决方法:tensorflow 初始化程序据说可以是张量或可调用对象,但我只有每个可调用对象都可以工作。
例如,如果我想将 RNN 中的内核初始化为所有 7 的 2D 向量,
kernel_initializer = tf.constant([7.,7.])
从来没有为我工作过,但是
init_state_getter = lambda shape, dtype: 7*np.ones(*shape)
...
kernel_initializer = init_state_getter
没关系。
推荐阅读
- c# - 有没有最好的方法来实现线程之间的延迟?
- html - 如何滚动溢出的内容?
- spring-boot - 是否可以使用 Shopware REST API 更新订单单个仓位的 statusIds?
- excel - 突出显示/提取包含特定值的行
- mysql - 如何从mysql中的不同关系表中获取多个数据
- c - Clang-format:如何以这种方式格式化 C 结构初始化程序
- java - 多个 RadioGroup 和 Radiobutton 创建问题
- angular - 为什么我的模拟服务属性在 Jasmine 单元测试中未定义?
- ios - 如何使用 OpenTok 在 IOS 应用中实现屏幕共享
- python - 将文本从 .txt 文件发送到电子邮件