TF LiR: Linear regression algorithm for machine learning based on Tensorflow

 

 

directory

The output

Code design


 

 

 

 

The output


Code design

# -*- coding: utf-8 -*-

LiR for TF: Linear regression algorithm for machine learning based on Tensorflow
import tensorflow as tf
import numpy
import matplotlib.pyplot as plt

rng =numpy.random

# Parameter setting
learning_rate=0.01
training_epochs=10000
display_step=50        Output every 50 iterations
# Training dataTrain_X = numpy. Asarray ([...] ) train_Y = numpy. Asarray ([...] ) n_samples=train_X.shape[0]
print("train_X:",train_X)
print("train_Y:",train_Y)  

# set placeholder
X=tf.placeholder("float")
Y=tf.placeholder("float")

# Set the weights and biases of the model. Variable is used because it is constantly updated
W=tf.Variable(rng.randn(),name="weight")
b=tf.Variable(rng.randn(),name="bias")

Set linear regression equation LiR: w*x+b
pred=tf.add(tf.multiply(X,W),b)
cost=tf.reduce_sum(tf.pow(pred-Y,2))/(2*n_samples)  # Set cost as mean square error, namely reduce_sum function
optimizer=tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) The minimize function automatically corrects W and B by default

init=tf.global_variables_initializer() Initialize all variables during session operation
# Start training
with tf.Session() as sess:
    sess.run(init)                        Run the initialized variable
    for epoch in range(training_epochs):  Enter all training data
        for(x,y) in zip(train_X,train_Y):
            sess.run(optimizer,feed_dict={X:x,Y:y})
            
            Print logs for each iteration, every 50
            if (epoch+1) % display_step ==0:
                c=sess.run(cost,feed_dict={X:train_X,Y:train_Y})
                print("Iteration Epoch:"."%04d" % (epoch+1),"The decreasing value cos (t) ="."{:.9f}".format(c),
                      "W=",sess.run(W),"b=",sess.run(b))
    print("Optimizer Finished!")
    training_cost=sess.run(cost,feed_dict={X:train_X,Y:train_Y})
    print("Training cost=",training_cost,"W=",sess.run(W),"b=",sess.run(b))
    # drawing
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.subplot(121)
    plt.plot(train_X, train_Y, 'ro', label='Original data') 
    plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
    plt.legend() 
    plt.title("LiR of TF: Original Data")
    
    
    # Test sample
    test_X = numpy.asarray([6.83.4.668.8.9.7.91.5.7.8.7.3.1.2.1]) 
    test_Y = numpy.asarray([1.84.2.273.3.2.2.831.2.92.3.24.1.35.1.03])
    print("Testing... (Mean square loss Comparison)") 
    testing_cost = sess.run(tf.reduce_sum(tf.pow(pred - Y, 2))/(2 * test_X.shape[0]), 
                            feed_dict={X:test_X,Y:test_Y}) # same function as cost above 
    print("Testing cost=", testing_cost) 
    print("Absolute mean square loss difference:".abs( training_cost - testing_cost)) 
    # drawing
    plt.subplot(122)
    plt.plot(test_X, test_Y, 'bo', label='Testing data') 
    plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
    plt.legend() 
    plt.title("TF LiR: Testing Data")
    plt.show() 
   
Copy the code
Iteration times Epoch: 6300 Decreasing value cost= 0.076938324 W= 0.25199208b = 0.8008495...... Cost = 0.076965131 W= 0.24998894 b= 0.80145526 Epoch: 10000 Cost = 0.076942705 W= 0.25047526b = 0.80151606 Epoch: Cost = 0.076929517 W= 0.25114807 b= 0.801635 Epoch: Cost = 0.076958008 W= 0.25011322 b= 0.8015234 Epoch: 10000 Cost = 0.076990739 W= 0.24960834 b= 0.80136055 Optimizer Finished! Training cost= 0.07699074 W= 0.24960834 B = 0.80136055 Testing... (Mean square loss Comparison) Testing cost= 0.07910849 Absolute Mean square loss difference: 0.002117753Copy the code