[02]tensorflow Basic usage

Source: Internet
Author: User
Tags mul

Points


Using TensorFlow, you must understand TensorFlow:

    • Use graphs to represent calculation tasks.
    • The diagram is executed in the context of what is called a session.
    • Use tensor to represent data.
    • The state is maintained through a variable (Variable).
    • Use feeds and fetch to assign or fetch data from any operation (arbitrary operation).
TensorFlow Structure: Sessions (session), graph (graph), node (operation), Edge (tensor)

TensorFlow is a programming system that uses diagrams to represent computational tasks. We start with the most basic elements.

    • Side (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.

    • Node (operation)

      The node is called the OP (operation abbreviation). An op obtains 0 or more Tensor, performs calculations, and produces 0 or more Tensor.

    • Graph (graph)

      The nodes (operation) and edges (tensor) are connected to each other as diagrams. A TensorFlow diagram describes the process of calculation.

    • Sessions (session)

      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, and 通常在构建阶段创建一个图来表示和训练神经网络 then at the execution stage 反复执行图中的训练 op .

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. Read the Graph class document to learn how to manage multiple diagrams.

[Root@master tensorflow]# IpythonPython2.7. 5(Default, Nov -  -, Geneva:xx: +) Type"Copyright","Credits" or "License"  forMore information. IPython5.0. 0--an enhanced Interactive Python.? Introduction andOverview of IPython' s features.%quickref, Quick reference.help, Python 's own Help System.Object? Details about' object ', use' object?? '  forExtra details. in [1]:ImportTensorFlow asTfin [2]: matrix1 = tf.constant ([[3.0,3.0]]) in [3]: matrix2 = tf.constant ([[2.0],[2.0]]) in [4]: Product = Tf.matmul (matrix1,matrix2)

The default graph now has three nodes, two constant () op, and one Matmul () op. In order to actually perform a matrix multiplication and get the result of a matrix multiply, you must start the diagram in the session.

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.

Read the session class for the full conversation API.

In [5]: sess = tf.Session()#没有参数就启动默认图I tensorflow/core/common_runtime/local_device.cc:2524I tensorflow/core/common_runtime/local_session.cc:4524In [6]: result = sess.run(product)In [7print12.]]In [8]: sess.close()

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.

In [9withas sess:   ...:     result = sess.run([product])   ...:     print result   12.]], dtype=float32)]

On the implementation, TensorFlow transforms the graphical definition into distributed execution operations to take advantage of available compute resources such as CPUs or GPUs. Generally you do not need to explicitly specify whether to use the CPU or GPU, TensorFlow can automatically detect. If the GPU is detected, TensorFlow will take advantage of the first GPU found to perform the operation.

If there is more than one GPU available on the machine, the other GPU, except the first one, does not participate in the calculation by default. In order for TensorFlow to use these GPUs, you must explicitly assign the OP to them to execute. With ... Device statements are used to assign specific CPUs or GPUs to perform operations:

with tf.Session() as sess:    with tf.device("/gpu:1"):        matrix1 = tf.constant([[3., 3.]])        matrix2 = tf.constant([[2.],[2.]])        product = tf.matmul(matrix1, matrix2)

The device is identified by a string. The devices currently supported include:

    • "/cpu:0": CPU of the machine, all CPUs
    • "/gpu:0": the first GPU of a machine, if any.
    • "/gpu:1": The second GPU of a machine, and so on.
      Read more about TensorFlow GPU usage using the GPU chapters.
Ipython Interactive Use

The Python example in the document uses a session to start the diagram and calls the Session.run () method to perform the operation.

In order to facilitate the use of Python interactive environments such as IPython, InteractiveSession alternative Session classes, uses, and methods can be used Tensor.eval() Operation.run() instead Session.run() . This avoids using a variable to hold the session.

# 进入一个交互式 TensorFlow 会话.importas tfsess = tf.InteractiveSession()x = tf.Variable([1.02.0])a = tf.constant([3.03.0])# 使用初始化器 initializer op 的 run() 方法初始化 ‘x‘ x.initializer.run()# 增加一个减法 sub op, 从 ‘x‘ 减去 ‘a‘. 运行减法 op, 输出结果 sub = tf.sub(x, a)print sub.eval()# ==> [-2. -1.]

Results:

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 dynamic type shape. To understand how TensorFlow handles these concepts, see Rank, Shape, and Type.

Order (rank)

In the TensorFlow system, the dimensions of the tensor are described as the order. But the order of the tensor and the order of the matrix is not the same concept. The Order of the tensor (sometimes about, for example, order or degree or n-dimensional) is a quantitative description of the number of Zhang Xiwei. For example, the following tensor (using the list defined in Python) is the 2-step.

 t = [[123], [456], [789]]

You can think of a second-order tensor as what we normally call a matrix , which can be thought of as a vector . For a second-order tensor, you can use the statement t[i, J] to access any of these elements. For third-order tensor you can use ' T[i, J, K ' to access any of these elements.

Shapes (Shape)

The TensorFlow document uses three symbols to conveniently describe the dimensions of the tensor: order, shape, and dimension. The following table shows the relationship between them:

Shapes can be represented either by a list of integers in Python or by ganso (int list or tuples), or by using the Tensorshape class.

DataType (type)

In addition to dimensions, tensors has a data type property. You can specify any one of the following data types for a tensor:

Variable

The variable maintains the state information during the execution of the diagram. The following example shows how to use a variable to implement a simple counter. See the variables section for more details.

#coding: Utf-8ImportTensorFlow asTf# Create a variable state. Initialize to scalar 0.state = TF. Variable (0, name="Counter")# Create an op whose purpose is to increase the state by 1one = Tf.constant (1) New_value = Tf.add (state, one)# define an actionUpdate = Tf.assign (state, New_value)# Update the state value with the return value of the operation# 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 withTf. Session () asSess:# Run ' init ' opSess.run (INIT_OP)# Print the initial value of ' state '    PrintSess.run (state)# Run OP, update ' state ', and print ' state '     for_inchRange3): Sess.run (update)PrintSess.run (state)

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.

Operation Result:

Fetch (Retrieve)

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 you retrieve the results. In the previous example, we only retrieved the single node state, but you can also retrieve multiple tensor:

#coding:utf-8importas tfinput1 = tf.constant(3.0)input2 = tf.constant(2.0)input3 = tf.constant(5.0)intermed = tf.add(input2, input3)mul = tf.mul(input1, intermed)withas sess:    result = sess.run([mul, intermed])#在 op 的一次运行中一起获得多个tensor值。    print result# 输出:# [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]

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

Output Result:

Feed (Inject)

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 parameters for 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.

#coding:utf-8importas tfinput1 = tf.placeholder(tf.types.float32)input2 = tf.placeholder(tf.types.float32)output = tf.mul(input1, input2)withas sess:    print sess.run([output], feed_dict={input1:[7.], input2:[2.]})#通过feed_dict词典向op里注入实际的值。

If the feed is not provided correctly, the placeholder () operation will produce an error. The MNIST fully connected feed tutorial (source code) gives an example of a larger-scale use of the feed.

Operation Result:

[02]tensorflow Basic usage

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.