Machine Learning: Linear Regression Example With TensorFlow In Python

November 10, 2018

“white robot near brown wall” by Alex Knight on Unsplash

Machine Learning: Linear Regression Example With TensorFlow In Python

Linear regression is the most basic form of machine learning. In linear regression we attempt to determine the best fitting line for our data. In the proceeding article, we’ll go through a simple example using python.

As always, we’ll need to import the following libraries.

import tensorflow as tf  
import numpy as np  
import pandas as pd  
import matplotlib.pyplot as plt

In the proceeding example, we’ll use a data set containing the relationship between salary and years of experience.

dataset = pd.read_csv('Salary_Data.csv').values
x_train = dataset[:,0]
y_train = dataset[:,1]

Next, we’ll define the Hyperparameters.

epochs = 200
learning_rate = 0.01

To start, we’ll initialize a random weight and bias.

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
W = tf.Variable(np.random.randn(), name='weights')
B = tf.Variable(np.random.randn(), name='bias')

In linear regression, our model is simply the equation for a line.

y_predicted = X * W + B

Recall how we must pick a loss function that should be minimized during the training process. In this case, we’ll use MSE or mean square error. To calculate the mean square error, we sum up all the arrows indicating loss, square the result, then take the average. We square the result because we don’t want arrows that point down to cancel out with arrows pointing up.

``` mean_square_error = tf.reduce_sum((Y - y_predicted) ** 2) / x_train.size ```

In this example, we’ll make use of gradient descent to minimize our loss function.

optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(mean_square_error)

The proceeding code uses TensorFlow’s high level API to train our model.

with tf.Session() as sesh:
 sesh.run(init)
 for epoch in range(epochs):
 for x, y in zip(x_train, y_train):
 sesh.run(optimizer, feed_dict={X: x, Y: y})

We can view how the mean square error, weight and bias changes every 10 epochs.

if not epoch % 10:
 mse = sesh.run(mean_square_error, feed_dict={X: x_train, Y: y_train})
 w = sesh.run(W)
 b = sesh.run(B)
 print(f'epoch: {epoch} mean square error: {mse} weight: {w} bias: {b}')

Finally, we can plot the best fitting line obtained by training our model.

weight = sesh.run(W)
bias = sesh.run(B)
plt.scatter(x_train, y_train)
plt.plot(x_train, weight * x_train + bias, color='red')
plt.show()
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

dataset = pd.read_csv('Salary_Data.csv').values

x_train = dataset[:,0]
y_train = dataset[:,1]

epochs = 200
learning_rate = 0.01

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

W = tf.Variable(np.random.randn(), name='weights')
B = tf.Variable(np.random.randn(), name='bias')

y_predicted = X * W + B

mean_square_error = tf.reduce_sum((Y - y_predicted) ** 2) / x_train.size

optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(mean_square_error)

init = tf.global_variables_initializer()

with tf.Session() as sesh:
    sesh.run(init)

    for epoch in range(epochs):
        for x, y in zip(x_train, y_train):
            sesh.run(optimizer, feed_dict={X: x, Y: y})
        
        if not epoch % 10:
            mse = sesh.run(mean_square_error, feed_dict={X: x_train, Y: y_train})
            w = sesh.run(W)
            b = sesh.run(B)
            print(f'epoch: {epoch} mean square error: {mse} weight: {w} bias: {b}')
    
    weight = sesh.run(W)
    bias = sesh.run(B)
    plt.scatter(x_train, y_train)
    plt.plot(x_train, weight * x_train + bias, color='red')
    plt.show()

Cory Maklin
_Sign in now to see your channels and recommendations!_www.youtube.com


Profile picture

Written by Cory Maklin Genius is making complex ideas simple, not making simple ideas complex - Albert Einstein You should follow them on Twitter