PyTorch made Easy : Linear regression with PyTorch

Mustafa Alahmid
The Startup
Published in
2 min readJun 4, 2020

--

In this article we are going to explain the linear regression and we will implement it from scratch with PyTorch.

Dataset:

We will create a small dateset for regression:

Now we have created the dataset that contain 100 rows with 3 columns of features and 1 column of label.

We can check the shape:

Now let’s see some samples of the dataset and labels:

Now we want to change the dataset into tensor:

Now we create the data loader and batch, data loader will load a data with size equal to batch size.

The batch is the size of data that will be feed into the model in each epoch.

Here we put the size equal to 100 it will take 100 rows and feed the model and next epoch will take the same and each time it will shuffle the data.

Building the model, using nn.model function.

The parameters are the numbers of features columns and number of labels columns.

PS* the features columns in ML called attributes.

Make predictions using the model without training:

Then create the loss function and calculate the loss

The loss function is MSE(mean Square Error)

Then we define the optimizer:

the used optimizer is SGD (Stochastic gradient descent).

Setting the learning rate (lr), weight and bias of the model.

Now we create the fit function :

1-Generate predictions then calculate the loss

2-Compute gradients w.r.t the weights and biases and adjust the weights by subtracting a small quantity proportional to the gradient

3-Reset the gradients to zero

Finally we will start train the model and keep training it as long as the loss is decreasing or reach to zero or a value near the zero.

Now we can check the predictions:

we can see the results are close to the ground truth

Now we have build a Linear Regression model using PyTorch

Exercise: make a linear regression using real dataset

Thank you and see you in the next article

--

--

Mustafa Alahmid
The Startup

Master degree in CS, interested in Machine learning and deep learning