So now let's take a look at building a model, so to do this, I'll structure the code.
I'll explain everything properly.
So you guys fully understand.
But what I would like to see is that when we're building a model, remember with the CNN, you have
different building blocks.
You have to convert layers, you have your reel, you have your Mac spools, you have a flattened,
fully connected and densely as many outputs.
And all of those things well and PyTorch participate, which allows a lot of low level control over
these things.
So I will give a warning for beginners to pay for this.
The code for this could seem a bit overwhelming compared to the Code for Carers and TensorFlow, which
you'll see in the next section.
However, what I would like to say is that it's a good thing that it's cool, it's complicated.
It actually allows you to have a deeper understanding of CNN's and deep learning and allow you to understand
Python a bit better and then to understand what exactly you're doing in the training process.
So let's begin.
So let's take a look at this.
This is a scene and we're going to be building.
You would remember this from our slides here.
We have the input image, the 28 by 28 by one grayscale image.
We use tree by tree filters.
We get our feature maps here.
We have 22 filters.
We get 22 feature maps.
They're now 26 by 26.
And then we apply tree by tree filters again, which gives us another set of feature maps here.
This is 24 by 24 by 64 because we have we use 64 convolution filters in this list that we apply max
boot to shrink the dimensionality in half.
But we still retain 64 filters or feature maps, and we flatten that to get nine thousand two hundred
sixty two new ones here.
This is connected to 128 nodes with no fully connected layer.
These are two for the connected.
This is also for the connected here to the output layer, where we apply the soft max function to get
probabilities for classes.
So let's take a look at this.
So I have some notes here.
This is how you define the parameters for conflict.
Now, as a kind of 2D layer, we're using.
We specify how much input channels.
So in our case, it's one because it's only one in the depth here, and it would have been treated with
a color image.
Our own channels is a number of filters, which is to clean size is the filter dimensions.
So tree by tree straight and padding, which you are familiar with from the floor is the straight is
how much jumps we take when moving too slowly.
When do around and padding is basically if we put a zero pads around the image here if you want to keep
their nationality going.
So by default, stride is one and padding is zero, so we can set parameters winning here.
So now let's take a look at how we used to watch and then functional to create the network.
So this is a quote I said could be a bit confusing to beginners, but I'll explain everything line by
line.
So this is where we're importing the torched on an.
Modules or objects that can be using to create the CNN and functional F.
That's a class where it's basically called F by convention.
And it's a module that allows us to access some of the parts of the pie to its library.
So it gives us access to things like the activation functions and other convenient functions that we
use.
So these are the two main functions we'll be using to create what objects we're using to create our
CNN.
So let's take a look at this class.
So in PyTorch, we create, we don't have to, but it's generally good practice.
This is how the tends to be done, but a model as a class.
So we call class net and we pass this in here.
This is basically the module function that it inherits from.
So super, you see this function in the first line in it.
I don't know if you know what it is, and it is a function that runs as soon as you instantiate a class.
So Super is a subclass of the module and with inherits all its methods.
So it allows us to access all the methods in the module object so we can use it conveniently in this,
which is which is how it's allowed us to do these sets of these types of declarations in this.
So hopefully you're still with me, but it's actually quite simple, once you get practice, it's good,
it's good to actually get hands-on experience practicing with these things and then slowly you'll develop
a deeper understanding of what's happening.
So for now, we're on this line here.
So we have we create a class that we're inheriting from super net.
That is what we call this one, and we just use that init function just to initialize it as well to
run it.
So now let's move on to dislike.
So this is this is a line.
These are the important lanes where we actually define what we're seeing in Liz.
So we call it the icon of one and we call.
This is how we create to see an area.
So we have Menin, which we it up above here of 2D.
We use one is in both dimension can scroll up here if you want to see the parameters it takes in four
dimension output channels kernel size, straight and putting.
So we have 22 filters here, tree by tree, and we're using the straight and putting default values
of one and zero.
We could specifically specify them here, but we'll just go with the default values in this in the CNN.
And secondly, we have a second currently here.
Now I notice we have 32 as in, but that's because this channel.
The second one outputs 22 filters.
So now the input to this wittily two filters.
64 filters.
What we're going to be using, though, in this conflict, which we buy three filters and again, the
default stride and putting next, we have the Max Pouliot where we just simply destroyed them.
Describe this to filter size, kernel size and the right here.
So that's two two.
And we call this stock pool and then we create another layer here.
But FC once a first for the connected layer, when we take the output of the Max Boolean, it's 64.
Because it was 64 was how many filters we used in the killing of Julia.
And we have 12 by 12, which is a size which we know and we opened this to 228 notes.
And then finally, in the final conflict, we have a 128.
It's connected to the 10 output notes.
No.
What did you notice here?
I kept saying this is but this is the output.
However, it's not linked to it.
These are just objects we're creating here.
This is an object.
All of these things that are still objects.
Think of it like pieces of a pipeline.
But we haven't linked the pipeline yet.
That's what we do in the forward function.
Remember forward sense of a forward pass of forward propagation.
That's how we create.
That's what we know, link.
All of these objects are created and in it, modular focus.
We linked them together.
So now we're treated like we're taking input.
X would be input.
So the input to actually remember when we actually using functions or objects that are declared in a
function, we use both access to the same one.
It's a good step by then terminology or syntax.
So in this function here, this is the real function here.
So we just apply.
We take the input x, we apply it.
Basically, this is how Touches C specifies that you're going to use the function you don't f don't
redo.
Suddenly, you want to use the activation function and which is kind of context input as X.
So it's self-cleaning for one x, as the input X, as the output now at when it went into the XS and
XS output.
You just take this input.
And because of the sequence, this input here is not processed as this year, so it changes that X.
And likewise, we can take the output to this S. committed the input of Columbus to again specifying
redo.
And now we used a Pouliot.
So you can see we know we have pool really closed all in one line, quite convenient.
And again with X here.
Next we have X dots.
View this view function here, and we use the view function to basically reshape our tensor.
Because remember, we have the max spool output here as sixty four by twelve by twelve with color flexible
over sixty four point.
We want to consider what the same thing and this is.
This is what we specified here.
So we why do we use minus one now?
Well, the meaning of the parameter minus one is that it glows and gives us a situation where if you
don't know how many rules you want, but you're sure the number of columns you can use, this minus
one had to specify.
So it's just a way to actually just tell the library.
We're not sure you give me the output and I'll deal with it effectively.
That's what it is.
So we just sort of like, Apply this here, reshape this vector.
This is basically flattening this ActionScript flat in the head.
It flattens this up.
This treaty, Trini Tensor.
I think it gets and basically passes up now to the fully connected layer because it fully connected,
the layer will need a flattened vector to operate on.
That's why it's good to have an understanding of what exactly happens in the CNN here, because with
PyTorch, you actually get a fairly low level with manipulating this data and these tenses.
So you can see how the dimensions are important.
It's good to know these things.
So we have no x flattened vector going as input to FC one layer.
And again, we apply real definition to that.
We'll take the output and then pass it to our last.
FC Dhulia Now you may have noticed one thing that that is sort of actually layer isn't here that don't
work.
That's coming.
It is it is part of the Network Hub.
It's not part of the fold propagation part of that.
We're here.
This here is basically just our model as it is.
And then we apply software.
In a different way, afterword from these outlets, one is open to that list.
So that's what we do here in these two lines at the end of this.
This class doesn't actually do anything just to visit that.
It is not to make anything until we actually run the class.
So we create this obliga equal that that's when we basically create a world class object here.
And then we set it to device.
And if you remember correctly, the device basically tells us, what do we want to do on a GPU or CPU?
It's something we set initially CUDA, meaning it's GPUs, and I would use CUDA as a value.
You can see me hover over it, so it would be passing all the information on this class.
CNN object to dejavu for operation.
Next, let's take a look at the code without the distracting comments, because I did leave a lot of
comments for you guys.
So you understand us fully.
You can see the code looks quite simple.
This is this is the code that codes this, you know?
So hopefully you realize that using pay too much isn't that difficult.
There's just a few things that you need to know how we just create object layers here.
We link them all to get into, followed by for loop.
We create the object here and send it to the GPU or CPU.
OK.
And then that this outputs this year, which is basically the layers in like a printed summary format
of our network.
We can actually print that again here by using it.
If you ever want to access that object and that object can print it like this again, it's the same
thing I think above here.
It's just another way to do it.
So next, we'll take a look at defining the Laws Function Optimizer, and then we'll start training
on CNN, so I'll see you in the next lesson.