【问题标题】:Attempting to use uninitialized value Variable in Tensorflow ( sess.run(tf.global_variables_initializer()) is used!)尝试在 Tensorflow 中使用未初始化的值变量(使用 sess.run(tf.global_variables_initializer())!)
【发布时间】:2017-01-31 22:25:54
【问题描述】:

我尝试通过将随机权重设置为零来划分我的神经网络模型和 restore() 函数。 这是型号代码:http://pastebin.com/TqN6kkeb (它工作正常)。

函数如下:

from __future__ import print_function

import tensorflow as tf
tf.GraphKeys.VARIABLES = tf.GraphKeys.GLOBAL_VARIABLES
import random
from LogReg import accuracy
from LogReg import W
from LogReg import x,y


# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)


def restore(model_file):

    with tf.Session() as sess:

        new_saver = tf.train.import_meta_graph(model_file + ".meta")
        new_saver.restore(sess, model_file)

        with tf.variable_scope("foo", reuse=True):
            temp_var = tf.get_variable("W")
            size_2a = tf.get_variable("b")

        size_2 = tf.shape(size_2a).eval()[0]
        size_1 = tf.shape(temp_var).eval()[0]

        ones_mask = tf.Variable(tf.ones([size_1, size_2]))
        arg = random.sample(xrange(size_1), size_1/2)
        index_num=tf.convert_to_tensor(arg, dtype=tf.int32)
        print("om", ones_mask)
        print("index", index_num)
        print(W)

        zeroes = tf.zeros([size_1/2, size_2])
        update = tf.scatter_update(ones_mask, index_num, zeroes)
        print(update)

        assign_op = W.assign(tf.mul(W, update))
        sess.run(update)
        sess.run(assign_op)
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        new_saver.save(sess, model_file)
        print("Accuracy_new:", accuracy.eval({x: mnist.test.images, y:mnist.test.labels}))

restore('./MyModel2')

问题是: 1)是它一直在给我写信吗 FailedPreconditionError(参见上面的回溯):尝试在此行中使用未初始化的值变量:

update = tf.scatter_update(ones_mask, index_num, zeroes)

无论如何。我已经阅读了这些主题:Prettytensor: Attempting to use uninitialized valueUpdate a subset of weights in TensorFlow(以及许多其他主题),但是来自那里的建议并没有帮助修复我的错误。 而且我不明白,只要我运行 tf.global_variables_initializer(); 初始化有什么问题;

2) 所有的权重似乎都设置为零而不是一半,我不明白为什么。

请帮忙,我真的卡住了。

【问题讨论】:

  • 您的变量的初始化相互依赖。这是一个已知的问题。请参阅4920 进行讨论和解决方案
  • @YaroslavBulatov,谢谢,这帮助我解决了第一个问题。但是,我还是不太明白为什么所有的权重似乎都是零。
  • 不确定这是否相关,但我之前遇到过一个问题,其中 def: 函数中的变量不受 global_variables_initializer() 的影响,它还有助于调用 local_variable_initializer()。这似乎有帮助...
  • 你能给出一个最小的、独立的例子来展示剩下的问题吗?它越小,我们就越有可能提供帮助。
  • @PeterHawkins,我想在全一矩阵中将随机索引设置为零,然后将其乘以权重矩阵-> 部分权重随机设置为零,这是小代码sn-p:pastebin.com/z1T87AQZ提前致谢。

标签: python python-2.7 machine-learning tensorflow neural-network


【解决方案1】:

仅作记录(以及其他人发现此帖子),方法名称已更改,根据此处的页面:https://www.tensorflow.org/versions/r0.10/how_tos/variables/#initialization

你应该像这样运行initialize_all_variables() 方法:

import tensorflow as tf
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

【讨论】:

猜你喜欢
  • 2016-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
相关资源
最近更新 更多