Deep Learning with PyTorch- Day 2

Manav Khadka
PyTorch: Zero to GANs
4 min readMay 26, 2020

If you are new to this blog, THIS is the place where you should actually start.This is the third article of the series “Deep Learning with PyTorch”. You will find links to all the articles of the series, my profiles, codes and general information of previous as well as next articles on the bottom.

Getting into ML/DL isn't an easy thing so that’s why my blog will contain the theory that I read throughout my process of learning, not just codes and examples and resources that my tutor Aakash N S teaches and provides us. I will also be blogging on essential concepts so that it would make the reading and learning process smooth.

2020/5/26 I learned about Linear Regression — “a foundational algorithm of Machine Learning”. I learned how to create a model that predicts crop yield field for apples and oranges(target variables) by looking at the average temperature,rainfall, and humidity(input variables or features) in a region in Pokemon Land.

Before heading Deep into this topic here are some prerequisites I want you to learn,which I did and I’m glad.
1) Learn Calculus,Vector,Matices and Linear Algebra (Basic only needed)
2) Have basic knowledge on Jupyter Notebook (optional)
3) Have PyTorch, matplotlib, and numpy installed (optional)
For 2 & 3 you can use binder.org to run your notebooks too.

Datasets to Train.

From the Data above we clearly can see the yield of apples and oranges depends upon the temperature,rainfall & humidity to some extend.You may be thinking… Linear Regression??? What the heck is he talking about. What is this? Is this a python library? Is this a method ? Let’s hop into Linear Regression for a while now.

What is Linear Regression?

Linear regression is a Supervised Learning algorithm ( a type of Machine Learning Algorithm) which goal is to predict continuous,numerical values based on given data input. From geometrical perspective, each data sample is a point and Linear regression tries to find the parameters (slope and intercept) of the linear function so the distance between all the points and the line is minimum i.e. in simple words Linear Regression tries to find the best fit equation from our training data set which we can use later for other untrained dateset to predict out output.

Why Linear Regression?

Some of the reader may be wondering why we are starting with basic machine learning algorithm like Linear Regression. It’s justifiable to start from here. It is a very plain algorithm so reader can grasp and understand the fundamentals of machine learning concepts. It will also be quite easy to learn about Logistic Regression and many more concepts coming up in the future blogs.

Visualizing Data

It is very important in deep learning (or any similar field) to understand the structure of Data.The more attributes the more harder it is to understand about the data. Lets us for now use scatter plot to display the relationship between target and training features.

NOTE:
Depending upon what is necessary to show, some other types of visualization (eg: box plot or clustering) and techniques could be useful.

Lets plot 2-D scatter plot (using matplotlib and numpy) of our yield of apple with our features(Temperature, Rainfall and Humidity). Note that for simplicity only apple is used. You can try yourself with oranges.

We can observe the linear dependency between features and our target.It is great because if it was not linear the case would have been different. If it was for example — exponential — it would have been hard for us to fit a line through all points and different algorithm should be used. Thus, in this particular problem Linear Regression can be used to find a mathematical function which will try to estimate the yield of apples and oranges based on its attributes.( of outside Datasets)

In a linear regression model, each target variable is estimated to be a weighted sum of the input variables, offset by some constant, known as a bias :

yield_apple  = w11 * temp + w12 * rainfall + w13 * humidity + b1
yield_orange = w21 * temp + w22 * rainfall + w23 * humidity + b2
Block diagram of Linear Regression Model

The learning part of linear regression is to figure out a set of weights w11, w12,... w23, b1 & b2 by looking at the training data, to make accurate predictions for new data (i.e. to predict the yields for apples and oranges in a new region using the average temperature, rainfall and humidity). This is done by adjusting the weights slightly many times to make better predictions, using an optimization technique called gradient descent.

Here is the detail of the linear regression model code.

Your feedback are appreciated. Thank you for reading!!

Important links:

  1. Start here [WELCOME to my Deep Learning Journey.]
  2. Jovian profile
  3. Instagram profile
  4. Week 1 plan

--

--

Manav Khadka
PyTorch: Zero to GANs

Welcome!Er. Manav Khadka. Associate Software Engineer@Ayat Digital. Co-Founder@Baliyo Ventures.Passionate about robotics and Deep Learning.Let's learn together!