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))