TensorFlow Introduction (II.)--Basic usage

Source: Internet
Author: User
Tags constant constructor eval mul

TensorFlow: A graph is used to indicate that a calculation task is performed in the context of a conversation called a session using tensor to represent data through variables (Variable) to maintain state using FE Ed and fetch can assign or fetch data from any operation (arbitrary operation)

TensorFlow is a programming system that uses diagrams to represent computational tasks. The node in the diagram is called the OP (operation abbreviation). An op obtains 0 or more Tensor, performs calculations, and produces 0 or more Tensor. Each Tensor is a typed multidimensional array. For example, you can represent a group of image sets as an array of four-dimensional floating-point numbers, which are [batch, height, width, channels], respectively.

A TensorFlow diagram describes the process of calculation. In order to calculate, the diagram must be started in the session. The session distributes the OP of the graph to devices such as CPUs or GPUs, while providing a way to execute the op. After these methods are executed, the resulting tensor is returned. In the Python language, the returned tensor is the NumPy Ndarray object; In the C and C + + languages, the returned tensor is an tensorflow::tensor instance.

calculation Diagram

TensorFlow programs are usually organized into a build phase and an execution phase. During the build phase, the OP execution steps are described as a graph. During the execution phase, the OP in the execution diagram is executed using the session.

For example, you typically create a diagram in the build phase to represent and train a neural network, and then perform the training op in the diagram repeatedly during the execution phase.

TensorFlow supports C, C + +, Python programming languages. Currently, the TensorFlow Python Library is more user-friendly, providing a number of helper functions to simplify the work of building diagrams that have not been supported by the C and C + + libraries.

The session libraries in three languages is consistent.

Build Diagram

The first step in building the diagram is to create the source op. The source OP does not require any input, such as constants (Constant). The output of the source OP is passed to other OP operations.

In the Python library, the return value of the OP constructor represents the output of the OP that is constructed, and these return values can be passed to other OP constructors as input.

The TensorFlow Python Library has a default graph, which the OP constructor can add nodes to. This default diagram is sufficient for many programs.

#!/usr/bin/env python3
import tensorflow as TF

