【发布时间】:2020-08-30 15:12:05
【问题描述】:
我正在尝试一个 Tensorflow 示例:
import tensorflow as tf
x = tf.Variable(0, name='x')
model = tf.global_variables_initializer()
with tf.Session() as session:
for i in range(5):
session.run(model)
x = x + 1
print(session.run(x))
但是输出是出乎意料的。我预计它会输出:
1 1 1 1 1
但实际输出是:
1 2 3 4 5
这是为什么? session.run(model) 每次都会初始化变量,这个说法正确吗?
【问题讨论】:
-
好问题。取决于实施。对我来说,初始化在会话中只进行一次似乎是有效的。
标签: tensorflow