TensorFlow: Remember LSTM state for next batch (stateful LSTM)

I found out it was easiest to save the whole state for all layers in a placeholder. init_state = np.zeros((num_layers, 2, batch_size, state_size)) … state_placeholder = tf.placeholder(tf.float32, [num_layers, 2, batch_size, state_size]) Then unpack it and create a tuple of LSTMStateTuples before using the native tensorflow RNN Api. l = tf.unpack(state_placeholder, axis=0) rnn_tuple_state = tuple( [tf.nn.rnn_cell.LSTMStateTuple(l[idx][0], … Read more

Many to one and many to many LSTM examples in Keras

So: One-to-one: you could use a Dense layer as you are not processing sequences: model.add(Dense(output_size, input_shape=input_shape)) One-to-many: this option is not supported well as chaining models is not very easy in Keras, so the following version is the easiest one: model.add(RepeatVector(number_of_times, input_shape=input_shape)) model.add(LSTM(output_size, return_sequences=True)) Many-to-one: actually, your code snippet is (almost) an example of this … Read more