简体   繁体   中英

How to add control dependency to Tensorflow op

I want update_op to run before I run summary . Sometimes I just create a tf.summary , and everything works just fine, but sometimes I want to do more fancy stuff, but still have the same control dependency.

Code that doesn't work:

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

Bad hack that works

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

The problem is that summary=summary doesn't create a new node, so the control dependency is ignored.


I am sure that there is a way better way of going about this, any suggestions? :-)

I don't think there exists a more elegant solution to this, because this the designed behavior. tf.control_dependencies is a shortcut of tf.Graph.control_dependencies call using a default graph, and here's the quote from its documentation:

NB The control dependencies context applies only to ops that are constructed within the context. Merely using an op or tensor in the context does not add a control dependency. The following example illustrates this point:

 # 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) 

So just use tf.identity(summary) , as suggested in the comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM