如何正确初始化tensorflow中的所有变量?

时间:2019-08-28 09:21:12

标签: python tensorflow

我有一个简单的神经网络类,其中有创建重复使用权重变量的成员方法。在运行成员方法中,我正在调用这些函数来创建权重变量,然后使用tf.global_variables_initializer````to initialize all those weight and bias variables. But when I run this program, I am getting an error. When I run it multiple times, the error varies, sometimes its about failedpre条件错误将其初始化:尝试使用未初始化的变量,``有时错误发生在后来使用我初始化的变量时权重以计算神经元的输入。谁能帮助我解决这个问题怎么办?

我尝试使用tf.Session()创建一个对象,并使用该对象运行初始化程序对象。

def run(self, carollis_input):
        c_in=normalize(carollis_input, axis = 0)
        print('normalized carollis input is \n')
        print(c_in)
        c_in = np.array(c_in)
        c_input = tf.compat.v1.convert_to_tensor(c_in, tf.float64)
        #'finding the output of the input layer'
        weight_i = self.input_weight()
        weight_h = self.hid_weight()
        weight_o = self.out_weight()
        bias_i = self.bias_in()
        bias_h = self.bias_hid()
        bias_o = self.bias_out()
        init = tf.global_variables_initializer()
        sess = tf.compat.v1.Session()
        sess.run(init)
        knowledge_input = tf.add(tf.multiply(c_input, weight_i), bias_i)
        sess.run(knowledge_input)
        knowledge_hidden = tf.nn.leaky_relu(knowledge_input, alpha=0.01) 
        #'calculating the output of hidden layer'
        knowledge_hidden_output = 3.14*(tf.add(tf.multiply(knowledge_hidden, weight_h), bias_h))#input function of hidden layer
        knowledge_hidden_out = tf.nn.leaky_relu(knowledge_hidden_output, alpha=0.01, name='leaky_relu')

        sess.run(knowledge_hidden_out)
        #'calculating the input of output layer'
        knowledge_hidden_out = tf.reshape(knowledge_hidden_out, [4, 2])#for quadrant method
        in_out = tf.add(tf.multiply(knowledge_hidden_out, weight_o), bias_o)
        sess.run(in_out)
        #'finding the softmax output of the neurons'
        softmax_output = np.array(4)
        softmax_output = self.out_softmax(in_out)  # this gives the softmax output and stores it in the newly created array
        return softmax_output

初始化变量的功能如下:

def input_weight(self):
        with tf.compat.v1.variable_scope("weight_in", reuse=tf.compat.v1.AUTO_REUSE):
            v = tf.compat.v1.get_variable("weight_input", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.weight_initer)
        return v

错误有时是,

[ERROR] [1566983446.912849, 15.130000]: bad callback: <function joint_callback at 0x7f0722d10510>
Traceback (most recent call last):
  File "/home/microbot/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1356, in _do_call
    return fn(*args)
  File "/home/microbot/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1341, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/home/microbot/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1429, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value bias_in/bias_input
     [[{{node bias_in/bias_input/read}}]]

0 个答案:

没有答案