Ways to view tensor values in TensorFlow

Source: Internet
Author: User

When debugging a program written in TensorFlow, you need to know what the value of a tensor is. Direct print can only print out information such as the shape,dtype of the output tensor, and the method to view the values of the tensor is as follows:

"1" with class TF. Session or Class TF. InteractiveSession class

Import TensorFlow as tf x = tf. Variable (Tf.constant (0.1, Shape = [ten])) Init_op = Tf.global_variables_initializer () with TF. Session () as Sess:sess.run (init_op) print (X.eval ()) [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]

Or is this:


Import TensorFlow as tf x = tf. Variable (Tf.constant (0.1, Shape = [ten])) Init_op = Tf.global_variables_initializer () with TF. Session () as Sess:sess.run (init_op) print (Sess.run (x)) [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]

You can show it.

The difference between session and InteractiveSession is summed up by others: TF. InteractiveSession (): It allows you to insert some calculation diagrams that are composed of some operations (operations) while running the diagram. This is very convenient for people working in interactive environments, such as using Ipython.

Tf. Session (): You need to build the entire diagram before starting the session, and then start the calculation diagram.

It means that when we use TF. InteractiveSession () to build a session, we can first build a conversation and then define the operation (operation) if we use TF. Session () To build sessions we need to define all the actions (operation) before the session is built and then build the session.

"2" with TF. Print () function

This blog is written more clearly, give examples can also be used. Http://blog.csdn.net/qq_34484472/article/details/75049179?locationNum=5&fps=1

First give the function description:

Tf. Print (input_, data, Message=none,first_n=none, Summarize=none, Name=none)

Print a list of tensor

Input: Input_: Through a tensor of this Op.

Data: The tensorlist that prints the output when this OP is computed.

Message: A string that is the prefix of the error messages.

First_n: Only first_n times are recorded. Always record negative numbers; This is the default.

Summarize: The number of items that are printed only for each tensor. If none, only 3 elements are printed for each input tensor.

Name:op's name.

return: The same tensor as Input_

Example: Import TensorFlow as tf sess = tf. InteractiveSession () A = Tf.constant ([1.0, 3.0]) Sess.run (TF. Print (A, [a], message= "This is a:"))

Can get the printed result.


Import TensorFlow as tf sess = tf. InteractiveSession () x = tf. Variable (Tf.constant (0.1, Shape = [ten])) Sess.run (X.initializer) Sess.run (TF. Print (x, [x], message = ' This is x: ')

This program can print the X variables I define, but if I take the TF. Print is placed in the with TF. The session () does not print, but it does not complain.




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.