【发布时间】:2018-09-01 20:41:14
【问题描述】:
我有非常简单的行会产生非常奇怪的意外行为:
import tensorflow as tf
y = tf.Variable(2, dtype=tf.int32)
a1 = tf.assign(y, y + 1)
a2 = tf.assign(y, y * 2)
with tf.control_dependencies([a1, a2]):
t = y+0
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(4):
print('t=%d' % sess.run(t))
print('y=%d' % sess.run(y))
预期是
t=6
y=6
t=14
y=14
t=30
y=30
t=62
y=62
但第一次运行,我得到了:
t=6
y=6
t=13
y=13
t=26
y=26
t=27
y=27
第二次运行,我得到了:
t=3
y=3
t=6
y=6
t=14
y=14
t=15
y=15
第三轮,我得到了:
t=6
y=6
t=14
y=14
t=28
y=28
t=56
y=56
很可笑,多次运行产生多个不同的输出序列,很奇怪,有人能帮忙吗?
编辑:更改为
import tensorflow as tf
import os
y = tf.Variable(2, dtype=tf.int32)
a1 = tf.assign(y, y + 1)
a2 = tf.assign(y, y * 2)
a3 = tf.group(a1, a2)
with tf.control_dependencies([a3]):
t = tf.identity(y+0)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(4):
print('t=%d' % sess.run(t))
print('y=%d' % sess.run(y))
...仍然无法正常工作。
这段代码还是很奇怪:
a1 = tf.assign(y, y + 1)
with tf.control_dependencies([a1]):
a2 = tf.assign(y, y * 2)
with tf.control_dependencies([a2]):
t = tf.identity(y)
... 可以正常工作,但只需将a2 移到之前作为
a1 = tf.assign(y, y + 1)
a2 = tf.assign(y, y * 2)
with tf.control_dependencies([a1]):
with tf.control_dependencies([a2]):
t = tf.identity(y)
...它没有。
【问题讨论】:
标签: python tensorflow machine-learning dependencies control-flow