Swind D.C. Xu
Swind D.C. Xu

Reputation: 225

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

When one uses dynamic_rnn, a parameter called initial_state is required. 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. But dynamic_rnn requires two matrixes where the first dimension is batch_size. How could I expand the vector h0 to a matrix whose each row is h0?

Upvotes: 4

Views: 1958

Answers (1)

Aaron
Aaron

Reputation: 2364

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.

Upvotes: 3

Related Questions