How to implement Tensorflow LSTM equivalent of Keras with return_sequence and return_state params?

Viewed 22

In Keras one would usually write:

model = Sequential()
model.add(LSTM(n, input_shape=(ntimesteps, nfeatures), 
    return_sequences=return_sequences, return_state=return_state))

How do you emulate return_sequences and return_state features in pure Tensorflow 2?

1 Answers

Keras is the high-level API of TensorFlow 2, so you would do the same, the only difference is that you do not import keras anymore, but

import tensorflow as tf

print(tf.__version__) # 2.x

model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(n, input_shape=(ntimesteps, nfeatures), 
    return_sequences=return_sequences, return_state=return_state))
Related