Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a stateful LSTM as a functional model?

I have a stateful LSTM defined as a Sequential model:

model = Sequential()
model.add(LSTM(..., stateful=True))
...

Later, I use it as a Functional model:

input_1, input_2 = Input(...), Input(...)
output_1 = model(input_1)
output_2 = model(input_2)  # Is the state from input_1 preserved?

Is the state from input_1 preserved when we apply model again on input_2? If yes, how can I reset the model state in between the calls?

like image 936
kennysong Avatar asked Jan 20 '17 08:01

kennysong


People also ask

What is a stateful LSTM?

Stateful flag is Keras. All the RNN or LSTM models are stateful in theory. These models are meant to remember the entire sequence for prediction or classification tasks. However, in practice, you need to create a batch to train a model with backprogation algorithm, and the gradient can't backpropagate between batches.

What is stateless LSTM?

Overview. For this example, we will be using a Stateless LSTM, which means the network's memory is reset for each batch. In this specific example, we will use single batches of audio data. Because we are using single batches, the LSTM essentially becomes a feed-forward network because there is no recurrent state.


1 Answers

Following Note on using statefulness in RNNs from this link and Keras implementation the answer is yes if:

  1. The batch_size in both models is the same (it's important due to the way Keras computes the inner states).
  2. You would first build and compile both models and then use them - for some reason Keras is resetting the inner states during the build of a layer (you can check it here by looking for reset_states method).

If you want to reset states you could call reset_states method on each recurrent layer you want ot reset states on.

like image 130
Marcin Możejko Avatar answered Sep 22 '22 12:09

Marcin Możejko