Introduction to PyTorch and Linear Regression
Updated: Nov 21, 2022

Would you believe that machine learning with PyTorch can be understood and implemented by someone with high school knowledge? If you said no, let me explain to you the prerequisites that you need to be able to understand the following concepts:
1) Linear algebra
2) Some calculus
3) Fundamental coding (loops and basic syntax)
4) Introductory-level probability
Even if you don't have these pre-requisites, don't sweat it. A lot of this stuff can be learned on the fly. The more you know, however, the less time you will have to spend dissecting things. Let's get talking about PyTorch!
What is PyTorch? It is a Python-based machine learning framework. The framework part just tells us that the more intricate details of implementing machine learning have been taken care of for us. It provides an abundance of tools, functions, and definitions that allow us to implement things with less code.
Why PyTorch over other frameworks and languages? PyTorch provides us with GPU acceleration, which allows us to train models much faster. This is required when working with images, where we will be working with thousands of pixels as input. PyTorch also has integrations that work very well with other well-known Python libraries such as NumPy and Pandas. These libraries are extremely useful for importing datasets and cleaning them prior to training.
Who invented PyTorch? Meta AI is credited for the release of PyTorch. You might recognize the name Meta, as this is one of the popular FAANG companies previously named Facebook. They initially released it on September 16th, 2016.
Now that we have some introductory knowledge of PyTorch, let's preface some things. PyTorch is an exciting tool that allows people to dive into machine learning without having to spend time ingesting tons of complex pre-requisite knowledge. If you're like me, a very young passionate computer science student looking to get into ML, there are a few things you should know.PyTorch's strengths are its simplicity and ease of implementation. You will come to learn that a lot of things in PyTorch are as simple as googling certain functions you want to use and figuring out their inputs and outputs. You rarely have to know exactly how the gears inside of these functions turn. This is also one of its weaknesses. The process of using something when you don't know how it works is my loose definition of black boxing. If you really want to become a successful and knowledgeable machine learning researcher or practitioner, you won't do it by black boxing. You will one day need to learn these things that PyTorch takes away, so please keep that in mind as you proceed to learn. Let's talk about one of the first types of problems we solve with machine learning. It is called linear regression.
Linear regression is, in my opinion, the introduction to machine learning and PyTorch.which is why it is often the first chapter or lesson of many machine learning courses. It essentially looks at numerical inputs and one or more numerical outputs. Remember in middle school when you learned y = mx + b? Linear regression looks to find the m (slope) and b (y-intercept) of all of our inputs, x1, x2, x3, and so on. In doing this, it can then predict the outputs, which is exactly the opposite of what we traditionally did with computers. We usually give computers the rules and inputs they need to calculate our outputs. Now we are giving computers inputs and outputs and asking them to give us the rules? Ta-da! That is machine learning in a nutshell, and also why we say the computer "learns."
You probably have a better idea of what linear regression is, but let's get into the nitty-gritty details. I mentioned that linear regression is modeled after y=mx+b. However, you will notice that we do not use m in linear regression. We actually use W, but instead of referring to it as a slope, we refer to it as a weight.It is the same concept, but essentially, it is looking at each input and asking itself, "How much does this number affect the output?" In the real world, certain things do affect outputs, and certain things don't. A clear example would be crop yield. How much do you think temperature affects crop yield? Now, you probably said a lot because crops don't grow as much, if at all, in the winter, but they thrive in the spring and summer. Now ask yourself this: "How much do you think the number of convenience stores in the area affects crop yield?" Probably not at all, but you could think of some bizarre ways that convenience store frequency does affect it slightly. Now you have a better idea of what weights are. We don't call b the y-intercept in linear regression. Instead, you will hear the term "bias." The bias says, "No matter the conditions, there will always be some sort of offset amount of crop yield that doesn't change despite the inputs." Through finding our weights and biases, we can make a model that makes predictions on real-world numerical input and output problems. Now that you have this conceptual knowledge, ask yourself, "What other real-world problems could be modeled by linear regression?"
How does linear regression work with machine learning? We will discuss some of the concepts of machine learning that we use to create linear regression models. You will find that these concepts are fairly simple and carry over to everything else you will learn later on. Firstly, when we say "model," it is really just a function that takes inputs, multiplies them by each of their individual weights, and finally adds their biases. The model is responsible for randomly creating weights and biases to make initial "guesses," since these won't be fine tuned to produce accurate predictions.
How do we fine-tune these weights and biases? We do a couple things. We have to define a continuous and differentiable "loss" or "cost" function. This function will take in model predictions, or outputs, and our target outputs (the correct answer) and process them to produce a number called loss. This function has to be continuous and differentiable. The reason this function has to be differentiable leads me onto our next concept, which is referred to as "gradient descent."
What is gradient descent? First, let's discuss what a gradient is. A gradient is essentially a multivariable or multi-dimensional derivative. Now I know that when I first saw this, I thought to myself, "I thought I only needed to know some high school calculus." You are right. Gradients are one of those topics that are technically not covered in depth until later Calculus classes, when multivariable calculus is discussed.However, the applications you are using it for are so simple that if you can take a basic derivative that you learn in the first c
ouple weeks of a calculus class, you can learn this too.
Now gradient descent is the act of taking a derivative and "descending" towards the minima of the function. It's pretty self-explanatory, but let's not miss the bigger picture. Why is this useful in teaching a ML model how to fine-tune weights and biases? Well, if you take a bunch of data points and calculate losses for all of them, You will be provided with a continuous function.If the goal of this problem is to minimize the loss, which it is because a lower loss translates to more accurate predictions, Taking gradients of the loss function allows you to get the slopes of your weights; from here, you can adjust those slopes in whichever direction allows the overall
loss to decrease. Let's summarize really quickly.
We import our data. Create a model that randomly initializes weights and biases. Plugging our input data into that model gives us predictions. We define a continuous loss function that includes our predictions and targets. We calculate our loss. We take a gradient and then optimize our weights and biases to move in a direction that allows our overall loss to decrease. If we do this enough, we can get our losses as low as possible and increase prediction accuracy over time. That is the whole premise of machine learning, and you will see that these concepts never go away. The problems just get more difficult and require us to be more creative with how we design our models. I will mainly cover the concepts of how machine learning works. I will provide documentation for the PyTorch implementations for these things if you would like to read more. I will also refer to the resource that taught me everything I know about machine learning. That resource is called Jovian.ai and their resources are free!
Jovian will teach you more in depth about the implementations of these concepts. How to set up your machine or a cloud virtual environment to code PyTorch.




Comments