简体   繁体   English

为什么类对象中的 tf.Variable 初始化失败?

[英]Why does tf.Variable inside a class object fail to be initialized?

The way tf.global_variables_initializer() works makes me confused. tf.global_variables_initializer() 的工作方式让我很困惑。

This code ends up with uninitialized error:此代码以未初始化的错误结束:

import tensorflow as tf

class C(object):
    def __init__(self):
        self.a = tf.Variable(tf.ones(()),tf.float32)

op_init = tf.global_variables_initializer()                                                                                                                                       12
with tf.Session() as sess:

    c = C()

    sess.run(op_init)
    print(sess.run(c.a))
    # -> fail. FailedPreconditionError: Attempting to use uninitialized value Variable

But on the other hands, the bellow works.但另一方面,波纹管工作。

import tensorflow as tf
class C(object):

    def __init__(self):
        self.a = tf.Variable(tf.ones(()),tf.float32)
        self.op_init = tf.global_variables_initializer()

with tf.Session() as sess:

    c = C()

    sess.run(c.op_init)
    print(sess.run(c.a))
    # -> success. '1.0'

The other answer is incorrect with regards to the "scope" of the initializer.关于初始化程序的“范围”,另一个答案是不正确的。 All variables created anywhere are added to the collection of global variables which is a Tensorflow concept and has nothing to do with Python variable scope.在任何地方创建的所有变量都添加到全局变量集合中,这是一个 Tensorflow 概念,与 Python 变量作用域无关。 Unfortunately I don't have TF 1.X installed anymore so I can't really check, but I suspect that the problem in the first case is that you are creating the initializer before creating the variable.不幸的是,我不再安装 TF 1.X,所以我无法真正检查,但我怀疑第一种情况的问题是您在创建变量之前创建了初始值设定项。 Try reordering like this:尝试像这样重新排序:

import tensorflow as tf

class C(object):
    def __init__(self):
        self.a = tf.Variable(tf.ones(()),tf.float32)

c = C()
op_init = tf.global_variables_initializer()                                                                                                                                       12
with tf.Session() as sess:
    sess.run(op_init)
    print(sess.run(c.a))

All I did was move the creation of c before the initializer.我所做的只是在初始化之前移动c的创建。 Please let us know if this works!请让我们知道这是否有效!

The Tensor is not initialized because it is not in the scope of the initializer function. Tensor 没有被初始化,因为它不在初始化函数的范围内。 The second way works because it is in the scope.第二种方式有效,因为它在范围内。 tf.global_variables_initializer() is a convenience for the user but it is not the one and only way you should initialize your Variables. tf.global_variables_initializer()为用户提供了便利,但它不是您初始化变量的唯一方式。

The correct way in the first option would be to pass an initial value as a fed input:第一个选项中的正确方法是将初始值作为馈送输入传递:

print(sess.run(c.a, feed_dict={c.a:1}))

This is generally how you want to use a tf.Variable as its contents can vary, meaning that it is probably initialized by the user during every run.这通常是您想要使用 tf.Variable 的方式,因为它的内容可能会有所不同,这意味着它可能在每次运行期间由用户初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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