Learn TensorFlow, print out the value of tensor

Source: Internet
Author: User
Tags constant data structures eval

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.