In chapter 1 and chapter 2 , we got an introduction to PyTorch, some interesting functions used in PyTorch, different algorithms used in machine learning and a brief but solid introduction to linear regression. In this chapter we are going to build a Linear Regression model from scratch that is, without the use of any PyTorch built-ins. Without further ado, let’s begin.

Image for post

Problem Statement

Predict the yield of apples and oranges (dependent or target variables) by observing the average temperature, rainfall and humidity (independent or predictor variables ) for a region.

Image for post

We know that in linear regression target variables are considered as the weighted sum of independent variables, offset by some constant called bias (we add this offset so that when all the independent variables become zero the output should not become zero).

This statement can be represented mathematically as :-

Image for post

These equations show that dependent variables share a linear relation with dependent variables.

Learning Part of Linear Regression

We should find the set of weights and bias that is, w11,w12,…w23 and b1 and b2 by analyzing training data to predict the yield of apples and oranges of a new region based on avg. temp. , rainfall and humidity. This is done by adjusting the weights and biases slightly.

Training data can be considered as two numpy arrays (matrices), input and target one row per observation.

Image for post

Generated by Author

We now convert the numpy arrays to tensors for this purpose we use torch.from_numpy(), this method takes a numpy array as input and output tensor.

Image for post

Generated by Author

Weights (w11,w12,…w23) and biases (b1,b2) can be represented as matrices and first row and first bias element is used to predict the yield of apples and the second row and element the yield of oranges.

Image for post

Generated by Author

In this code snippet we create two tensors w and b using torch.randn() which will act as our weight and bias matrices.The torch.randn() function creates two matrices with element picked randomly from a normal distribution with mean=0 and standard deviation=1.

#deep-learning #linear-regression #scratch #tutorial #machine-learning #deep learning

Chapter 3 — Linear Regression From Scratch
1.30 GEEK