1

Let's consider a simple dataset:

X_train = np.ones((2, 5, 4))

To implement a simple LSTM model and run the dataset through it (without training), I can do the following:

lstm_model = tf.keras.models.Sequential()
lstm_model.add(tf.keras.layers.LSTM(units=16, return_sequences=False, input_shape=X_train.shape[-2:]))

print(lstm_model.predict(X_train))

This yields the same result as:

lstm_model = tf.keras.models.Sequential()
lstm_model.add(tf.keras.layers.RNN(tf.keras.layers.LSTMCell(units=16, input_shape=X_train.shape[-2:])))

print(lstm_model.predict(X_train))

However, I do not understand how to generate the same result using the older version:

g = tf.Graph()

with g.as_default():
    X = tf.compat.v1.placeholder(tf.float32, shape=[None, 5, 4])
    cell = tf.compat.v1.nn.rnn_cell.LSTMCell(16)
    outputs, state = tf.compat.v1.nn.dynamic_rnn(cell, X, dtype=tf.float32)

with tf.compat.v1.Session(graph=g) as sess:
    print(sess.run(state.h, feed_dict={X: X_train}))

Could you show how to modify the last block of code to obtain the same results as in the previous two?

NC520
  • 346
  • 3
  • 13

0 Answers0