TensorFlow Basic Concepts

Source: Internet
Author: User
Tags constant
Use the graph (graphs) to represent the calculation task, the node in the graph is called Operation OP (Operation). Each diagram represents its own calculation task. The TensorFlow Python library has a default diagram, and the first step in the start diagram is to create a session object, and if there are no parameters to create, the session builder starts the default diagram. The OP constructor can add nodes to it. Most of this default diagram is enough to use. Calculations are performed in the context of the session (session), which distributes the OP (operation) of the graph to the CPU or GPU, executes these methods, and returns the resulting tensor. Therefore, the calculation is to create a session, note the Conversation (session) of the S capital. Maintaining state through variables (Variable) use tensor to represent data using feeds and fetches to assign or fetch data from any operation

Here's a simple example to help you understand how TensorFlow works:

Import TensorFlow as TF
#创建一个常量op, added to the default diagram
a1 = Tf.constant ([[3,2]]) 
a2 = Tf.constant ([[1],
                  [3]])
Print (a1, a2)
product = Tf.matmul (A1,A2)
print (product) #如果不创建会话, the calculation results are not output properly.

sess = tf. Session ()

#此时, the default graph has three nodes. Two X constant () op, one Matmul () op
result = Sess.run (product)
print (Result)
Sess.close ()

Create a variable (Variable) to store the parameters. Passes a tensor as the initial value to the constructor variable (). TensorFlow provides a series of operators to initialize the tensor, with the initial value being a constant or random value . variables must be initialized before they are used, or they will be error-

Failedpreconditionerror (see above for Traceback): Attempting to use uninitialized value Variable

For example, here's a simple example.

Import TensorFlow as tf

a = tf. Variable ([up])
x = Tf.constant ([3,3])

add = Tf.add (x,a)
sub = tf.subtract (x,a)

init = Tf.initialize_ All_variables () #进行变量的初始化 with

TF. Session () as Sess:  #with代码块会自动完成关闭会话动作
    sess.run (init)
    print (Sess.run (add))
    print (Sess.run (sub))

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.