Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TensorFlow tf.print with non capital p?

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)
like image 907
CodeMonkey Avatar asked Dec 17 '18 12:12

CodeMonkey


2 Answers

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.

like image 69
jdehesa Avatar answered Sep 24 '22 00:09

jdehesa


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.
like image 35
Tore Rudberg Avatar answered Sep 22 '22 00:09

Tore Rudberg