#创建一个常量op, producing a 2x3 matrix
#这个op被作为一个节点加到默认图中
#
# The return value of the constructor represents the return value of the constant op
matrix1 = tf.constant ([[[5.,3.,2.],[1.,2.,3.]])

#创建另一个常量op, produces a 3x4 matrix
matrix2 = Tf.constant ([[2.,1.,3.,4.],[2.,1.,3.,4.],[2.,1.,2.,4.]])

#创建一个矩阵乘法matmul op
#把matrix1和matrix2作为输入
#返回值product代表矩阵乘法的结果
product = Tf.matmul (matrix1,matrix2)
# Start the default diagram.
Sess = tf. Session ()

# calls the ' run () ' method of Sess to perform the matrix multiplication op, passing in ' product ' as the parameter of the method
# mentioned above, ' product ' represents the output of the Matrix multiplication op, which is passed to the method indicating that I We want to retrieve the
# Matrix multiplication op output
#
# # # # # # # # # # # # # # # # # # # # # # #
The 
function call ' run ' triggers the execution of the three OP (two constant op and one matrix multiplication op) in the diagram.
#
# return value ' result ' is a numpy ' Ndarray ' object.
result = Sess.run (product)
print (Result)

# Task completed, closing session.
Sess.close ()

Output Result:

[[Ten]  .  ().
[   6.  24.]

The default graph now has three nodes, two constant () op, and one Matmul () op. In order to actually perform the multiplication of matrices and get the result of matrix multiplying, the graph must be started in the session.

to start a diagram in a session

After the construction phase is complete, the diagram can be started. The first step in the start diagram is to create a session object, and if no parameters are created, the session builder starts the default diagram.
Session objects need to be closed to release resources after they are used ... In addition to explicitly calling close, you can use the "with" code block to automatically complete the close action.

With TF. Session () as sess:
  result = Sess.run ([Product])
  print (Result)

Interactive Use
You can use InteractiveSession instead of the Session class, using the Tensor.eval () and Operation.run () methods instead of Session.run (). This avoids using a variable to hold the session.

#!/usr/bin/env python3
import tensorflow as tf
sess = tf. InteractiveSession ()

x = tf. Variable ([1.,2.])
y = tf.constant ([3.,4.])

The run () method of the #使用初始化器initializer op initializes x
x.initializer.run ()

#增加一个减法sub op, minus y sub from x
= Tf.subtract (x, y)
print (Sub.eval ())

Output Result:

[-2.-2.]

Tensor

The TensorFlow program uses a tensor data structure to represent all of the data, and the data passed between operations is tensor in the calculation diagram. You can think of TensorFlow tensor as an array or list of n-dimensions. A tensor contains a static type rank, and a shape.

variables

The variable maintains the state information during the execution of the diagram.

Use variables to implement a simple calculator:

#!/usr/bin/env python3
import tensorflow as TF

#创建一个变量, initialized to scalar 0 state
= tf. Variable (0,name= "counter")

#创建一个op with the effect of increasing state by 1 One

= tf.constant (1)
new_value = Tf.add (State,one)
update = tf.assign (state,new_value)

#启动图后, the variable must first initialize (INIT) OP initialization
#首先必须增加一个初始化op到图中
init_op = Tf.initialize_all_variables ()

#启动图, run op with
TF. Session () as Sess:
    #运行init op
    sess.run (init_op)
    #打印state初始值
    Print (Sess.run (state))
    #运行op , update state, and print state for
    _ in range (3):
        sess.run (update)
        print (Sess.run)

Output Result:

0
1
2
3

The Assign () operation in the code is part of the expression depicted by the diagram, just like the Add () operation. So before calling run () to execute an expression, it does not actually perform the assignment operation.

Typically, a parameter in a statistical model is represented as a set of variables. For example, you can store the weight of a neural network as a variable in a tensor. During the training process, the tensor is updated by repeatedly running the training map.

Fetch

In order to retrieve the output of the operation, you can pass in some tensor when you invoke run () of the Session object, and these tensor will help retrieve the results. In the previous example, we retrieved only the single node state, but we can also retrieve multiple tensor:

#!/usr/bin/env python3
import tensorflow as tf

INPUT1 = tf.constant (4.0)
Input2 = tf.constant (1.0)
INPUT3 = tf.constant (3.0)
intermed = Tf.add (Input2, input3)
mul = tf.multiply (INPUT1, intermed) with

TF. Session () as sess:
  result = Sess.run ([Mul, intermed])
  print (Result)

Output Result:

[16.0, 4.0]

Multiple tensor values that need to be acquired are obtained together in one run of the OP (instead of getting tensor individually).

Feed

The example above introduces tensor in a calculation diagram, stored as a constant or variable. TensorFlow also provides a feed mechanism that can temporarily replace tensor in any operation in the diagram to submit patches to any action in the diagram and insert a tensor directly.

The feed uses a tensor value to temporarily replace the output of an operation. You can provide the feed data as a parameter of the run () call. The feed is only valid within the method that invokes it, and the feed disappears when the method ends. The most common use case is to designate certain special actions as "feed" operations, and the tagging method is to use Tf.placeholder () to create placeholders for these operations.

#!/usr/bin/env python3
import tensorflow as tf

input1 = Tf.placeholder (tf.float32)
input2 = Tf.placeholder (Tf.float32)
Output = Tf.multiply (INPUT1, Input2) with

TF. Session () as Sess:
  print (Sess.run ([Output], feed_dict={input1:[7.], input2:[2.]))

Output Result:

[Array ([], Dtype=float32)]

If the feed is not provided correctly, the placeholder () operation will produce an error.

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.