2017-10-18 134 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),如意见提出。

相关问题