Now, let's take a look at the most important part of this PyTorch project that we're doing.
This is called training the model.
Now you can see how we created a prototype object model class.
We declared optimizes data load is lost, criterion all of those things.
But how do we know use all of those things to train our model?
Because that's the essential part of what we're trying to do.
So I'm going to take a look at this flowchart Typekit here, and we'll look at a quarter in each line
afterward because mainly it's good to look at this in this simplified way because the code for this,
as you can see here, can be a bit confusing.
And these comments, even though they're quite helpful, added, I put in for you guys, they can add
some bulk to the code, which could seem intimidating.
So let's start with the other top.
We're using these batches.
Remember what data the returns can return in batches of 128 of sites 128.
So we get 128 samples of data along with the labels.
Well, that's what we do in the first phase of training.
We get so many this, we need to get all data and that sort of mini batch.
So we get about 128 inputs on the labels.
Next, we have to initialize our videos to zero for the first run of this.
And afterwards, after each run, we'll be optimizing these weights.
So set our great gradients to zero value here.
And next, what we do, we found propagate our own batches or batch of data, but to true network,
get the outputs past outputs to get a loss.
We'll use opposite outputs to get lost.
Then once we have that loss, we propagate through and network and then we update gradients using the
Optimizely.
And you can see this cut each part of the code to show you what part of the code does what.
So you understand fully.
So just to take a look at this here to get the data, batch what you need to use.
We just enumerate, you see, enumerate function for the looping to get the data loader and that puts
the data at.
But each batch is data in a loop, so we can.
Data consists of two parts, consists of the inputs and levels.
So we just use inputs and labels throughout the loop to feed into the neural to CNN.
Next, we can use Optimizer 2.0 credit as zero as a gradients.
Next, we actually default.
Propagate is quite simple.
Surprisingly, isn't.
Think about it.
We just take the inputs from a batch or batch of inputs, feed it into the net like this and collect
outputs an end to consume the very, very simple.
Next, we test to get the lowest criterion against is quite simple.
Remove remember the criteria and function we created above criterion using cross entropy loss.
That simply takes two outputs and labels.
We get the lowest value.
Then we use the lowest validator to do about proper application.
So we do have a loss because this returns a loss object and that loss object contains to back backward
for the net backward to implement that propagation.
So we have that.
Did we implement that?
And then we just used to optimize a lot step to update the gradients using the stochastic gradient descent
algorithm.
So now let's take a look at the code.
So you'll have some extra things in the code, which I'll explain to you labelling what we are using
them for things like epochs.
So epochs.
Basically, if you remember correctly from the other slides, epochs are how many times we pass.
All the data to network in one epoch means that we have passed.
We've passed all the batches of our data through our network, and that means we have completed one
book, one book and take anywhere from like a second to minutes or hours, even depending on how big
the dataset is and how deep and complex your CNN or net neural net is.
So I tend to use the box maybe around 50.
In most cases, I'm using 10 in this example, just because I don't want you guys be waiting for an
hour or so to finish to finish.
Actually, with 20 books, it takes five minutes to have 50 bucks and think about 25 minutes.
So either way, you can set whatever you want.
The more ebooks you used, the better your accuracy would be.
However it starts, it starts.
Topping out diminishes.
You get diminishing returns at some point.
So there's no point in selling e-books that I wasn't going to get a better network.
And if you used e-books at 50 or 100 and the tendency is to over fit to that point, which we'll talk
about, don't worry.
So let's step through this code so you can see we have these empty lists.
We've created epochal, long lost log in accuracy.
Look, these are lowest.
This is necessary as we keep the stored information as we go redundant.
So we just want to keep a log of all information.
So we just keep track of the information by putting it, appending it into these areas.
Well, this upon completion.
So for epoch in ebooks, which means.
We're going to look you one to 10.
We just print this, I think it's OK here because it starts at zero.
So on the eve of PLoS One, the book won just to just add some human, more human, more human element
to it because we tend to start counting at 1.0 and we create a function here.
I mean, the function of variable here called running loss and then make it a zero.
This story's Kimberly.
It's the loss after each mini batch run.
So that's important to use for some of the calculations as we output an information output.
So this is a plot we need to discuss now.
This is a pilot talked about where we get the data from the data loader can see Enumerate this here.
So we have AI as a index function to use within a loop, and we have data here, which is the 128 batch
size we extracted, which comprised of the inputs and the labels.
So we have data equal that so now we can pass the inputs now to a GPO as well as labels.
So this is an important line in here.
If you're using a CPU, then we said two gradients to zero, if it's a fresh start.
OK?
There are also things where you can pause learning and resume learning.
So that's what this function allows us to do.
So if in most cases, you would be something that says zero.
So just remember that.
So no, this is a process we want to do forward.
Then back backdrop that optimize, so we fully propagate here, take the inputs, get the outputs,
get the outputs, the outputs into the most criterion cross entropy loss.
So we get to loss here.
Then we can run back propagation and then we can update the gradients here.
This is a lot of stuff going on in these four lines of good by total, which makes it quite simple for
us, to be honest.
Next, we keep track of your running loss, so we use a plus equal just to keep adding on to the previous
lowest value, so we extract the lost value.
Add it here that allows us to keep track of the loss.
The end of the book so that at the end of that epoch, we can see what the overall loss was.
And this function, this line here is a simple line that tells us after every 15 minute matches what
we'll do, we'll just print some stuff and get some information.
Let's see what it is.
So.
We have a value call, a variable called correct.
We make it equal to zero, then total equals zero.
What does that mean?
Well, it means two here.
We're going to keep track of accuracy in this look in this part of the code.
So how do we keep track of accuracy?
Remember, we're training a model, so and we have access to a test data, too.
So every 50 minute match is what we do here.
We tick would know too much credit.
This allows us to quickly access to gradients of network.
And basically the way to do it without dumping it to memory.
So it's quite a fast way we do it.
We can use pie too much within the loop to do this.
So we use with no would touch that, no dread.
Then we just take the data from the little order again.
This returns.
This is a little over the test data set.
No, sorry.
So it's 10000 samples in our test data set and 60000 in our training dataset.
So we still have the same batch size one hundred and twenty eight now.
So we just take the first batch from our test dataset here again, moving to the GPU labels and images,
we felt propagated here.
This is about, you know, this is about I want you to pay attention to what we do know is that we have
the outputs from here.
We use this to dodge too much of max function to get to the node of the MAX with the maximum probability
analysis to get the predictive class.
So we get to predictive class here.
And this allows us no, we can just keep track so we know how much the total is.
We're keeping track as a total as it goes along.
That's why we use this accumulator on function here.
Operator And then we just keep track of the how many values are predicted correctly.
This is a good way to monitor your training to monitor your training of the neural net.
Do you have to monitor its progress while it's training?
So for every 50 many matches, we take the test.
Either we run the testing and shooting that went to that point and we just see how much you got it got
right?
That's what all of this is doing here.
It's quite simple.
Actually, we calculate accuracy.
We get it epoch number here, we get the actual loss and then we just print it out here.
So we just use the f string function, the sprint epoch number, how many many batches were complete
or the actual loss was which is running a loss divided by 50.
We divided by 50 because that's how much we set this here.
And then we just get the accuracy as well, which is how much it got correct over the total multiplied
by 100.
And then we started running the list again to zero at this point, at the end here.
And that's it.
Oh, after sorry, after all of that, that we just append the above number to log, but then the loss,
actual loss after each epoch to lot more here as well as accuracy.
This is important because we want to be able to plot our results and visualize them afterward.
Our training results.
So you can see when you run this, I would like you all to go press play here to run of this big trading
loop here.
And you can see after every 50 50 minute matches in epoch one, it tells you how much many batches we
complete gives you.
The loss or loss goes down.
This is a good thing because we want the loss to go down.
And you can see your test accuracy, which is what we were calculating here.
Its accuracy.
This is accuracy.
Initially, when the first run 65 percent, which is quite good.
So first quick run and you can see it gets better and better, better.
And you can see after 10 epochs, we get ninety seven point eight percent accuracy in earnest.
That's quite good for five minutes of training the day.
I think this to record it right, no one else stands at maybe ninety nine point three percent accuracy.
So feel free to experiment and see how close it can get that it would be fun.
So we'll know stuff to after the lengthy lesson, and we'll take a look at some of the things in the
next section, which is saving our model so we can see this moment and use it.
I deleted.
And also taking a look at how we can visualize the results so we can pass input data into a network
and get the outputs afterwards.
So I'll see you in the next section.