简体   繁体   English

如何在tf.nn.dynamic_rnn中为LSTM初始化intitial_state?

[英]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.

相关问题 使用tf.nn.dynamic_rnn使多个隐藏层的LSTM RNN - using tf.nn.dynamic_rnn to make LSTM RNN of multiple hidden layers tf.nn.dynamic_rnn 中的默认 initial_state 是什么 - What's default initial_state in tf.nn.dynamic_rnn tf.nn.dynamic_rnn()“输出”与“状态”的概念理解 - Conceptual understanding of tf.nn.dynamic_rnn() "outputs" vs. "state" Tensorflow:使用tf.mat_fn()或tf.nn.dynamic_rnn()在LSTM之前应用图层有什么区别? - Tensorflow: What's the difference between the use of tf.mat_fn() or tf.nn.dynamic_rnn() to apply layers before an LSTM? seq2seq中的tf.nn.dynamic_rnn形状错误 - tf.nn.dynamic_rnn shape error in seq2seq 如何在张量流存储库中找到“ tf.nn.dynamic_rnn”代码? - How do I finde the Code of “tf.nn.dynamic_rnn” in the tensorflow repository? 从 tf.nn.dynamic_rnn 获取非填充条目的最后一个输出 - get the last output for non-padded entry from tf.nn.dynamic_rnn 升高tf.nn.dynamic_rnn尝试使用未初始化的值错误 - tf.nn.dynamic_rnn raised Attempting to use uninitialized value error tf.nn.dynamic_rnn使用其输入参数进行哪种计算? - What kind of calculation does tf.nn.dynamic_rnn do with its input parameters? 为什么tf.nn.dynamic_rnn返回有关sequence_length的不同结果 - why the tf.nn.dynamic_rnn returns different result regarding sequence_length
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM