我正在尝试定义双向层的初始状态,我无法弄清楚如何为该层定义初始状态。我可以为简单的lstm RNN定义初始状态,但是当我将层更改为双向时,我不知道如何定义初始状态。
这是一个简单的例子
h_in = K.reshape(tf.constant([2.]),(1,1))
c_in = K.reshape(tf.constant([3.]),(1,1))
lstm = Bidirectional(LSTM(1, return_sequences=True),merge_mode='concat')
l_lstm = lstm(sequence_input , initial_state= [h_in, c_in, h_in, c_in])
l1 = Dense(1, activation = 'linear')(l_lstm)
model = Model(inputs = [sequence_input], outputs = l1)
它返回以下错误:
-
TypeError Traceback (most recent call last)
<ipython-input-93-d5838abcb4be> in <module>
11
12 lstm = Bidirectional(LSTM(1, return_sequences=True),merge_mode='concat')
---> 13 l_lstm = lstm(sequence_input , initial_state=[h_in, c_in, h_in, c_in])
14 # ( sequence_input , initial_state= init_state )
15 # l0 = Dense(40, activation='softplus')(l_lstm)
~\Anaconda3\lib\site-packages\keras\layers\wrappers.py in __call__(self, inputs, initial_state, constants, **kwargs)
481 return output
482 else:
--> 483 return super(Bidirectional, self).__call__(inputs, **kwargs)
484
485 def call(self,
~\Anaconda3\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)
455 # Actually call the layer,
456 # collecting output(s), mask(s), and shape(s).
--> 457 output = self.call(inputs, **kwargs)
458 output_mask = self.compute_mask(inputs, previous_mask)
459
~\Anaconda3\lib\site-packages\keras\layers\wrappers.py in call(self, inputs, mask, training, initial_state, constants)
516 backward_inputs += inputs[-self._num_constants:]
517 y = self.forward_layer.call(forward_inputs,
--> 518 initial_state=forward_state, **kwargs)
519 y_rev = self.backward_layer.call(backward_inputs,
520 initial_state=backward_state, **kwargs)
~\Anaconda3\lib\site-packages\keras\layers\recurrent.py in call(self, inputs, mask, training, initial_state)
2192 mask=mask,
2193 training=training,
-> 2194 initial_state=initial_state)
2195
2196 @property
~\Anaconda3\lib\site-packages\keras\layers\recurrent.py in call(self, inputs, mask, training, initial_state, constants)
603 mask = mask[0]
604
--> 605 if len(initial_state) != len(self.states):
606 raise ValueError('Layer has ' + str(len(self.states)) +
607 ' states but was passed ' +
TypeError: object of type 'Tensor' has no len()