如何解决FailedPreconditionError?

时间:2019-09-03 16:15:40

标签: python-3.x python-2.7 tensorflow

我有一个小型的3层网络,在其中我使用initer创建了权重和偏差的变量,并且我使用tf.variables_initializer来初始化某人建议的变量。不幸的是,尽管初始化了它们,但仍然会导致失败的前提条件错误。我现在对如何解决它或到底是什么引起了想法。

with tf.compat.v1.variable_scope("weight_in", reuse=tf.compat.v1.AUTO_REUSE):
            weight_i = tf.compat.v1.get_variable("weight_input", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.weight_initer)
        with tf.compat.v1.variable_scope("weight_hid", reuse=tf.compat.v1.AUTO_REUSE):   
            weight_h = tf.compat.v1.get_variable("weight_hidden", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.weight_initer1)
        with tf.compat.v1.variable_scope("weight_out", reuse=tf.compat.v1.AUTO_REUSE): 
            weight_o = tf.compat.v1.get_variable("weight_input", dtype=tf.float64, shape=[4, 2], initializer=self.weight_initer2)
        with tf.compat.v1.variable_scope("bias_in", reuse=tf.compat.v1.AUTO_REUSE): 
            bias_i = tf.compat.v1.get_variable("bias_input", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.bias_initer)
        with tf.compat.v1.variable_scope("bias_hid", reuse=tf.compat.v1.AUTO_REUSE): 
            bias_h = tf.compat.v1.get_variable("bias_hidden", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.bias_initer1)
        with tf.compat.v1.variable_scope("bias_out", reuse=tf.compat.v1.AUTO_REUSE): 
            bias_o = tf.compat.v1.get_variable("bias_out", dtype=tf.float64, shape=[1, 4], initializer=self.bias_initer2)
       uninitialized_variables = []
        sess = tf.Session()
        print("printing the names of all variables")
        print(tf.all_variables())
        for var in tf.all_variables():
            try:
                sess.run(var)
            except tf.errors.FailedPreconditionError:
                uninitialized_variables.append(var)
        init = tf.variables_initializer(uninitialized_variables)
        sess.run(init)

错误是

FailedPreconditionError: Attewmpting to use initialized value weight_in/weight_input

有时相同的错误将指向另一个偏差变量。我想知道是否有人可以阐明如何正确创建和初始化变量。谢谢。

0 个答案:

没有答案