In the process of learning tensorflow, we need to know what the value of a tensor is, which is important, especially at the time of Debug. Maybe you can say, this is easy, just print it. In fact, print only prints output shape information, and to print out the value of the tensor, you need to use class TF. Session, class TF. InteractiveSession. Because we are building graph, we only build tensor structure shape information, and do not perform data operation.
A class TF. Session
A class that runs the TensorFlow operation whose objects encapsulate the environment that executes the operand and evaluates the tensor value. This we introduced before, after defining all the data structures and operations, it runs at the end.
Import TensorFlow as TF
# Build a graph.
A = tf.constant (5.0)
B = tf.constant (6.0)
C = A * b
# Launch The graph in a session.
Sess = tf. Session ()
# Evaluate The tensor ' C '.
Print (Sess.run (c))
Two class TF. InteractiveSession
As the name implies, the session for the interactive context facilitates the output of tensor values. Compared to the previous session, it has a default session to perform related operations, such as: Tensor.eval (), Operation.run (). Tensor.eval () is the same for all operations before performing this Tensor, Operation.run ().
Import TensorFlow as tf
a = tf.constant (5.0)
B = tf.constant (6.0)
C = A * B with
TF. Session ():
# We can also use ' c.eval () ' here.
Print (C.eval ())
Reference:
[1] Https://www.tensorflow.org/versions/r0.9/api_docs/python/client.html#InteractiveSession
[2] Http://stackoverflow.com/questions/33633370/how-to-print-the-value-of-a-tensor-object-in-tensorflow