1. Basic concepts of Tensorflow
1. Use graphs to represent computing tasks
2. Execute the graph in a context called a Session
3. Use tensor to represent data
4. Maintain state through variables (Variable)
5. Use feed and fetch to assign values to or get data from arbitrary operations
Tensorflow is a programming system that uses graphs to represent computing tasks. The nodes in the graphs are called ops (operations). An op obtains 0 or more Tensors, performs calculations, and generates 0 or more Tensor, Tensor is regarded as an n-dimensional array or list. The graph must be started in a session.
Second, the basic concepts of Python code implementation
1. Create and run the graph
1 import tensorflow as tf #The abbreviation is convenient 2 3 #Create two constants (constant) 4 m1=tf.constant([[3,3]]) #Matrix with one row and two columns 5 m2=tf.constant([[2 ],[3]]) #Matrix with two rows and one column 6 7 #Create a matrix multiplication (matmul) op 8 product= tf.matmul(m1,m2) 9 print (product)
Running will get the display result
Tensor("MatMul:0", shape=(1, 1), dtype=int32)
The result is not a specific number as imagined, but a Tensor. This is because it was mentioned that the graph must be run in a session. Now we do not use a session, so we can only get one Tensor.
There are two ways to define a session
1 # method 1 2 sess=tf.Session() #Abbreviate Session as sess 3 result=sess.run(product) #Call the run method to execute the graph, which triggers the establishment of three op (operations) and two constants, Matrix multiplication 4 print (result) 5 sess.close() #Close the session
1 # method 2 2 with tf.Session() as sess: # The () after Session() is not in the code prompt, so it is easy to lose 3 result= sess.run(product) 4 print (result) # with as This structure automatically closes the session
Running shows the result is
[[15]]
2. Variables
1 import tensorflow as tf 2 3 x=tf.Variable([1,2]) #define a variable 4 a=tf.constant([3,3]) #define a constant 5 6 sub= tf.subtract (x, a) #Add a subtraction op 7 add=tf.add(x,sub) #Add an addition op 8 9 init=tf.global_variables_initializer() #Use variables in tensorflow to initialize, this statement can also initialize multiple variables 10 11 with tf.Session() as sess: 12 sess.run(init) #Variable initialization must be placed in the session to execute 13 print (sess.run(sub)) 14 print (sess.run (add))
run will get the result
[-2 -1]
[-1 1]
The above code shows the definition and initialization of variables, but does not reflect the essence of variables. The following code implements the variable state to perform 5 +1 operations
1 state=tf.Variable(0,name= ' counter ' ) #Create a variable initialized to 0 and named counter. (The name in this code has no effect) 2 new_value = tf.add(state,1) #Create an addition op, the function is to add 1 to the state 3 update=tf.assign(state,new_value) #This sentence is an assignment op , in tensorflow, assignment also requires corresponding method 4 init=tf.global_variables_initializer() #variable initialization 5 with tf.Session() as sess: 6 sess.run(init) 7 print (sess.run(state)) 8 for _ in range(5): #In Python, variables that do not need to pay attention to their actual meaning can be replaced by _, which is the same as for i in range(5), because here we don't care about i, so use _ instead to just get the value. 9 sess.run(update) 10 print (sess.run(state))
run, the result is
0
1
2
3
4
5
3、Fetch
The so-called fetch is to perform multiple ops, and two ops are executed in the next code sess.run
1 import tensorflow as tf 2 3 input1 = tf.constant(3.0 ) 4 input2 = tf.constant(2.0 ) 5 input3 = tf.constant(5.0 ) 6 7 add = tf.add(input2,input3) 8 mul= tf. multiply(input1,add) 9 10 with tf.Session() as sess: 11 result = sess.run([mul,add]) #Execute two ops , pay attention to format 12 print (result)
run and the result is displayed as
[21.0, 7.0]
4、Feed
The meaning of feed is to not enter specific values first, first use the placeholder method to occupy the place, and then enter the specific value when calling op in the session.
import tensorflow as tf input1 = tf.placeholder(tf.float32) #Use placeholder () to take place, need to provide type input2 = tf.placeholder(tf.float32) output = tf.multiply(input1,input2) with tf.Session() as sess: print (sess.run(output,feed_dict={input1:[8.],input2:[2.]})) #To use feed, you need to call run(), in addition to input op, also need to be in dictionary form enter feed_dict
run, the result is
[ 16.]
The above is the description and code implementation of some basic tensorflow concepts
ps: I am a beginner, please point out any mistakes. grateful.