Python 如何在tensorflow中正确实现延迟加载?

Python 如何在tensorflow中正确实现延迟加载?,python,tensorflow,Python,Tensorflow,以下代码(尝试在中复制代码结构时) 给出了一个错误。其中一部分内容如下: Caused by op 'Variable_1/read', defined at: File "example.py", line 27, in <module> main() File "example.py", line 23, in main output = sess.run(model.output, {x: 4.0}) File "exam

以下代码(尝试在中复制代码结构时)

给出了一个错误。其中一部分内容如下:

Caused by op 'Variable_1/read', defined at:
    File "example.py", line 27, in <module>
        main()
    File "example.py", line 23, in main
        output = sess.run(model.output, {x: 4.0})
    File "example.py", line 12, in output
        weight = tf.Variable(tf.Variable(tf.constant(4.0)))

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable_1
由op“变量_1/read”引起,定义于:
文件“example.py”,第27行,在
main()
文件“example.py”,第23行,在main中
output=sess.run(model.output,{x:4.0})
输出中第12行的文件“example.py”
权重=tf.变量(tf.变量(tf.常数(4.0)))
FailedPremissionError(回溯见上文):尝试使用未初始化的值变量_1

如何解决该问题?

问题是对
sess.run(tf.global\u variables\u initializer())
的调用发生在变量创建之前,即在下一行对
model.output的第一次调用中

要解决此问题,您必须在调用
sess.run(tf.global\u variables\u initializer())之前以某种方式访问
model.output
。例如,以下代码起作用:

import tensorflow as tf

class Model:

    def __init__(self, x):
        self.x = x
        self._output = None

    @property
    def output(self):
        # NOTE: You must use `if self._output is None` when `self._output` can
        # be a tensor, because `if self._output` on a tensor object will raise
        # an exception.
        if self._output is None:
            weight = tf.Variable(tf.constant(4.0))
            bias = tf.Variable(tf.constant(2.0))
            self._output = tf.multiply(self.x, weight) + bias
        return self._output

def main():
    x = tf.placeholder(tf.float32)
    model = Model(x)

    # The variables are created on this line.
    output_t = model.output

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        output = sess.run(output_t, {x: 4.0})
        print(output)

if __name__ == '__main__':
    main()

问题是对
sess.run(tf.global\u variables\u initializer())
的调用发生在创建变量之前的,在下一行对
model.output的第一次调用中

要解决此问题,您必须在调用
sess.run(tf.global\u variables\u initializer())之前以某种方式访问
model.output
。例如,以下代码起作用:

import tensorflow as tf

class Model:

    def __init__(self, x):
        self.x = x
        self._output = None

    @property
    def output(self):
        # NOTE: You must use `if self._output is None` when `self._output` can
        # be a tensor, because `if self._output` on a tensor object will raise
        # an exception.
        if self._output is None:
            weight = tf.Variable(tf.constant(4.0))
            bias = tf.Variable(tf.constant(2.0))
            self._output = tf.multiply(self.x, weight) + bias
        return self._output

def main():
    x = tf.placeholder(tf.float32)
    model = Model(x)

    # The variables are created on this line.
    output_t = model.output

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        output = sess.run(output_t, {x: 4.0})
        print(output)

if __name__ == '__main__':
    main()

为什么您将
权重定义为
tf.Variable(tf.Variable(tf.constant(4.0))
而不是
tf.Variable(tf.constant(4.0))
?这是一个错误。更正了问题。为什么将
weight
定义为
tf.Variable(tf.Variable(tf.constant(4.0))
而不是
tf.Variable(tf.constant(4.0))
?这是一个错误。更正了问题。