Keras: stacking multiple LSTM layer with

谁都会走 提交于 2020-06-26 07:24:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!