This article is used to learn the basic use of TensorFlow (basic structure, variable management, model persistence)! ! !
1. The basic structure of TensorFlow
#1. Create a compute node a = tf.constant([1.2, 1.2], name='a') b = tf.constant([1.0, 1.0], name='b') result = tf.add(a,b,name='add'); #output operation node information print(result) #2. Create a session sex = tf.Session () #3. Running the computational graph print( sess.run(result) ) # Calculate the value of the tensor print( result.eval(session = sess) ) #4. Close the session and release resources sess.close() #1. Create a compute node a = tf.constant([1.2, 1.2], name='a') b = tf.constant([1.0, 1.0], name='b') result = tf.add(a,b,name='add'); #output operation node information print(result) #2. Create a session and manage this session through the context of python. When the context exits, the session is closed and the resource release is automatically completed sex = tf.Session () with sess.as_default(): #3. Running the computational graph print( sess.run(result) ) # Calculate the value of the tensor print( result.eval() )
Based on the use of TensorFlow linear regression algorithm, the training set is two features of iris! ! !
#Based on the use of TensorFlow linear regression algorithm, the training set is two features of iris import tensorflow as tf import numpy as np import matplotlib.pyplot as plt from sklearn import datasets #get dataset iris = datasets.load_iris().data print(type(iris)) train = np.array([arr[3]for arr in iris])#petal width feature label = np.array([arr[0]for arr in iris])#petal length feature #1. Create a compute node #define placeholder variables x_data = tf.placeholder(shape = [None,1], dtype = tf.float32) y_data = tf.placeholder(shape = [None,1], dtype = tf.float32) w = tf.Variable(tf.random_normal([1,1])) b = tf.Variable(tf.random_normal([1,1])) #decision function y = tf.add(tf.matmul(x_data, w), b) #mean squared loss function loss = tf.reduce_mean(tf.square(y_data - y)) #optimization train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss) #2. Create a session sex = tf.Session () #3. Running the computational graph init = tf.global_variables_initializer() sess.run(init) steps = 500 batch = 5 for i in range(steps): #Get the training set and label set of the batch rand_index = np.random.choice(len(train), size = batch) rand_x = np.transpose([train[rand_index]]) rand_y = np.transpose([label[rand_index]]) #train model parameters sess.run(train_step, feed_dict = {x_data:rand_x,y_data:rand_y}) if i%30==0: print(sess.run(loss, feed_dict = {x_data:rand_x,y_data:rand_y})) # get model parameters [weight] = sess.run(w) [bias] = sess.run(b) # get the fitting result result = [] for i in train: result.append(i*weight+bias) # draw the fitting result plt.plot(train, label, 'o', label = 'points') plt.plot(train, result, 'r-', label = 'result', linewidth=1) plt.xlabel('x') plt.ylabel('y') plt.show() #4. End the session sess.close()
2. TensorFlow variable management
#Create variables, the two methods are equivalent v1 = tf.get_variable("v1", shape=[1], initializer=tf.constant_initializer(1.0)) # v1 = tf.Variable( tf.constant(1.0, shape=[1]), name='v1' ) print( "Variable name: ", v1.name ) #Generate a context manager, create a namespace, and create variables in the namespace with tf.variable_scope("space", reuse=False): v2 = tf.get_variable("v2", shape=[1], initializer=tf.constant_initializer(1.0)) #Variables created in a namespace will be added with the namespace name before the variable name print( "Variable name: ", v2.name )# :0 is the first result of the operation when the variable is generated #Nested namespace, if the inner layer does not specify the reuse parameter, it will be the same as the outer reuse parameter with tf.variable_scope("space_"): #Get the reuse parameter print( tf.get_variable_scope().reuse ) v4 = tf.get_variable("v2", shape=[1]) print( v4.name ) #Generate a context manager to get the variables that have been created in the namespace with tf.variable_scope("space", reuse=True): v3 = tf.get_variable("v2", shape=[1]) #Create a namespace with an empty name with tf.variable_scope("", reuse=True): #Get the variables under its namespace through the variable name with the namespace name v5 = tf.get_variable( "space/v2", shape=[1] ) #Initialize variables init = tf.initialize_all_variables() #create session with tf.Session() as sess: sess.run( init ) print( sess.run( v1 ) ) print( sess.run( v2 ) ) print (sess.run (v3)) print (sess.run (v4)) print( sess.run( v5 ) )3. TensorFlow model persistence
import tensorflow as tf #Problem: Saving the TensorFlow model #create variable v1 = tf.Variable( tf.constant(1.0, shape=[1]), name='v1' ) v2 = tf.Variable( tf.constant(2.0, shape=[1]), name='v2' ) result = v1 + v2 #Initialize variables init = tf.initialize_all_variables() #declare the tf.train.Saver class for saving the model saver = tf.train.Saver() #create session with tf.Session() as sess: sess.run(init) print( sess.run(result) ) #Save the model model.ckpt.meta saves the structure of the computational graph model.ckpt saves the value of each variable #checkpoint saves a list of all model files in a directory saver.save( sess, "D:/Code_py/Neural/TensorFlow/model/model.ckpt" ) #Problem: Loading a saved TensorFlow model #declare variable v1 = tf.Variable( tf.constant(1.0, shape=[1]), name='v1' ) v2 = tf.Variable( tf.constant(2.0, shape=[1]), name='v2' ) result = v1 + v2 #declare save class saver = tf.train.Saver() #create session declare variable with tf.Session() as sess: #No need to run the initialization process of variables, directly load the saved variables saver.restore( sess, "D:/Code_py/Neural/TensorFlow/model/model.ckpt" ) print( sess.run(result) ) #Problem: Variables are renamed while loading the model #declare variable v1 = tf.Variable( tf.constant(2.0, shape=[1]), name='other-v1' ) v2 = tf.Variable( tf.constant(2.0, shape=[1]), name='other-v2' ) #Load the variable originally named v1 into the variable v1 named 'other-v1' #Load the variable originally named v2 into the variable v2 named 'other-v2' saver = tf.train.Saver( {"v1":v1, "v2":v2} ) #create session with tf.Session() as sess: #No need to run the initialization process of variables, directly load the saved variables saver.restore( sess, "D:/Code_py/Neural/TensorFlow/model/model.ckpt" ) print (sess.run (v1))
Knowledge used:
1. TensorFlow is divided into two stages:
1) Define the calculation in the calculation graph;
2) perform calculations;
2. All data in TensorFlow is represented in the form of tensors. Zero-order tensors are scalars, first-order tensors are vectors, and n-order tensors are n-dimensional arrays. The tensors don't really hold the numbers, they hold the computations of how to get those numbers. A tensor holds three attributes: name, shape, type.
All in all, each node on the computation graph represents an operation, and the result of the computation is stored in a tensor.
3. All random number generators currently supported by TensorFlow:
tf.random_normal() normal distribution
tf.truncated_normal() Normal distribution, the random value deviates from the mean by more than two standard deviations, and the number is re-randomized
tf.random_uniform() uniform distribution
tf.random_gamma() Gamma distribution
4. TensorFlow commonly used optimization algorithms:
tf.train.GradientDescentOptimizer()
tf.train.AdamOptimizer()
tf.train.MomentumOptimizer()
5.tf provides a mechanism to create or obtain a variable by the variable name. Through this mechanism, variables can be used directly by their names in different functions, instead of passing variables into functions in the form of formal parameters. This mechanism is implemented by the tf.get_variable() and tf.variable_scope() functions.
tf.get_variable() is used to create or get variables. When it is used to create a variable, it has the same function as tf.Variable(); when it is used to obtain a variable that has been created, it needs to generate a context manager through the tf.variable_scope() function. In this manager, tf .get_variable() will directly get the variable that has been created.
References:
1. "TensorFlow combat Google deep learning framework"
2. TensorFlow implements linear regression https://blog.csdn.net/lilongsy/article/details/79360458
3. The usage of Saver https://blog.csdn.net/u011500062/article/details/51728830