TensorFlow Series-Basic usage

Source: Internet
Author: User
Tags constant constructor mul sessions

In order to use TensorFlow, we need to understand what TensorFlow is. The following is a description of the 5 characteristics of TensorFlow: Use a graph to indicate that the calculation process uses sessions (sessions) to perform diagrams using tensors to represent data using variables to maintain state using feeds and fetches operations to remove or deposit data to any operation 1. Overview

TensorFlow is a programming system, and you need to use a graph of calculations. The nodes in the diagram are called OPS (operations, operation abbreviations). The input of an operation is 0 or more tensors, which is used for some calculations and then produces 0 or more tensors outputs. We can think of tensor as a multidimensional matrix. For example, we can represent a heap (batch) picture as a 4-d matrix, [batch, height, width, channels].
A TensorFlow diagram depicts a computational process. In order to complete the calculation, the graph is started in a session. The session assigns the actions in the diagram to devices, such as Cpus,gpus, and then provides some way to execute them. 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 tensor returned is the Tensorflow::tensor instance. 2. Calculation Diagram

A tensorflow program is usually divided into two stages-the construction phase, the execution phase. The construction phase is a diagram, and the execution phase uses a session to complete the operation in the diagram.
For example, we construct a diagram in the construction phase to describe the structure and training process of the neural network, and then perform the training operations repeatedly on the graph during the execution phase.
TensorFlow supports c,c++ and Python 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. 2.1 Build Diagram

At first we constructed a non-input operation (source node), such as constant, and then put the output of the operation as input to other operations.
The TensorFlow Python Library has a default graph, which the OP constructor can add nodes to. This default diagram is sufficient for many programs. Read the Graph class document to learn how to manage multiple diagrams.

Import TensorFlow as TF  

# Creates a constant op that produces a 1x2 matrix. This op is added as a node  
# to the default diagram.  
# The return value of the constructor represents the return value of the constant op.  
matrix1 = Tf.constant ([[3., 3.]])  

# Create another constant op that produces a 2x1 matrix.  
matrix2 = Tf.constant ([[[2.],[2.]])  

# Create a matrix multiplication Matmul op, put ' matrix1 ' and ' matrix2 ' as input.  
# The return value ' product ' represents the result of the matrix multiplication.  
Product = Tf.matmul (matrix1, matrix2)
2.2 Starting a diagram in one session

After the diagram is built, you need to create a Session object to start the diagram.

# 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. 
# As mentioned above, ' product ' represents the output of the Matrix multiplication op, which is passed to the method to indicate that we want to retrieve
the output of the # matrix multiplication op.
# The entire execution process is automated and the session is responsible for passing all the inputs required by the OP. The OP is usually executed concurrently.
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
# ==> [[[]

] # The task is complete and the session is closed.
Sess.close ()

In addition to the displayed close drop, you can also use the With statement to automatically close, similar to the Python open file:

With TF. Session () as sess:
  result = Sess.run ([Product])
  Print result
2.3 Interactive Use

Using the method in 2.2, it is very inconvenient to run a node each time Session.run (section name). Sometimes you need to look at the results interactively. TensorFlow provides the InteractiveSession class instead of the session class.

# Enter an interactive tensorflow session.
Import TensorFlow as tf
sess = tf. InteractiveSession ()

x = tf. Variable ([1.0, 2.0])
a = Tf.constant ([3.0, 3.0])

# initializes the ' X ' initializer using the initializer X.initializer.run op () method 
(

# Add a subtraction sub op, subtract ' a ' from ' X '. Run subtraction op, output 
sub = tf.sub (x, a)
print sub.eval ()
# ==> [-2.-1.]

Because the input in the diagram is no longer a constant, x is a variable, so you need to initialize the variables in the diagram before starting the diagram. representation of data in 2.4 TensorFlow

TensorFlow uses tensor to represent data, whether the data is a constant or a variable, a one-dimensional array or a two-dimensional array. A tensor contains a static type rank and a shape. 2.5 Variables

Variables are one of the most commonly used data types in TensorFlow. The following example shows how to use a variable to implement a simple counter.

# Create a variable initialized to scalar 0.
state = TF. Variable (0, name= "counter")

# creates an op whose role is to increase the state by 1 One

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

# After starting the diagram, the variable must first be initialized with the ' init ' op initialization,
# First you must add an ' Initialize ' op to the diagram.
Init_op = Tf.initialize_all_variables ()

# start diagram, run op with
TF. Session () as Sess:
  # run ' init ' op
  sess.run (init_op)
  # Print the initial value of ' state ' print
  sess.run (state)
  # Run OP, update ' state ', and print ' state ' for
  _ in range (3):
    sess.run (update)
    print Sess.run (state)

# output:

# 0
# 1
# 2
# 3

Any node before Sess.run () will not be run, Tf.add, tf.assign, tf.initialize_all_variables These statements just tell the computer how these nodes are calculated, and there is no real operation. The real operation is when they are run. 2.6 Fetch

We can also perform multiple operations in one run at the same time.

Import TensorFlow as tf
input1 = tf.constant (3.0)
Input2 = tf.constant (2.0)
INPUT3 = tf.constant (5.0)
intermed = Tf.add (Input2, input3)
mul = Tf.mul (INPUT1, intermed) with

TF. Session () as Sess:
  result1, result2 = Sess.run ([Mul, intermed])
  print result1
  print result2
2.7 Feed

Sometimes when building diagrams, it's not quite certain what the input is. Or, for flexibility, the input can be multiple variables. When defining the input, you can use the placeholder object to occupy a hole first, and then the definition of the Operation node is the same as before. The difference is that you need to specify the input when run.

INPUT1 = Tf.placeholder (tf.float32)
input2 = Tf.placeholder (tf.float32)
output = Tf.mul (INPUT1, Input2) With

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

# output:
# [Array ([14.] , Dtype=float32)]

This is the basic usage of tensorflow, which will be followed by a more in-depth introduction to TensorFlow usage for specific applications.

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.