[英]How to add control dependency to Tensorflow op
我希望update_op
在運行summary
之前運行。 有時我只是創建一個tf.summary
,並且一切正常,但是有時我想做更多花哨的事情,但仍然具有相同的控件依賴性。
無效的代碼:
with tf.control_dependencies([update_op]):
if condition:
tf.summary.scalar('summary', summary)
else:
summary = summary
無效的hack
with tf.control_dependencies([update_op]):
if condition:
tf.summary.scalar('summary', summary)
else:
summary += 0
問題是summary=summary
不會創建新節點,因此控件依賴項將被忽略。
我敢肯定,有解決此問題的更好方法,有什么建議嗎? :-)
我認為對此沒有更優雅的解決方案,因為這是設計行為。 tf.control_dependencies
是使用默認圖形的tf.Graph.control_dependencies
調用的快捷方式, tf.control_dependencies
是其文檔中的引文:
注意控件依賴項上下文僅適用於在上下文中構造的操作。 僅在上下文中使用op或張量不會添加控件依賴項。 以下示例說明了這一點:
# 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)
,如注釋中所建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.