In this lecture we are going to do what I call code preparation.
It's like the in between step that will help us get from theory to code so we know what each of the
steps are and we know conceptually how they work.
The next question is what's the right syntax in PI talk to actually make it work.
I like to think of this like how I think of learning programming languages.
I hope everyone in this class knows Python since that's the language we'll be using.
So let's say what I want to do is print the numbers from zero to nine inclusive.
I know how to do that in Python.
It's just for i in range 10 ten print I but let's say that now I want to learn how to do this in Java.
If you know Java.
Just pretend that you don't for a moment.
What's the situation now.
Well I understand the concept.
The only challenges.
What is the Java syntax for it.
I know the concept but I don't know the syntax.
So let's look at the syntax looking at this all the components should feel very familiar to you.
Even if you've never seen it before.
I can see the keyword for that's the same in both languages.
That tells me I'm doing a for loop.
Same concept different syntax.
I see the index variable I again it's the same concept in both languages in a for loop.
I might have an index variable something that iterate as The Loop progresses I see a lower limit and
an upper limit the lower limit is zero and the upper limit is 10 which are inclusive and exclusive respectively.
Same thing in Python except that the lower limit is implicitly zero in Python so you don't have to specify
it unless you want something other than zero.
Finally I see that inside the loop we again have the same concept printing but again just with a different
syntax.
So I hope you get the analogy here which is that no one concepts and syntax are separate things.
You might know the concepts but in different libraries the syntax will be different.
Number two since we already know the concepts our next task is to ask what is the syntax
what I want you to realize at this point is that while this is the most simple scripts from this course
it is also the most important you're going to notice a pattern in this course which is that no matter
how complex what we do is these steps will essentially remain the same.
Remember that what we want to focus on right now is number one.
How do we build the model.
Number two.
How do we train the model.
And number three how do we make predictions using the model
let's start with number one.
How do we build the model.
At this stage it's going to be very easy.
Just one line of code model equals an end linear one one.
What this means is that we want a linear model with one input and one output.
The first argument is for the input size and the second argument is for the output size there are cases
where the input and output might be sizes other than one in one.
But we'll see those later.
Right now we're interested in doing simple linear regression where there is one x axis and one Y axis
next step.
Number two how do we train the model.
If you've ever used other libraries such as KRS and psychic learn you might assume this would be easy
in python.
But unfortunately it doesn't encapsulate all the steps as you would expect.
Luckily thanks to the concepts we just learn that will help us in understanding the syntax.
The first step in training the model is to define a loss and an optimizer as we discussed the loss that
we need for linear regression is the mean squared error and there just so happens to be this object
called MSE loss in PI talk.
Typically we call this object a criterion.
There's one interesting thing to notice here which is that this is a bit of an exercise in what I call
API hunting.
If you think only about the concepts you know the equation for the MSE laws.
If I gave you the whys and the white hats you could calculate this loss yourself but in PI talk it's
not a matter of knowing the formula.
It's a matter of knowing the syntax.
So you need to find the right object that implements the laws you were looking for and then do the right
things with that object at the same time.
It's nice to know a little bit about the concepts so that you actually have some sense of what's going
on.
Next we have the optimizer.
There are many such optimizes in PI torch but the simplest one is called as QED.
When we create the as the object we pass in two arguments the models parameters which we get by calling
the convenient function modeled up parameters.
And the learning rate as a side note as these stands for stochastic gradient descent again in these
lectures I'm not so concerned about what's going on mathematically but rather the syntax.
If you'd like to understand what's going on mathematically I would encourage you to check out the in-depth
sections of this course
All right.
So at this point we've defined a lost object in an optimizer object but we haven't yet done any computation
with them.
As you recall gradient descent involves doing a loop in inside the loop.
You repeatedly apply the gradient descent equation so that's essentially what we want to do right now.
But in PI torch.
So obviously we need a loop.
Usually we call each iteration of the loop an epoch.
Unfortunately it's not so easy as simply translating this one line of mathematical expression into one
line of code.
There are multiple steps in the code that have to be done.
The weirdest part just happens to come first.
We have to call optimizer that 0 grad at this stage.
I like to think of it just as some boilerplate code something that needs to be done.
So just put it there and forget about it.
If you want to know what it does then what we can say is that internally Pi which accumulates gradients
whenever you call lost out backward which is a line that shows up later in this loop.
Personally I would not worry about that because ninety nine percent of the time we'll be doing exactly
what you see here.
And so far none of the examples in this course require you to actually accumulate radiance.
The rest of the steps are relatively straightforward as you know in order to calculate the loss.
We need the predictions.
The Y happens in order to get those we pass them into our model.
This allows us to see the functional aspect of Pi talk.
Even though the variable model is an object of type of linear as you recall that's what we created earlier.
It can be used as if it were a function.
So when we pass in some inputs into the model and call it like a function we get the model outputs.
The next step is to pass the arguments and the outputs into our criteria on the means squared error.
Again this is an object but we're using it like a function.
The next step is to call lost not backward.
Conceptually you can think of this as calculating the gradients.
For example the partial derivative of L with respect to M and the partial derivative of L with respect
to B the final step is to actually apply the gradients using the gradient descent equation as you'll
see later.
There are a lot of variations to the basic gradient descent.
So once you have the gradients there are a few different things you can do with them.
Aside from just this
and you may have noticed something strange in the previous coach that been previously I call the data
X and Y but in the previous code snippet I use the variable names inputs and targets.
Is this just a matter of some trivial renaming.
Or did I really mean to say that they are different things.
In fact they are different things.
When I load in the data I usually call the inputs X and the targets y and usually they are no higher
is but one important detail that we haven't looked at yet is that pi talk does not work with regular
no higher res instead pi torch worked with torch tenses.
These are basically arrays but they are special arrays that are part of the pie towards library rather
than the name pi library.
So they have some special functionality so some conversion has to take place before you pass the data
into your model
in the following example.
For simple linear regression the steps are what you see here.
First we're going to reshape x and y to be end by one instead of just n in machine learning.
We typically assume that our data is two dimensional but the number of samples along the rows and the
number of dimensions along the columns.
This applies even for the targets which is not the case in library such as carers or psychic learn so
be aware of that second Pi which is really picky about data types.
It won't allow you to multiply a flow 32 by a float 64 which is also known as a double.
So you have to be mindful of this also by default.
If you don't specify anything pi touch will create all your variables as float 32 as in num pi.
If you don't specify anything your arrays will be float sixty fours.
So before we create torch tenses we're going to cast our name pi arrays into float 32 the final step
is to call the function torch start from name PI.
This takes as input our number higher res and returns corresponding towards tensor is.
So these are the inputs and targets we were working with in the previous training loop
the final step is to make predictions.
You might think we already did that in the training loop when we called model of inputs but the important
thing to remember is that pi talk only works with torch sensors and the inputs variable is a torch tensor.
It's reasonable to assume then that the output is also a torch tensor.
Of course if you want to do any later processing such as plotting the array with map plot lib you might
want to have a name pi array rather than a torch tensor.
Therefore in addition to actually calculating the output you'll also want to convert it back into an
umpire.
Normally you can do this just using the NUM pi function.
However pi torture also does some stuff behind the scenes where it creates a graph and keeps track of
the gradients in the graph.
Basically you can just call the detach function before you call them and Pi function.
In order to detach that tensor from the graph
all right.
So that was our code preparation for simple linear regression.
I said this before but it's worth repeating while this is the simplest example in the course it's also
the most important because it introduces a lot of new concepts mostly about how to use pi torch in future
examples.
We'll just be repeating these same concepts over and over again.
So you'll get lots of practice.
You've seen that pi torch is quite a bit more complicated than simply instantiating a model calling
fit and then calling predict.