The native Python print()
statement will be called when the graph is build the first time. Check this out:
a = tf.placeholder(shape = None, dtype = tf.int32)
b = tf.placeholder(shape = None, dtype = tf.int32)
print("a is ", a, " while b is ", b)
c = tf.add(a, b)
with tf.Session() as sess:
print(sess.run(c, feed_dict = {
a: 1,
b: 2
}))
print(sess.run(c, feed_dict = {
a: 3,
b: 1
}))
By exectuing this code block, the output is:
# a is Tensor("Placeholder:0", dtype = int32) while b is Tensor("Placeholder_1:0", dtype = int32) # 3 # 4
On the other hand, let us see tf.print()
:
a = tf.placeholder(shape = None, dtype = tf.int32)
b = tf.placeholder(shape = None, dtype = tf.int32)
print_op = tf.print("a is ", a, " while b is ", b)
with tf.control_dependencies([print_op]):
c = tf.add(a, b)
with tf.Session() as sess:
print(sess.run(c, feed_dict = {
a: 1,
b: 2
}))
print(sess.run(c, feed_dict = {
a: 3,
b: 1
}))
No. The Python
print
is not called when you callsess.run()
later.
If you want to print when you callsess.run()
then you can usetf.print
.To print out multiple tensor values in a graph, you should use
sess.run()
after openingtf.Session()
. Sample code is below.
t = tf.constant(42.0)
u = tf.constant(37.0)
pt = tf.print(t)
pu = tf.print(u)
with sess.as_default():
sess.run([pt, pu])
t = tf.constant(42.0)
u = tf.constant(37.0)
pt = tf.print(t)
pu = tf.print(u)
with sess.as_default():
sess.run([pt, pu])
42 37
Last updated 2022-06-28 UTC.
View aliases
Compat aliases for migration
See Migration guide for more details.
tf.print( * inputs, ** kwargs )
tensor = tf.range(10) tf.print(tensor, output_stream = sys.stderr)
Multi-input usage:
tensor = tf.range(10)
tf.print("tensors:", tensor, {
2: tensor * 2
}, output_stream = sys.stdout)
Usage in a tf.function
:
@tf.function
def f():
tensor = tf.range(10)
tf.print(tensor, output_stream = sys.stderr)
return tensor
range_tensor = f()
In graphs manually created outside of tf.function
, this method returns
the created TF operator that prints the data. 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
with tf.compat.v1.control_dependencies([print_op])
.
tf.compat.v1.disable_v2_behavior() # for TF1 compatibility only sess = tf.compat.v1.Session() with sess.as_default(): tensor = tf.range(10) print_op = tf.print("tensors:", tensor, { 2: tensor * 2 }, output_stream = sys.stdout) with tf.control_dependencies([print_op]): tripled_tensor = tensor * 3 sess.run(tripled_tensor)
tensor = tf.range(10) tf.print(tensor, output_stream = sys.stderr)
Multi-input usage:
tensor = tf.range(10)
tf.print("tensors:", tensor, {
2: tensor * 2
}, output_stream = sys.stdout)
Changing the input separator:
tensor_a = tf.range(2)
tensor_b = tensor_a * 2
tf.print(tensor_a, tensor_b, output_stream = sys.stderr, sep = ',')
No. The Python print is not called when you call sess.run() later. If you want to print when you call sess.run() then you can use tf.print. ,The native Python print() statement will be called when the graph is build the first time. Check this out:,To print out multiple tensor values in a graph, you should use sess.run() after opening tf.Session(). Sample code is below. ,So, according to the output below, we can see that if we add the dependency that the tf.print op must be run whenever c is run, we get to see the output we want to:
a = tf.placeholder(shape = None, dtype = tf.int32)
b = tf.placeholder(shape = None, dtype = tf.int32)
print("a is ", a, " while b is ", b)
c = tf.add(a, b)
with tf.Session() as sess:
print(sess.run(c, feed_dict = {
a: 1,
b: 2
}))
print(sess.run(c, feed_dict = {
a: 3,
b: 1
}))
tf.print( * inputs, ** kwargs )
import tensorflow as tf
a = tf.placeholder(shape = None, dtype = tf.int32)
b = tf.placeholder(shape = None, dtype = tf.int32)
print("a is ", a, " while b is ", b)
c = tf.add(a, b)
with tf.Session() as sess:
print(sess.run(c, feed_dict = {
a: 1,
b: 2
}))
print(sess.run(c, feed_dict = {
a: 3,
b: 1
}))
Last Updated : 22 Apr, 2022
Syntax:
tf.print(value, verbose)
Output:
Tensor
dtype: string
rank: 2
shape: [2, 2]
values: [
['geeks', 'for'],
['geeks', 'website']
]
Sometimes we need to fetch and print the value of a TensorFlow variable to guarantee our program is correct., Fetch the value of a TensorFlow variable or a Tensor , Fetch the value of a TensorFlow variable or a Tensor , Using if condition inside the TensorFlow graph with tf.cond
For example, if we have the following program:
import tensorflow as tf import numpy as np a = tf.Variable(tf.random_normal([2, 3])) # declare a tensorflow variable b = tf.random_normal([2, 2]) #declare a tensorflow tensor init = tf.initialize_all_variables()
if we want to get the value of a or b, the following procedures can be used:
with tf.Session() as sess:
sess.run(init)
a_value = sess.run(a)
b_value = sess.run(b)
print a_value
print b_value
or
with tf.Session() as sess:
sess.run(init)
a_value = a.eval()
b_value = b.eval()
print a_value
print b_value