简体   繁体   English

如何在张量流中将rnn的初始状态设置为参数?

[英]How to set initial state of rnn as parameter in tensorflow?

When one uses dynamic_rnn , a parameter called initial_state is required. 当使用dynamic_rnn ,需要一个名为initial_state的参数。 An easy solution is 一个简单的解决方案是

initial_state = lstm_cell.zero_state(batch_size, tf.float32)

But I want to set the initial state as a parameter which can be optimized, how should I do? 但是我想将初始状态设置为可以优化的参数,该怎么办?

I can define two trainable_variables called h0 and c0 , which are two vectors. 我可以定义两个名为h0c0 ,它们是两个向量。 But dynamic_rnn requires two matrixes where the first dimension is batch_size . 但是dynamic_rnn需要两个矩阵,其中第一个维度是batch_size How could I expand the vector h0 to a matrix whose each row is h0 ? 如何将向量h0扩展到每行均为h0的矩阵?

What if you did something like this? 如果您做了这样的事情怎么办?

initial_state_vector = tf.get_variable('initial_state_vector', [1, state_size])
initial_state = tf.tile(initial_state_vector, batch_size)

Then you can supply the initial_state variable to the LSTM and it will be the proper size. 然后,您可以向LSTM提供initial_state变量,它将是适当的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM