Keras: How to get the "h" and "c" tensors from an LSTM layer?

Created on 13 Feb 2018  路  4Comments  路  Source: keras-team/keras

Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on StackOverflow or join the Keras Slack channel and ask there instead of filing a GitHub issue.

Thank you!

  • [ ] Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps

  • [x] If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.

  • [x] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
    pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps

  • [ ] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

Most helpful comment

@rfernand2 Assume you using TensorFlow backend (import keras.backend as K), "h" and "c" can be accessed by [K.get_value(state) for state in layer.states].

All 4 comments

I have a encoder-decoder app that I am trying to make more modular. One of the scenarios is inference on the decoder (feed in a vector and get out a sequence of tokens).

I'm trying to load the trained model and build an inference decoder model based on the training decoder sub-model, just adding the "h" and "c" states from the first LSTM in the decoder sub-model to the output list of the inference model.

I call training_model.get_layer(lstm_name) - that works great. Now, how can I get access to the "h" and "c" tensors for that layer? Thanks in advance.

@rfernand2 Assume you using TensorFlow backend (import keras.backend as K), "h" and "c" can be accessed by [K.get_value(state) for state in layer.states].

Thanks - I tried the above - looks like in my case the states are all "None" values. This seems like the wrong API since it returns a numpy array. I am trying to get the "h" and "c" symbolic tensors from the layer - the same values that are returned when you create an LSTM layer with "return_values=True" and then call it with an input tensor argument.

I think I just found what I wanted - looking at the layer.output in the debugger I can see it is 3 tensors. I believe these are the output, h, and c tensors.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anjishnu picture anjishnu  路  3Comments

snakeztc picture snakeztc  路  3Comments

harishkrishnav picture harishkrishnav  路  3Comments

kylemcdonald picture kylemcdonald  路  3Comments

zygmuntz picture zygmuntz  路  3Comments