2017-10-18 45 views
2

我希望update_op在我運行summary之前運行。有時我只是創建一個tf.summary,並且一切正常,但有時我想做更多花哨的東西,但仍然具有相同的控制依賴關係。如何將控制依賴關係添加到Tensorflow中運行

不起作用

代碼:

的作品

with tf.control_dependencies([update_op]): 
    if condition: 
     tf.summary.scalar('summary', summary) 
    else: 
     summary += 0 

問題

with tf.control_dependencies([update_op]): 
    if condition: 
     tf.summary.scalar('summary', summary) 
    else: 
     summary = summary 

壞黑客是summary=summary不創建一個新的節點,因此控制依賴被忽略。


我相信有一個更好的方式去做這個,有什麼建議嗎? :-)

+0

'tf.identity(summary)'有效嗎? –

+0

使用'summary = tf.identity(summary)'可行,但它與當前的實現非常相似。我希望有一個更好的解決方案,但它是我擁有的最好的:) –

回答

3

我不認爲有一個更優雅的解決方案,因爲這是設計的行爲。 tf.control_dependencies是使用默認的圖形tf.Graph.control_dependencies通話的快捷方式,這裏是從文檔報價:

注:控件相關性上下文僅適用於在上下文中構建的操作數 。僅在 上下文中使用操作或張量不會添加控件依賴項。下面的例子 說明了這一點:

# WRONG 
def my_func(pred, tensor): 
    t = tf.matmul(tensor, tensor) 
    with tf.control_dependencies([pred]): 
    # The matmul op is created outside the context, so no control 
    # dependency will be added. 
    return t 

# RIGHT 
def my_func(pred, tensor): 
    with tf.control_dependencies([pred]): 
    # The matmul op is created in the context, so a control dependency 
    # will be added. 
    return tf.matmul(tensor, tensor) 

所以只使用tf.identity(summary),如意見提出。

相關問題