Linear Regression model using TensorFlow

Given below is an implementation of a Linear Regression model using TensorFlow Core API. 

Python




# importing the dependencies
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
 
# Model Parameters
learning_rate = 0.01
training_epochs = 2000
display_step = 200
 
# 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]
 
# Test Data
test_X = np.asarray([6.83, 4.668, 8.9, 7.91, 5.7, 8.7, 3.1, 2.1])
test_y = np.asarray([1.84, 2.273, 3.2, 2.831, 2.92, 3.24, 1.35, 1.03])
 
# Set placeholders for feature and target vectors
X = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
 
# Set model weights and bias
W = tf.Variable(np.random.randn(), name="weight")
b = tf.Variable(np.random.randn(), name="bias")
 
# Construct a linear model
linear_model = W*X + b
 
# Mean squared error
cost = tf.reduce_sum(tf.square(linear_model - y)) / (2*n_samples)
 
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
 
# Initializing the variables
init = tf.global_variables_initializer()
 
# Launch the graph
with tf.Session() as sess:
    # Load initialized variables in current session
    sess.run(init)
 
    # Fit all training data
    for epoch in range(training_epochs):
 
        # perform gradient descent step
        sess.run(optimizer, feed_dict={X: train_X, y: train_y})
         
        # Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, y: train_y})
            print("Epoch:{0:6} \t Cost:{1:10.4} \t W:{2:6.4} \t b:{3:6.4}".
                  format(epoch+1, c, sess.run(W), sess.run(b)))
             
    # Print final parameter values
    print("Optimization Finished!")
    training_cost = sess.run(cost, feed_dict={X: train_X, y: train_y})
    print("Final training cost:", training_cost, "W:", sess.run(W), "b:",
          sess.run(b), '\n')
     
    # Graphic display
    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.show()
 
    # Testing the model
    testing_cost = sess.run(tf.reduce_sum(tf.square(linear_model - y)) / (2 * test_X.shape[0]),
                            feed_dict={X: test_X, y: test_y})
     
    print("Final testing cost:", testing_cost)
    print("Absolute mean square loss difference:", abs(training_cost - testing_cost))
 
    # Display fitted line on test data
    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.show()


Output:

Epoch:   200      Cost:    0.1715      W: 0.426      b:-0.4371
Epoch: 400 Cost: 0.1351 W:0.3884 b:-0.1706
Epoch: 600 Cost: 0.1127 W:0.3589 b:0.03849
Epoch: 800 Cost: 0.09894 W:0.3358 b:0.2025
Epoch: 1000 Cost: 0.09047 W:0.3176 b:0.3311
Epoch: 1200 Cost: 0.08526 W:0.3034 b:0.4319
Epoch: 1400 Cost: 0.08205 W:0.2922 b:0.5111
Epoch: 1600 Cost: 0.08008 W:0.2835 b:0.5731
Epoch: 1800 Cost: 0.07887 W:0.2766 b:0.6218
Epoch: 2000 Cost: 0.07812 W:0.2712 b: 0.66
Optimization Finished!
Final training cost: 0.0781221 W: 0.271219 b: 0.65996
Final testing cost: 0.0756337
Absolute mean square loss difference: 0.00248838

Let us try to understand the above code.

  • First of all, we define some parameters for training our model, like:
learning_rate = 0.01
training_epochs = 2000
display_step = 200
  • Then we define placeholder nodes for feature and target vector.
X = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
  • Then, we define variable nodes for weight and bias.
W = tf.Variable(np.random.randn(), name="weight")
b = tf.Variable(np.random.randn(), name="bias")
  • linear_model is an operational node which calculates the hypothesis for the linear regression model.
linear_model = W*X + b
  • Loss (or cost) per gradient descent is calculated as the mean squared error and its node is defined as:
cost = tf.reduce_sum(tf.square(linear_model - y)) / (2*n_samples)
  • Finally, we have the optimizer node which implements the Gradient Descent Algorithm.
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
  • Now, the training data is fit into the linear model by applying the Gradient Descent Algorithm. The task is repeated training_epochs number of times. In each epoch, we perform the gradient descent step like this:
sess.run(optimizer, feed_dict={X: train_X, y: train_y})
  • After every display_step number of epochs, we print the value of current loss which is found using:
c = sess.run(cost, feed_dict={X: train_X, y: train_y})
  • The model is evaluated on test data and testing_cost is calculated using:
testing_cost = sess.run(tf.reduce_sum(tf.square(linear_model - y)) / (2 * test_X.shape[0]),
feed_dict={X: test_X, y: test_y})

Introduction to TensorFlow

TensorFlow is an open-source machine learning library developed by Google. TensorFlow is used to build and train deep learning models as it facilitates the creation of computational graphs and efficient execution on various hardware platforms. The article provides an comprehensive overview of tensorflow.

Table of Content

  • TensorFlow
  • How to install TensorFlow?
  • The Computational Graph
  • Variables
  • Placeholders
  • Linear Regression model using TensorFlow
  • tf.contrib.learn
  • What are TensorFlow APIs?

Similar Reads

TensorFlow

TensorFlow is basically a software library for numerical computation using data flow graphs where:...

How to install TensorFlow?

An easy-to-follow guide for TensorFlow installation is available....

Computational Graph

Any TensorFlow Core program can be divided into two discrete sections:...

Variables

...

Placeholders

...

Linear Regression model using TensorFlow

TensorFlow has Variable nodes too which can hold variable data. They are mainly used to hold and update parameters of a training model. Variables are in-memory buffers containing tensors. They must be explicitly initialized and can be saved to disk during and after training. You can later restore saved values to exercise or analyze the model. An important difference to note between a constant and Variable is:...

tf.contrib.learn

...

What are TensorFlow APIs?

A graph can be parameterized to accept external inputs, known as placeholders. A placeholder is a promise to provide a value later. While evaluating the graph involving placeholder nodes, a feed_dict parameter is passed to the session’s run method to specify Tensors that provide concrete values to these placeholders. Consider the example given below:...