when should i initialize state in LSTM code?

Viewed 610

This is the LSTM code from UDACITY for sentiment classification.

Here is the link of whole sentence-rnn code: udacity/sentiment-rnn

I wonder why they initialize the cell state right under the for loop for epoch.

I think the cell state must be zero-initialize when the input sentence changes, so it must be under the mini-batch for loop statement.

## part of the sentence-rnn code
# Getting an initial state of all zeros
initial_state = cell.zero_state(batch_size, tf.float32)

with tf.Session(graph=graph) as sess:
    sess.run(tf.global_variables_initializer())
    iteration = 1
    for e in range(epochs):
        state = sess.run(initial_state)    ###### i think this line

        for ii, (x, y) in enumerate(get_batches(train_x, train_y, batch_size), 1):
            ###### should be here
            feed = {inputs_: x,
                    labels_: y[:, None],
                    keep_prob: 0.5,
                    initial_state: state}
            loss, state, _ = sess.run([cost, final_state, optimizer], feed_dict=feed)

anyone who can explain why?

Thanks!

1 Answers
Related