Machine Learning Building Blocks: Linear Regression

Linear regression from scratch using Pytorch and Autograd

Photo by Ryan Quintal on Unsplash
A tensor can be used in place of a matrix as input to most models and work well for when your data is highly dimensional or in the case of images where you need to specify pixel intensity as well as colour.

The Normal Equation

Automatic Gradient Calculations

Torch has a tensor and tensor manipulation API that is highly inspired by Numpy. You can effectively translate directly, or almost directly, between the two libraries.
The computational graph that is created for all of the elements in the multiplication of 2*1 where the gradient was required.
This is the same general equation of the linear equation y = W*X + B, only we are using matrices instead of the single values about. All of the same operation occur, multiplication, addition, only the structure of the data is different.
Here we are using the Numpy array method to create the input and target data, torch.Tensor is also an option for this exact same thing. The translation from package to package is seemless too as the Pytorch team took inspiration for their api from Numpy.
Converting from Numpy arrays to Torch tensors is trivial with the from_numpy() method.

Using the NN Built-in Methods of PyTorch



BBC Data Scientist

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store