简体   繁体   中英

Tensorflow error: Attempting to use uninitialized value multi_rnn_cell

In my model file, I create a multi-layer-rnn, like this:

#RNN initialization part
cell = tf.contrib.rnn.GRUCell(self.global_dim, kernel_initializer=self.xavier_initializer)   
self.GRU = tf.contrib.rnn.MultiRNNCell([cell for _ in range(self.rnn_layers)])

I call this cell in another function:

def RNN(self):
    state = self.initRNNState()
    inputs = tf.reshape(self.itemVec, [self.num_steps, self.batch_size, self.global_dim])
    hiddenState = []

    for time_step in range(self.num_steps):
        _, state = self.GRU(inputs[time_step], state)
        hiddenState.append(tf.reshape(state[-1], [self.global_dim])) #Store last layer

    return tf.convert_to_tensor(hiddenState)

While in my main file, I tried both sess.run(tf.global_variables_initializer()) and sess.run(tf.local_variables_initializer()) , but got the same eror:

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value multi_rnn_cell/cell_0/gru_cell/gates/kernel
     [[Node: multi_rnn_cell/cell_0/gru_cell/gates/kernel/read = Identity[T=DT_FLOAT, _class=["loc:@multi_rnn_cell/cell_0/gru_cell/gates/kernel"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](multi_rnn_cell/cell_0/gru_cell/gates/kernel)]]
     [[Node: Neg/_11 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_1304_Neg", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

I just wonder why my gru cell not initialized.

You didn't show the full code, but I'm sure that you're calling sess.run(tf.global_variables_initializer()) first , and then RNN() method. This won't work because RNN() is adding new nodes to the graph and they need to be initialized, just like others.

Solution: make sure you create the full computational graph and only then call an initializer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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