Tok*_*rby 3 python machine-learning deep-learning tensorflow
我想update_op先跑再跑summary。有时我只是创建一个tf.summary,一切都很好,但有时我想做更多花哨的东西,但仍然具有相同的控件依赖性。
不起作用的代码:
with tf.control_dependencies([update_op]):
if condition:
tf.summary.scalar('summary', summary)
else:
summary = summary
Run Code Online (Sandbox Code Playgroud)
有效的坏黑客
with tf.control_dependencies([update_op]):
if condition:
tf.summary.scalar('summary', summary)
else:
summary += 0
Run Code Online (Sandbox Code Playgroud)
问题是summary=summary它不会创建新节点,因此忽略了控件依赖项。
我相信有更好的方法来解决这个问题,有什么建议吗?:-)
我认为没有更优雅的解决方案,因为这是设计的行为。tf.control_dependencies是tf.Graph.control_dependencies使用默认图形调用的快捷方式,这是其文档中的引用:
注意控制依赖上下文仅适用于在上下文中构造的操作。仅在上下文中使用 op 或张量不会添加控制依赖项。以下示例说明了这一点:
Run Code Online (Sandbox Code Playgroud)# 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)按照评论中的建议使用。
| 归档时间: |
|
| 查看次数: |
1553 次 |
| 最近记录: |