[英]How to initialize intitial_state for LSTM in tf.nn.dynamic_rnn?
I am not sure how to pass a value for initial_state when the cell is a LSTMCell. 我不确定当单元格为LSTMCell时如何传递initial_state的值。 I am using LSTMStateTuple as it is shown in the following piece of code: 我正在使用LSTMStateTuple,如以下代码所示:
c_placeholder = tf.placeholder(tf.float32, [ None, config.state_dim], name='c_lstm')
h_placeholder = tf.placeholder(tf.float32, [ None, config.state_dim], name='h_lstm')
state_tuple = tf.nn.rnn_cell.LSTMStateTuple(c_placeholder, h_placeholder)
cell = tf.contrib.rnn.LSTMCell(num_units=config.state_dim, state_is_tuple=True, reuse=not is_training)
rnn_outs, states = tf.nn.dynamic_rnn(cell=cell, inputs=x,sequence_length=seqlen, initial_state=state_tuple, dtype= tf.float32)
However, the execution returns this error: 但是,执行返回此错误:
TypeError: 'Tensor' object is not iterable.
Here is the link of the documentation for dynamic_rnn 这是dynamic_rnn文档的链接
I've seen this same error before. 我以前见过同样的错误。 I was using multiple layers of RNN-cells made with tf.contrib.rnn.MultiRNNCell
, and I needed to specify a tuple of LSTMStateTuples
-- one for each layer. 我正在使用由tf.contrib.rnn.MultiRNNCell
制成的多层RNN单元,我需要指定一个LSTMStateTuples
元组- LSTMStateTuples
一个。 Something like 就像是
state = tuple(
[tf.nn.rnn_cell.LSTMStateTuple(c_ph[i], h_ph[i])
for i in range(nRecurrentLayers)]
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.