Implementation of Simple Linear Regression Training Model by TensorFlow

Simple Linear Regression Training Model Code

import tensorflow as tf
import os

os.environ['TF_CPP_MIN_LOG_LEVEL']='2'"max_step", 200, "Step Number of Training Model")  # Training steps"model_path",
                           "Path of Model Preservation+Model name")            # Define the path of the model
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
    # 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:  # 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):
            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 =
        # Model preservation, FLAGS.model_path)

    return None

def main(argv):
    print("This is main function")

if __name__ == "__main__":                  # Start main(argv) function by

The cmd command line executes the file TensorFlow for linear

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']
 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.

Keywords: Session Python

Added by Thethug on Sun, 06 Oct 2019 21:07:00 +0300