I try to use tenorflow lstm, but I get the following local variable 'lstm_state' referenced before assignment error. I read from many other threads that I should declare lstm_state as a global variable, but when I see its tensor type, I'm not sure how I can do this. Below is a snippet containing only the relevant section. Hope someone has a suggestion. By the way, I need to use a for loop.
lstm = tf.nn.rnn_cell.LSTMCell(num_units = 1024, state_is_tuple=True) lstm_state = lstm.zero_state(batch_size, tf.float32) def with_input(i): phi_x_out = encoder(x_[:,i,:,:,:]) // Error here phi_prior_out_mu, phi_prior_out_sigma = phi_prior(lstm_state[1]) return [tf.add(i, 1)] def without_input(i): phi_x_out = encoder(y_hat) phi_prior_out_mu, phi_prior_out_sigma = phi_prior(lstm_state[1]) return [tf.add(i, 1)] i = tf.constant(1) while_condition = lambda i : tf.less(i, timesteps_) tf.while_loop(while_condition, with_input, [i]) while_condition = lambda i : tf.less(i, tf.constant(20)) tf.while_loop(while_condition, without_input, [i])
source share