I have been using the introductory example of matrix multiplication in TensorFlow.
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
produ
Based on the answers above, with your particular code snippet you can print the product like this:
import tensorflow as tf
#Initialize the session
sess = tf.InteractiveSession()
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
#print the product
print(product.eval())
#close the session to release resources
sess.close()
You can check the output of a TensorObject without running the graph in a session, by enabling eager execution.
Simply add the following two lines of code:
import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()
right after you import tensorflow
.
The output of print product
in your example will now be:
tf.Tensor([[ 12.]], shape=(1, 1), dtype=float32)
Note that as of now (November 2017) you'll have to install a Tensorflow nightly build to enable eager execution. Pre-built wheels can be found here.
I think you need to get some fundamentals right. With the examples above you have created tensors (multi dimensional array). But for tensor flow to really work you have to initiate a "session" and run your "operation" in the session. Notice the word "session" and "operation". You need to know 4 things to work with tensorflow:
Now from what you wrote out you have given the tensor, and the operation but you have no session running nor a graph. Tensor (edges of the graph) flow through graphs and are manipulated by operations (nodes of the graph). There is default graph but you can initiate yours in a session.
When you say print , you only access the shape of the variable or constant you defined.
So you can see what you are missing :
with tf.Session() as sess:
print(sess.run(product))
print (product.eval())
Hope it helps!
The easiest[A] way to evaluate the actual value of a Tensor
object is to pass it to the Session.run()
method, or call Tensor.eval()
when you have a default session (i.e. in a with tf.Session():
block, or see below). In general[B], you cannot print the value of a tensor without running some code in a session.
If you are experimenting with the programming model, and want an easy way to evaluate tensors, the tf.InteractiveSession lets you open a session at the start of your program, and then use that session for all Tensor.eval()
(and Operation.run()
) calls. This can be easier in an interactive setting, such as the shell or an IPython notebook, when it's tedious to pass around a Session
object everywhere. For example, the following works in a Jupyter notebook:
with tf.Session() as sess: print(product.eval())
This might seem silly for such a small expression, but one of the key ideas in Tensorflow 1.x is deferred execution: it's very cheap to build a large and complex expression, and when you want to evaluate it, the back-end (to which you connect with a Session
) is able to schedule its execution more efficiently (e.g. executing independent parts in parallel and using GPUs).
[A]: To print the value of a tensor without returning it to your Python program, you can use the tf.print() operator, as Andrzej suggests in another answer. According to the official documentation:
To make sure the operator runs, users need to pass the produced op to
tf.compat.v1.Session
's run method, or to use the op as a control dependency for executed ops by specifying withtf.compat.v1.control_dependencies([print_op]
), which is printed to standard output.
Also note that:
In Jupyter notebooks and colabs,
tf.print
prints to the notebook cell outputs. It will not write to the notebook kernel's console logs.
[B]: You might be able to use the tf.get_static_value() function to get the constant value of the given tensor if its value is efficiently calculable.
Please note that tf.Print()
will change the tensor name.
If the tensor you seek to print is a placeholder, feeding data to it will fail as the original name will not be found during feeding.
For example:
import tensorflow as tf
tens = tf.placeholder(tf.float32,[None,2],name="placeholder")
print(eval("tens"))
tens = tf.Print(tens,[tens, tf.shape(tens)],summarize=10,message="tens:")
print(eval("tens"))
res = tens + tens
sess = tf.Session()
sess.run(tf.global_variables_initializer())
print(sess.run(res))
Output is:
python test.py
Tensor("placeholder:0", shape=(?, 2), dtype=float32)
Tensor("Print:0", shape=(?, 2), dtype=float32)
Traceback (most recent call last):
[...]
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'placeholder' with dtype float
Reiterating what others said, its not possible to check the values without running the graph.
A simple snippet for anyone looking for an easy example to print values is as below. The code can be executed without any modification in ipython notebook
import tensorflow as tf
#define a variable to hold normal random values
normal_rv = tf.Variable( tf.truncated_normal([2,3],stddev = 0.1))
#initialize the variable
init_op = tf.initialize_all_variables()
#run the graph
with tf.Session() as sess:
sess.run(init_op) #execute init_op
#print the random values that we sample
print (sess.run(normal_rv))
Output:
[[-0.16702934 0.07173464 -0.04512421]
[-0.02265321 0.06509651 -0.01419079]]