Tensorflow错误:尝试使用未初始化的值multi_rnn_cell

时间:2017-12-25 13:12:16

标签: python tensorflow machine-learning deep-learning recurrent-neural-network

在我的模型文件中,我创建了一个多层-nnn,如下所示:

#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)])

我在另一个函数中调用此单元格:

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)

在我的主文件中,我尝试了sess.run(tf.global_variables_initializer())sess.run(tf.local_variables_initializer()),但却得到了同样的错误:

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"]()]]

我只是想知道为什么我的gru细胞没有初始化。

1 个答案:

答案 0 :(得分:2)

您没有显示完整代码,但我确定您先调用sess.run(tf.global_variables_initializer()) ,然后然后 RNN()方法。这不起作用,因为RNN()正在向图表中添加新节点,并且需要对其进行初始化,就像其他人一样。

解决方案:确保创建完整的计算图,然后才调用初始化程序。