Linear Regression with TensorFlow 2.0

Aadhil imam
4 min readNov 21, 2020

TensorFlow is the one of the world famous Open source numerical computation machine learning Library from Google team.TensorFlow 2.0 has been a major breakthrough in the TensorFlow family. It’s completely new and remodel and also less creepy.

Linear Regression is a supervised machine learning algorithm where the predicted output is continuous and has a constant slope. It’s used to predict values within a continuous range, (e.g. sales, price). In this article we are going to make a prediction using TensorFlow 2.0 .So, open up your code editors and let’s get started!

So start coding important necessary modules.

I have a dataset called data.csv and load the dataset into dataframe called df.After importing the dataset visualize the first 5 rows of the dataframe.

This is how exactly dataframe look like and i have a feature called X and have a target variable called Y . In this article we are going to predicted Y related to the X feature.

Now lets the plot a scatter plot the X values correspond to the Y values.

As the scatter plot we can see the when increasing the X values corresponding to the Y values are increased.As the above plot we going to build the linear regression best fit line for the X and Y.

So lets get started to build Linear Regression model using TensorFlow 2.0.As first we create the instance of the model then we add a layer with one hidden unit. We take a X as our input Y is as Output.

First of all we use TensorFlow Sequential API to build the model .Then we define the dense layer in that dense layer we have one input feature so we use one input as one as well as We use One layer for this model.Next part is compute the loss how far is may actual value from the predicted value in case of Regression we use Mean Squared Error for finding the loss.Afterwards we we need to use a optimizer there are so many Optimizer available in TensorFlow and we used the Adam Optimizer for this task.

After Running the model let us understand the summary of the model.

From above picture as you can clearly see we have a output of one dimension that is why output shape also (None , 1 ) and its specify the total parameters and total trainable parameters.

Now model structure is ready lets we train the model for X and Y using 300 epochs in order to compute the most optimum values of weights for the combination of data what we have.

After running the fit function we can see the model is compute for the 300 models.Finlay training of 300 epochs we have converge to a small loss value.The let us the loss as well.

loss

As the picture we can see the initial epochs loss are very large which has been breakdown from time and reached the optimum values from train.

Now we take the same training data values pass trough the model we will get the output value from the corresponding X value.

Now we have prediction(df[prediction])and actual value Lets plot the scatter plot and best fit line for the X and Y.

best fit line

As the picture scatters are the training data and the red line shows the actual predicted line this is how we can build Linear Regression using TensorFlow 2.0.

--

--