TensorFlow Learning Notes 2:about Session, Graph, operation and Tensor

Source: Internet
Author: User

Brief introduction

Previous note: TensorFlow study notes 1:get Started We talked about TensorFlow is a computing system based on graph. The nodes of the graph are made up of operations (operation), and each node of the graph is connected by tensor (Tensor) as an edge. So TensorFlow's calculation process is a tensor flow graph. The TensorFlow diagram must be calculated in a session. This note outlines the session, Graph, operation, and tensor.

Session

The session provides an environment for operation execution and tensor evaluation. As shown below,

Import TensorFlow as TF # Build a graph. A = Tf.constant ([1.0, 2.0= Tf.constant ([3.0, 4.0= A * b#  Launch The graph in a SES Sion. Sess = TF. Session ()#  Evaluate the tensor ' C '. Print Sess.run (c) sess.close () # Result: [3., 8.]

A session may have some resources, such as variable or queue. These resources need to be freed when we no longer need the session. There are two ways of

    1. Call the Session.close () method;
    2. Use with TF. Session () Creates a context to execute and is automatically freed when the context exits.

The above example can be written,

Import TensorFlow as TF # Build a graph. A = Tf.constant ([1.0, 2.0= Tf.constant ([3.0, 4.0= a * bwith TF. Session () as Sess:    print sess.run (c)

The constructor for the session class is as follows:

Tf. Session.__init__ (target= ", Graph=none, Config=none)

If graph is not specified when the session is created, the session loads the default graph. If you create multiple graph in a process, you need to create a different session to load each graph, and each graph can be loaded in multiple sessions for calculation.

There are two ways to perform operation or evaluate tensor:

    1. Call the Session.run () method: The method is defined as follows, and the parameter fetches is one or more operation or tensor.

      Tf. Session.run (fetches, Feed_dict=none)

    2. Call Operation.run () or Tensor.eval () method: Both methods receive the parameter session, which specifies the session in which to calculate. However, this parameter is optional and defaults to none, at which point it is calculated in the process default session.

So how do you set a session as the default session? There are two ways of doing this:

1. The session defined in the With statement becomes the default session in that context, and the example above can be modified to:

Import TensorFlow as TF # Build a graph. A = Tf.constant ([1.0, 2.0= Tf.constant ([3.0, 4.0= a * bwith TF. Session ():   print c.eval ()

2. Call the Session.as_default () method in the With statement. The above example can be modified to:

Import TensorFlow as TF # Build a graph. A = Tf.constant ([1.0, 2.0= Tf.constant ([3.0, 4.0= A *= TF. Session () with Sess.as_default ():    print  c.eval () sess.close ()
Graph

The Tf.graph class is used in TensorFlow to represent computable graphs. The graph is composed of the operation operation and tensor tensor, wherein the operation represents the node of the graph (i.e. the computational unit), while tensor represents the edge of the graph (the data unit that flows between the operation).

Tf. Graph.__init__ ()

Create a new empty graph

In TensorFlow, there is always a default graph. If you want to add operation to the default graph, you only need to call the function that defines operation (for example, Tf.add ()). If we need to define multiple graph, we need to call the Graph.as_default () method in the With statement to set the graph to the default graph, so that the operation or tensor called in the With statement block will be added to the graph.

For example

 import   TensorFlow as Tfg1  = TF. Graph () with G1.as_default (): C1  = Tf.constant ([1.0

If you swap the C1 and C2 in Sess1.run (C1) and Sess2.run (C2) of the above example for a position, the operation will be an error. Because the Sess1 loaded G1 does not have C2 this tensor, similarly, sess2 loaded G2 does not C1 this tensor.

Operation

A operation is a compute node in TensorFlow graph. It receives 0 or more tensor objects as input, and then produces 0 or more tensor objects as outputs. The Operation object is created by calling the Python operation method directly (for example, Tf.matmul ()) or Graph.create_op ().

For example c = tf.matmul(a, b) , a operation of type Matmul is created, and the operation receives tensor a and tensor B as input and produces tensor C as the output.

When a graph is loaded into a session, you can call Session.run (OP) to execute the OP, or call Op.run () to execute (Op.run () is the abbreviation for the Tf.get_default_session (). Run ().

Tensor

Tensor represents the output of the operation. However, tensor is only a symbolic handle, and it does not save the value of the operation output. The value of the tensor can be obtained by calling Session.run (tensor) or tensor.eval ().

On the calculation process of graphs of tensorflow

Let's take a look at the graph calculation process for TensorFlow in the following code:

Import= tf.constant (1= tf.constant (2= tf.constant (3= tf.constant (4  = = = = = Tf.add (add1, MUL1) with TF. Session () as Sess:    print  sess.run (output)
# Result:9

The above code is composed of graph as shown,

When the session loads graph, the compute nodes inside the graph are not triggered to execute. When the Sess.run (output) is run, the corresponding node is triggered back along the specified tensor output to the input path (the portion of the red line in the figure). When we need the value of output, trigger operation Tf.add (ADD1, MUL1) is executed, and the node needs tensor add1 and tensor mul1 value, then trigger Operation Tf.add (A, B) and Operation Tf.mul (b, c). And so on

Therefore, when calculating graph, it is not necessarily all the nodes in graph are computed, but the specified compute node or the output result of the node is required.

(done)

TensorFlow Learning Notes 2:about Session, Graph, operation and Tensor

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.