Linear Regression in Pytorch

Helik Thacker
2 min readJun 6, 2020

--

We will see how to implement a linear regression model using Pytorch. We use the following dataset: https://www.kaggle.com/mirichoi0218/insurance

First we have to install a few packages if not installed: torch, pandas, matplotlib and seaborn. One can easily do it using Anaconda.

1 Parsing the Dataset

Pytorch provides download_url using which we can get CSV file.

Then we can use Pandas to load the data into a pandas dataframe using the read_csv() function and then we can visualize the dataset using the seaborn package which is built over the matplotlib package.

We then have to prepare the dataset. We need the data as Pytorch tensors so that we can use that in our model which we will make. We use the dataloader so that we can extract the data in batches. This is especially helpful for large datasets.

The flowchart of converting a Pandas Dataframe to Pytorch Dataset and then creating a Pytorch DataLoader

2 Creating and Training the Linear Regression Model

Now that we have the dataset ready, we code the linear regression model. We do so by extending the nn.Module class. We can use this as a template for many other models as well. We also need a evaluation criteria for checking how the model is performing. We have used the l1_loss as our loss function.

Now we have a linear regression model with us. The next step is to train the model so that we can get more accurate results. We have to carefully choose a learning rate and the number of epochs for which the training will happen. For the number of epochs we can run till we have a satisfactory result or if the model has converged to an optimum. For the learning rate, we have to be careful not to choose a very high value or a very low value. High values will make the model oscillate and the low values will take a huge number of epochs for the model to converge.

After the training is complete we can use the model to make predictions on unseen data and see how it performs.

There you have it! It is not hard to have a linear regression model up and running using a few lines of code.

The entire code for this blog can be found at https://jovian.ml/thackerhelik/02-insurance-linear-regression

If you want to learn more, check out the course Deep Learning with PyTorch: Zero to GANs. It is a great place to start with Pytorch.

--

--

Helik Thacker

I love Deep Reinforcement Learning and applying it to various problems especially to games.