Hook
More breakout videos from this creator.
Hi, I'm Iris. I'm 19 and I work in industry, data science and machine learning. I'm gonna teach you how to make a linear regression machine learning model from scratch using just pytorch so you better understand how it works. Linear regression is supervised, meaning you hand it the right answers upfront, and all it's doing is drawing the straightest line you can through your data. There are two numbers that control that line, which is W the slope, and B where it sits where X is zero. Every round, pretty much the model does the same four things, where it guesses a line and checks how wrong that line was against the real points, and then work out which way to nudge W and B to be less wrong. You nudge them a small amount at a time, and that's the whole training loop. In my run, W hit 2.99 by epoch 20 and basically just sat there for the next 180 rounds. So B was still crawling towards this final answer at epoch 180 and it hadn't even finished. I wrote two versions of this code so you can basically understand how it looks. I did the first one using just raw code and didn't really use pytorch's ingrained tools. However, the second one I did use, um, nn.module. I think this helps better break down what's actually going on under the hood and you can see how it works. Syntactically, let's go back to the model, right? B isn't hard to learn. It's actually cheaper to get wrong. But W is multiplied by x, and my x values range from minus 10 to 10, which is, uh, what I generated earlier. So a mistake and W gets stretched out across the whole line and it cost the model a lot. So B just shifts the line up or down by a flat amount, right? They're both the same size of a mistake, but B is way smaller in cost. And the model always fixes the expensive stuff first. And by the way, this isn't just a two number quirk. Every model you will ever train from this to literally a large language model with billions of numbers in it. Like, they all have parameters that converge at different speeds for exactly this reason. Some are pulling their weight early, and some are quietly catching up in the background pretty much the whole time. And if you're learning this in pytorch, everything I just described you can write by hand, you can predict, work out the loss, call backwards to get the direction to nudge, subtract the small step pretty much once that clicks. nn.linear() and an optimiser just aren't magic anymore. They're pretty much doing the exact loop that we define in the simple_main() But you can also take away from this is that if a number in your model pretty much looks frozen, you shouldn't panic and assume that it's broken cause you should actually check with. It's actually costing the model to get it wrong