Linear regression model – building and training

According to our explanation of linear regression in the Chapter 2, Data Modeling in Action - The Titanic Example we are going to rely on this definition to build a simple linear regression model.

Let's start off by importing the necessary packages for this implementation:

import numpy as np
import tensorflow as tf
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (10, 6)

Let's define an independent variable:

input_values = np.arange(0.0, 5.0, 0.1)
input_values
Output:
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ,
1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1,
2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2,
3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , 4.1, 4.2, 4.3,
4.4, 4.5, 4.6, 4.7, 4.8, 4.9])
##You can adjust the slope and intercept to verify the changes in the graph
weight=1
bias=0
output = weight*input_values + bias
plt.plot(input_values,output)
plt.ylabel('Dependent Variable')
plt.xlabel('Indepdendent Variable')
plt.show()
Output:
  
Figure 11: Visualization of the dependent variable versus the independent one

 

Now, let's see how this gets interpreted into a TensorFlow code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset