问题
I have the following network which works fine:
output = LSTM(8)(output)
output = Dense(2)(output)
Now for the same model, I am trying to stack a few LSTM layer like below:
output = LSTM(8)(output, return_sequences=True)
output = LSTM(8)(output)
output = Dense(2)(output)
But I got the following errors:
TypeError Traceback (most recent call last)
<ipython-input-2-0d0ced2c7417> in <module>()
39
40 output = Concatenate(axis=2)([leftOutput,rightOutput])
---> 41 output = LSTM(8)(output, return_sequences=True)
42 output = LSTM(8)(output)
43 output = Dense(2)(output)
/usr/local/lib/python3.4/dist-packages/keras/layers/recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)
480
481 if initial_state is None and constants is None:
--> 482 return super(RNN, self).__call__(inputs, **kwargs)
483
484 # If any of `initial_state` or `constants` are specified and are Keras
/usr/local/lib/python3.4/dist-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
601
602 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 603 output = self.call(inputs, **kwargs)
604 output_mask = self.compute_mask(inputs, previous_mask)
605
TypeError: call() got an unexpected keyword argument 'return_sequences'
This is confusing because return_sequences is a valid argument based on the Keras document : https://keras.io/layers/recurrent/#lstm
What did I do wrong here? Thanks!
回答1:
The problem lied in the fact that return_sequences
should be passed as an argument to layer constructor - not layer call. Changing the code to:
output = LSTM(8, return_sequences=True)(output)
solved the problem.
来源:https://stackoverflow.com/questions/48510318/keras-stacking-multiple-lstm-layer-with