- We pass the last hidden state only in blue LSTM as the initial hidden state. Or is this the last hidden state and memory of the cell.
The latent state h and the memory of cell c are transmitted to the decoder.
Tensorflow
In the source code of seq2seq , you can find the following code in basic_rnn_seq2seq() :
_, enc_state = rnn.static_rnn(enc_cell, encoder_inputs, dtype=dtype) return rnn_decoder(decoder_inputs, enc_state, cell)
If you use LSTMCell , the returned enc_state from the encoder will be a tuple (c, h) . As you can see, the tuple is passed directly to the decoder.
Keras
In Keras, the “state” defined for LSTMCell is also a tuple (h, c) (note that the order is different from TF). In LSTMCell.call() you can find:
h_tm1 = states[0] c_tm1 = states[1]
To get the states returned from the LSTM level, you can specify return_state=True . The return value is a tuple (o, h, c) . The tensor o is the result of this layer, which will be equal to h unless you specify return_sequences=True .
- Is there a way to set the initial hidden state and cell memory in Keras or Tensorflow? If this is a link?
Tensorflow
Just specify the initial state of LSTMCell when invoked. For example, in the official RNN tutorial :
lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size) ... output, state = lstm(current_batch_of_words, state)
There is also an initial_state argument for functions such as tf.nn.static_rnn . If you are using the seq2seq module, specify the status of rnn_decoder , as shown in the code for question 1.
Keras
Use the initial_state keyword argument in the LSTM function call.
out = LSTM(32)(input_tensor, initial_state=(h, c))
In fact, you can find this use on the official documentation :
Note on setting the initial state of RNN
You can indicate the initial state of the RNN layers symbolically by calling them the keyword argument initial_state . The value of initial_state should be a tensor or a list of tensors representing the initial state of the RNN layer.
EDIT:
Now there is an example script in Keras ( lstm_seq2seq.py ) showing how to implement basic seq2seq in Keras. How to make a forecast after training, the seq2seq model is also discussed in this script.