2017-05-29 144 views
2

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) 

回答

2

我認爲問題在於最初的代碼創建了由control_flow_ops.with_dependencies隱含創建的虛擬標識操作符之間的依賴關係,而不是實際的tf.Print操作。 Tensorflow似乎只能確保依賴項列表中的操作已經執行,但其他先前操作的順序不固定。在上面的例子中,在創建所述虛擬身份OPS創建由control_flow_ops.with_dependencies的依賴關係:

dependency = control_flow_ops.with_dependencies([dependency], x) 

這應該是等效於:

 with tf.control_dependencies([dependency]): 
      dependency = tf.identity(x) 

因此,依賴這裏是本tf.identity OPS和之間產生而不是tf.Print ops。 tf.Print ops可以以任何順序運行,嚴格的排序僅在tf.identity ops上。我不認爲有可能通過control_flow_ops.with_dependencies達到預期的行爲。相反,人們必須使用with tf.control_dependencies來代替(如已經暗示的那樣):

xs = [tf.constant(x) for x in range(10)] 
dependency = None 
dxs = [] 

for x in xs: 
    if dependency is None: 
     dependency = tf.Print(x, [x]) 
    else: 
     with tf.control_dependencies([dependency]): 
      dependency = tf.Print(x, [x]) 

    dxs.append(dependency) 
3

使用tf.control_dependencies正確不解決這個問題:

import tensorflow as tf 

xs = [tf.constant(x) for x in range(10)] 
dependency = None 
dxs = [] 

for x in xs: 
    if dependency is None: 
     dependency = tf.Print(x, [x]) 
    else: 
     with tf.control_dependencies([dependency]): 
      dependency = tf.Print(x, [x]) 

    dxs.append(dependency) 

print_all_op = tf.group(*dxs) 

with tf.Session() as session: 
    session.run(print_all_op) 

注意,Print操作需要在tf.control_dependencies內創建情境管理器。

我仍然不清楚爲什麼control_flow_ops.with_dependencies版本失敗。