I have some TensorFlow code in a custom loss function.
I'm using tf.Print(node, [debug1, debug2], "print my debugs: ")
It works fine but TF says tf.Print
is depricated and will be removed once i update TensorFlow and that i should be using tf.**p**rint()
, with small p.
I've tried using tf.print
the same way i would tf.Print()
but it's not working. Once i fit my model in Keras, i get an error. unlike tf.Print
, tf.print
seems to take in anything **kwargs
, so what am i suppose to give it? and unlike tf.Print
it do not seem to return something that i can inject into the computational graph.
It's really difficult to search because all the information online is about tf.Print()
.
Can someone explain how to use tf.print()
?
Edit: Example code
def custom_loss(y_true, y_pred):
loss = K.mean(...)
print_no_op = tf.Print(loss, [loss, y_true, y_true.shape], "Debug output: ")
return print_no_op
model.compile(loss=custom_loss)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With