Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TensorFlow's Print is not printing

I am trying to understand some code from a reinforcement learning algorithm. In order to do that I am trying to print the value of a tensor.

I made a simple piece of code to show what I mean.

import tensorflow as tf
from keras import backend as K

x = K.abs(-2.0)
tf.Print(x,[x], 'x')

The goal is to have the value '2' printed(the absolute value of -2). But I only get back the following:

Using TensorFlow backend.

Process finished with exit code 0

Nothing, how can I print the value '2' just like a print('...') statement would do?

like image 942
JorDik Avatar asked Oct 10 '18 07:10

JorDik


1 Answers

If you are using Jupyter Notebook, then tf.Print() so far isn't compatible and would be printing the output to Notebook's server output as is described in the docs

In the tensorflow documentation, here is how Tensors are described:

When writing a TensorFlow program, the main object you manipulate and pass around is the tf.Tensor. A tf.Tensor object represents a partially defined computation that will eventually produce a value.

Hence, you would have to initialize them with a tf.Session() to get their value. To print the value, you eval()

Here is the code you want:

import tensorflow as tf
from keras import backend as K

x= K.abs(-2.0)
with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    print(x.eval())

The initializer is important to actually initialize x.

like image 163
Saket Kumar Singh Avatar answered Oct 11 '22 11:10

Saket Kumar Singh