0
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())

我在 tensorflow 中编写了这段代码并运行它我得到了这个错误

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.

问题是什么?

4

1 回答 1

1

tf.nn.dynamic_rnn参数initial_state应该是完全定义的tensor,而不是占位符。用此行替换init_state将修复错误

...
init_state = lstm_cell.zero_state(batch_size=batch_size, dtype=tf.float32)
...
于 2017-08-27T19:42:25.600 回答