我正在尝试向模型添加自定义图层,但是出现此illogic错误,我需要更改什么? 如果需要自定义层的代码,请告诉我 如果您帮助我了解我做错了什么以及如何解决它,这对我来说将是一个世界。
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) ```
答案 0 :(得分:0)
这并不是一个真正的答案,但可能是一种解决方法: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
还可以。