TypeError in arguments of dynamic_rnn function in tensorflow

82 Views Asked by At
hidden_size = 1
batch_size = 1
seq_len = 3
feature_dim = 1
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=hidden_size)
init_state = tf.placeholder_with_default(
    lstm_cell.zero_state(batch_size=batch_size, dtype=tf.float32), shape = [2, batch_size, hidden_size])
l = tf.Variable([seq_len])
v = tf.Variable(tf.random_normal(shape=[batch_size, seq_len, feature_dim], mean = 0, stddev = 0.01), name = 'v', trainable=True, dtype=tf.float32)
otuput, out_state = tf.nn.dynamic_rnn(lstm_cell, v, [seq_len], initial_state= init_state)
with tf.Session() as ses:
    ses.run(tf.global_variables_initializer())

I wrote this code in tensorflow and with running it i am getting this Error

TypeError                                 Traceback (most recent call last)
<ipython-input-55-f105d2bb8ade> in <module>()
      8 l = tf.Variable([seq_len])
      9 v = tf.Variable(tf.random_normal(shape=[batch_size, seq_len, feature_dim], mean = 0, stddev = 0.01), name = 'v', trainable=True, dtype=tf.float32)
---> 10 otuput, out_state = tf.nn.dynamic_rnn(lstm_cell, v, [seq_len], initial_state= init_state)
     11 with tf.Session() as ses:
     12     ses.run(tf.global_variables_initializer())
.
.
.
TypeError: 'Tensor' object is not iterable.

What is the problem?

1

There are 1 best solutions below

0
On BEST ANSWER

tf.nn.dynamic_rnn argument initial_state should be a fully defined tensor, not placeholder. Replacing init_state with this line would fix the errors

...
init_state = lstm_cell.zero_state(batch_size=batch_size, dtype=tf.float32)
...