繼earlier question之後,好像tf.group
確實忽略了依賴關係。這裏有一個簡單的獨立實例(我已經在Python 2.7版與TensorFlow 1.1運行它):TensorFlow tf.group忽略依賴關係?
import tensorflow as tf
from tensorflow.python.ops import control_flow_ops
xs = [tf.constant(x) for x in range(10)]
xs = [tf.Print(x, [x]) for x in xs]
dependency = None
dxs = []
for x in xs:
if dependency is None:
dependency = x
else:
dependency = control_flow_ops.with_dependencies([dependency], x)
dxs.append(dependency)
print_all_op = tf.group(*dxs)
with tf.Session() as session:
session.run(print_all_op)
預期輸出:
2017-05-29 15:11:53.961221: I tensorflow/core/kernels/logging_ops.cc:79] [0]
2017-05-29 15:11:53.961236: I tensorflow/core/kernels/logging_ops.cc:79] [1]
2017-05-29 15:11:53.961255: I tensorflow/core/kernels/logging_ops.cc:79] [2]
2017-05-29 15:11:53.961237: I tensorflow/core/kernels/logging_ops.cc:79] [3]
2017-05-29 15:11:53.961262: I tensorflow/core/kernels/logging_ops.cc:79] [4]
2017-05-29 15:11:53.961263: I tensorflow/core/kernels/logging_ops.cc:79] [5]
2017-05-29 15:11:53.961268: I tensorflow/core/kernels/logging_ops.cc:79] [6]
2017-05-29 15:11:53.961272: I tensorflow/core/kernels/logging_ops.cc:79] [7]
2017-05-29 15:11:53.961274: I tensorflow/core/kernels/logging_ops.cc:79] [8]
2017-05-29 15:11:53.961221: I tensorflow/core/kernels/logging_ops.cc:79] [9]
實際輸出(每次不同的代碼運行):
2017-05-29 15:16:26.279655: I tensorflow/core/kernels/logging_ops.cc:79] [0]
2017-05-29 15:16:26.279655: I tensorflow/core/kernels/logging_ops.cc:79] [9]
2017-05-29 15:16:26.279697: I tensorflow/core/kernels/logging_ops.cc:79] [3]
2017-05-29 15:16:26.279660: I tensorflow/core/kernels/logging_ops.cc:79] [1]
2017-05-29 15:16:26.279711: I tensorflow/core/kernels/logging_ops.cc:79] [8]
2017-05-29 15:16:26.279713: I tensorflow/core/kernels/logging_ops.cc:79] [4]
2017-05-29 15:16:26.279723: I tensorflow/core/kernels/logging_ops.cc:79] [5]
2017-05-29 15:16:26.279663: I tensorflow/core/kernels/logging_ops.cc:79] [2]
2017-05-29 15:16:26.279724: I tensorflow/core/kernels/logging_ops.cc:79] [7]
2017-05-29 15:16:26.279728: I tensorflow/core/kernels/logging_ops.cc:79] [6]
tf.group
文檔中沒有任何內容可以指示忽略依賴關係的原因。
是否有替代tf.group
的確考慮了依賴關係?
切換到使用tf.control_dependencies
而不是tensorflow.python.ops.control_flow_ops.with_dependencies
沒有幫助:
import tensorflow as tf
xs = [tf.constant(x) for x in range(10)]
xs = [tf.Print(x, [x]) for x in xs]
dependency = None
dxs = []
for x in xs:
if dependency is None:
dependency = x
else:
with tf.control_dependencies([dependency]):
dependency = x
dxs.append(dependency)
print_all_op = tf.group(*dxs)
with tf.Session() as session:
session.run(print_all_op)