1. Basic concepts of tensorflow
Use graphs to represent computational tasks
Execute the graph in a context called a Session
Use tensor to represent data
Maintaining state through variables
Use feed and fetch to assign values to or fetch data from any operation
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 seen as an n-dimensional array or list. The graph must be started in a session.
2. Start the graph with python
import tensorflow as tf #create a constant op m1=tf.constant([[3,3]]) #create a constant op m2=tf.constant([[2],[3]]) #Create a matrix multiplication, passing in m1 and m2 product=tf.matmul(m1,m2) print (product) #Define a session to start the default graph sex = tf.Session () #Call the run method of sess to perform matrix multiplication op #run(product) triggers 3 ops in the figure result=sess.run(product) print(result) #Close the session sess.close() #The following definition method does not require close: #with tf.Session() as sess: # result=sess.run(product) # print(result)
3. Use of variables
import tensorflow as tf x=tf.Variable([1,2]) a=tf.constant([3,3]) #Add a subtraction op sub=tf.subtract(x,a) #Add an addition op add=tf.add(x,a) #initialize all variables init=tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print (sess.run (sub)) print (sess.run (add))
result:
import tensorflow as tf #Create a variable, initialized to 0 state=tf.Variable(0,name='counter') #Create an addition op that adds 1 to the state new_value=tf.add(state,1) #assign op update=tf.assign(state,new_value) #initialize all variables init=tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print (sess.run (state)) for _ in range(5): sess.run(update) print (sess.run (state))
result:
4.fetch executes multiple ops in a session and gets the results
import tensorflow as tf #Fetch input1=tf.constant(3.0) input2=tf.constant(2.0) input3=tf.constant(5.0) add=tf.add(input2,input3) mul=tf.multiply(input1,add) with tf.Session() as sess: result=sess.run([mul,add]) print(result)
result:
A feed can temporarily replace a tensor in any operation in the graph, and a patch can be submitted for any operation in the graph, directly inserting a tensor.
import tensorflow as tf #Feed #create placeholder input1=tf.placeholder(tf.float32) input2=tf.placeholder(tf.float32) output=tf.multiply(input1, input2) with tf.Session() as sess: #feed data is passed in as a dictionary print(sess.run(output,feed_dict={input1:[7.0],input2:[2.0]}))
result:
5. Use Cases
import tensorflow as tf import numpy as np #Use numpy to generate 100 random points x_data=np.random.rand(100) y_data=x_data*0.1+0.2 #Construct a linear model b=tf.Variable(0.) k=tf.Variable(0.) y=k*x_data+b #Secondary cost function loss=tf.reduce_mean(tf.square(y_data-y)) #Define a gradient descent method to train an optimizer with a 0.2 learning rate optimizer=tf.train.GradientDescentOptimizer(0.2) #define a minimization cost function train=optimizer.minimize(loss) #Initialize variables init=tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for step in range(201): sess.run (train) if step%20==0: print(step,sess.run([k,b]))
result: