Linear Regression using Tensor Flow - Basic Example

This code fits a simple Linear Model y = W*x+b where (x,y) are the input data and W - weights and b-biases respectively. This code uses Gradient Descent Optimizer of Tensor flow to perform gradient descent and converge on the Weights and biases

In [1]:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
In [3]:
# Training Data
train_X = np.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
                         7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = np.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
                         2.827,3.465,1.65,2.904,2.42,2.94,1.3])
n_samples = train_X.shape[0]
In [30]:
# Input to Tensorflow
X= tf.placeholder("float")
Y= tf.placeholder("float")

W = tf.Variable(np.random.randn(),"Weight")
b = tf.Variable(np.random.randn(), "Bias")

#Predicted Value
Y_Pred = tf.multiply(X,W)+b

# L2 Loss
#cost = tf.reduce_mean(tf.pow(Y_Pred - Y,2))
cost = tf.reduce_sum(tf.pow(Y_Pred-Y, 2))/(n_samples)
learning_rate = 0.014

optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
In [37]:
init = tf.global_variables_initializer()

#Variables for number of epochs and when to display error
epochs = 1000
displayStep = 50
with tf.Session() as sess:
    sess.run(init)
    
    for epoch in range(epochs):
        for (x,y) in zip(train_X,train_Y):
            sess.run(optimizer,feed_dict={X:x,Y:y})
            
        if(epoch%displayStep == 0):
            c=sess.run(cost,feed_dict={X:train_X,Y:train_Y})
            print("Epcoh = ",epoch,"Cost = ", c,"W =", sess.run(W), "b =" ,sess.run(b))
            
    #Plot the Data and the fitted line. 
    #Note this is being done inside the session inorder to access 'W' and 'b'
    plt.plot(train_X,train_Y,'ro',label="Original Data")
    Pred_Y = sess.run(W)*train_X+sess.run(b)
    plt.plot(train_X,Pred_Y,label="Fitted Line")
    plt.legend()
    plt.show()

    
    
Epcoh =  0 Cost =  11.2238 W = -0.214846 b = 0.561721
Epcoh =  50 Cost =  0.157416 W = 0.265978 b = 0.66016
Epcoh =  100 Cost =  0.156634 W = 0.262814 b = 0.683433
Epcoh =  150 Cost =  0.156083 W = 0.260156 b = 0.702979
Epcoh =  200 Cost =  0.155693 W = 0.257924 b = 0.719398
Epcoh =  250 Cost =  0.155417 W = 0.256049 b = 0.733188
Epcoh =  300 Cost =  0.155223 W = 0.254475 b = 0.74477
Epcoh =  350 Cost =  0.155085 W = 0.253152 b = 0.754498
Epcoh =  400 Cost =  0.154988 W = 0.252042 b = 0.762668
Epcoh =  450 Cost =  0.154919 W = 0.251108 b = 0.769532
Epcoh =  500 Cost =  0.15487 W = 0.250325 b = 0.775298
Epcoh =  550 Cost =  0.154835 W = 0.249666 b = 0.78014
Epcoh =  600 Cost =  0.154811 W = 0.249113 b = 0.784207
Epcoh =  650 Cost =  0.154793 W = 0.248649 b = 0.787622
Epcoh =  700 Cost =  0.154781 W = 0.248259 b = 0.790491
Epcoh =  750 Cost =  0.154772 W = 0.247931 b = 0.792901
Epcoh =  800 Cost =  0.154766 W = 0.247656 b = 0.794924
Epcoh =  850 Cost =  0.154761 W = 0.247425 b = 0.796624
Epcoh =  900 Cost =  0.154758 W = 0.247231 b = 0.798052
Epcoh =  950 Cost =  0.154756 W = 0.247068 b = 0.79925
In [ ]: