Simple Linear Regression Training Model Code
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' tf.app.flags.DEFINE_integer("max_step", 200, "Step Number of Training Model") # Training steps tf.app.flags.DEFINE_string("model_path", "./ckpt/linearregression", "Path of Model Preservation+Model name") # Define the path of the model FLAGS = tf.app.flags.FLAGS # Define Getting Command Line Parameters def linear_regression(): with tf.variable_scope("dataset"): # Setting namespaces for variables X = tf.random_normal(shape=(100,1), mean=0.5, stddev=1) Y_true = tf.matmul(X, [[4.0]]) + 3.0 # Establishing Linear Model with tf.variable_scope("linear_model"): weights = tf.Variable(initial_value=tf.random_normal(shape=(1,1)), name="weights") bias = tf.Variable(initial_value=tf.random_normal(shape=(1,1)), name="bias") Y_predict = tf.matmul(X, weights) + bias with tf.variable_scope("loss"): loss = tf.reduce_mean(tf.square(Y_predict-Y_true), name="loss") with tf.variable_scope("gradient_optimiter"): optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss) # Collecting variables tf.summary.scalar("loss",loss) tf.summary.histogram("weight",weights) tf.summary.histogram("bias",bias) # Merging variables merge = tf.summary.merge_all() # Initialize all variables init = tf.global_variables_initializer() # Create a model save and load instance saver saver = tf.train.Saver() with tf.Session() as sess: sess.run(init) # Assignment of initial values to variables print("Initialized weights are%f,Offset to%f" % (weights.eval(), bias.eval())) # Model loading # saver.restore(sess, "./ckpt/linearregression") print("Weight is%f,Offset to%f" % (weights.eval(), bias.eval())) # Create event files for Tensorboard to show the whole process of training model file_writer = tf.summary.FileWriter(logdir="./summary/",graph=sess.graph) # Training model for i in range(FLAGS.max_step): sess.run(optimizer) print("The first%d The loss of step is%f,Weight is%f, Offset to%f" % (i+1, loss.eval(), weights.eval(), bias.eval())) # Add the collected and merged variables to the event file for display on Tensorboard summary = sess.run(merge) file_writer.add_summary(summary,i+1) # Model preservation saver.save(sess, FLAGS.model_path) return None def main(argv): print("This is main function") print(argv) print(FLAGS.model_path) linear_regression() if __name__ == "__main__": tf.app.run() # Start main(argv) function by tf.app.run()
The cmd command line executes the file TensorFlow for linear regression.py
The cmd command line can reassign max_step, model_path
E: Tensorflow > workon AI Must be switched to a virtual environment (ai) E: Tensorflow > python. / 09-TensorFlow for linear regression. py --max_step=100 === cmd execution results:=========================================== This is the main function. ['. / TensorFlow for linear regression. py'] ./ckpt/linearregression Initialization weights are -0.996474 and bias is 1.092397. The weight is -0.996474 and the bias is 1.092397. The loss of the first step is 38.175697, the weight is -0.828843, and the bias is 1.183127. The second step has a loss of 38.077343, a weight of -0.671796 and a bias of 1.268724. The third step has a loss of 42.033585, a weight of -0.526859 and a bias of 1.366900. ... The loss of step 98 is 0.180080, the weight is 3.587485, and the bias is 3.236459. The loss of step 99 is 0.224762, the weight is 3.594254, and the bias is 3.237265. The loss of step 100 is 0.146797, the weight is 3.602060, and the bias is 3.237001.