Reputation: 5968
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? :-)
Upvotes: 3
Views: 1798
Reputation: 53758
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:
N.B. 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.
Upvotes: 5