How to use TensorFlow tf.print with non capital p?
Both the documentation of tf.print
and tf.Print
mention that tf.print
returns an operation with no output, so it cannot be evaluated to any value. The syntax of tf.print
is meant to be more similar to Python's builtin print
. In your case, you could use it as follows:
def custom_loss(y_true, y_pred):
loss = K.mean(...)
print_op = tf.print("Debug output:", loss, y_true, y_true.shape)
with tf.control_dependencies([print_op]):
return K.identity(loss)
Here K.identity
creates a new tensor identical to loss
but with a control dependency to print_op
, so evaluating it will force executing the printing operation. Note that Keras also offers K.print_tensor
, although it is less flexible than tf.print
.
Just a little addition to jdehesa's excellent answer:
tf.tuple can be used to couple the print operation with another operation, which will then run with that operation whichever session executes the graph. Here's how that is done:
print_op = tf.print(something_you_want_to_print)
some_tensor_list = tf.tuple([some_tensor], control_inputs=[print_op])
# Use some_tensor_list[0] instead of any_tensor below.