00:00Every big AI model you've heard of, from chatbots to image generators, is trained with tools like PyTorch. In the
00:09next five minutes, you'll understand exactly how it works, from a single number all the way to a neural network
00:16that learns. Let's start with the one idea everything else is built on, the tensor. A tensor is simply a
00:26container for numbers. A single number
00:30is a zero-dimensional tensor called a scalar a list of numbers is a one-dimensional tensor a vector a
00:40grid of numbers is two-dimensional a matrix stack matrices together and you get three dimensions like a color image
00:49with red green and blue channels every tensor has a shape which tells you its size along each dimension and
00:58a data type
00:59such as 32-bit floats. And with one line, dot to CUDA, the same tensor moves onto a graphics card,
01:07where thousands of cores can work on it at once. Once you have tensors, you can do maths with them.
01:16Adding two tensors simply adds each pair of matching elements. Matrix multiplication is different. Each output number is a row
01:26multiplied by a column and summed up.
01:30This one operation is the heart of every neural network. PyTorch also broadcasts. It stretches a smaller tensor to fit
01:40a bigger one, so adding a single row to a whole matrix just works. Now for the clever part, autograd.
01:49When you create a tensor with requires grad set to true, PyTorch quietly records every operation you do with it.
01:58Here x is multiplied by w, b is added, and the result is squared to give a loss. That record
02:07is called a computation graph. Call dot backward on the loss, and PyTorch walks the graph in reverse, using the
02:16chain rule to work out how much each input affected the result. These numbers are gradients, they tell us which
02:24way to nudge each weight to make the loss smaller.
02:29Imagine the loss as a landscape and our model as a ball standing on it. The gradient points uphill, so
02:37we take a small step in the opposite direction. Repeat that again and again, and the ball rolls down towards
02:45the lowest point. The size of each step is the learning rate, too small and training crawls, too large and
02:54the ball overshoots and bounces around.
02:59is just layers of these weights stacked together. In PyTorch, you build one by subclassing nn.module. You define your
03:09layers, such as linear layers, in the constructor. Then the forward method describes how data flows through them, with an
03:18activation like ReLU in between, so the network can learn curves, not just straight lines. Training comes down to five
03:28lines.
03:29repeated in a loop. First, the forward pass makes a prediction. Second, a loss function measures how wrong it was.
03:39Third, we clear out the old gradients. Fourth, backward computes the new ones. And fifth, the optimizer takes a step,
03:49updating every weight. Let's watch it work. These dots come from a noisy sine wave, and our small network starts
03:58out knowing nothing.
04:00As training runs, its prediction bends to fit the data. And on the right, the loss falls, epoch after epoch.
04:10That's real Pi Torch, trained on this laptop. So, to recap, tensors hold the data, Autograd computes the gradients, and
04:21the optimizer uses them to improve the model, one small step at a time. That's Pi Torch. If this helped,
04:29Subscribe to the AI IT Guy and in the next video we'll train a model on real images.