Instructor: Hello and welcome to this Python tutorial.
Now we have to define the five variables
of this init function.
That is the three convolutions,
and the two full connections.
So let's start with the first one.
Convolution one applies convolution to the input images.
So that's the original images.
And now you're gonna see how everything will become
so simple to create this convolution.
Well, what we have to do is actually create an object
of some specific class.
And this class is taken from NN,
and then the class is COM-2D.
2D because we're working with 2D images.
And now as you can see, we need to input several arguments.
First one is in channels, so let's input it in channels.
The second one is out channels.
The third one is kernel size.
And the rest of them are stride, the betting,
the dilatation groups, and bias.
And we have default values for all these ones.
So we're not going to input them.
We're gonna keep the default values.
But what's important is these three arguments in channels,
out channels, and kernel size.
And so do you guess what they correspond to?
Well, very simply, in channels correspond
to the input of the convolution,
and out channels correspond to the output
of the convolution.
So in channels, what is it going to be?
Well, very simply, that's going to be the number
of channels in our images.
And actually we are gonna work
with black and white images because basically
we don't need colors to recognize the monsters.
The AI is totally capable
of recognizing the monsters in black and white.
So we don't need the colors,
it'll just recognize them by their shape.
And therefore we're gonna use one channel.
So one channel is when you have black and white images
and three channels is when you have colored images.
And therefore, since we're working with
black and white images in channels,
is going to be equal to one.
Then out channels.
So out channels is going to be equal to the images
you want to have in the convolutional layer,
which is the output of this convolution one.
And so basically this is equal to the number
of features you want to detect in your original images,
because we will create one image per feature
we want to detect because basically you know how it works.
We apply one feature detector to the input image to
detect a specific feature in the input image,
and therefore the number of outputs images
here is the number of features we want to detect.
So now the question is how many features
do we want to detect?
Well, a common practice is to start
with 32 feature detectors.
And so that will lead us to 32 processed images
in this first convolutional layer.
So the input is one black and white image, a real image
and the output.
And the first convolution layer is 32 processed images.
And by processed, I mean of course,
that the convolution was applied
to the input image to get 32 new images
with detected features.
And then we need to specify a kernel size,
which is nothing else than
the dimensions of this square
that will go through the original image.
And in common practice we use either
two by two, or three by three, or five by five.
And for the first one
we're gonna use a five by five feature detector.
That is a feature detector that will have
five by five dimensions.
And then we will reduce the size
of this kernel for the next convolution layers.
And speaking of it, this is
exactly what we're gonna do now.
We are going to copy this to
define the second convolution,
and therefore I'm facing that here,
and now it's very funny and very easy.
It's like a domino.
The input channel
of the second convolution layer is the output channel
of the first convolution layer.
So this number of outputs 32 here,
is the same number of inputs, 32 here.
And that's because we have 32 images
in the input convolution layer of the second convolution.
And so the second convolution is applied
to this second convolutional layer,
to return a third convolutional layer.
And so now the question is how many new images do we want?
Well same, let's create 32 new images.
32 is actually a very common number
in convolutional neural networks.
If you look at the architectures
you will find 32 in many of them.
And then for the kernel size, well
we need to reduce the kernel size.
That is the dimensions of our feature detector.
And so now we're gonna go from five to
either four or even three,
and then we'll go even smaller.
All right, so our second convolution is ready.
It takes as inputs, 32 processed images,
each one detecting a first feature
of the original input image
and it creates 32 new images,
thanks to this reduced dimensions of the feature detector.
And so now let's push this even more.
So I'm copying this,
and pasting that here to
create a third convolution to detect some features.
And so now that's the same, the input channels
here is the number of input images at the left
of the convolution connection, and that is the number
of processed images that was at the right
of the previous convolution connection.
So that's 32, therefore we keep 32 here.
That's perfect.
And now the question is again, how many new images
do we want to detect?
We are gonna take now 64,
and therefore 64 output processed images.
And of course now we take a smaller kernel size,
and we're gonna take two.
And so that's a very classic architecture
of a convolutional layer, and it's very efficient to
have a high level of feature detection inside images.
All right, and so, now that we have our
three convolution layers, thanks to our three
convolution connections here,
well now it's time to get our two full connections that
I remind will take this huge vector that we obtain
after flattening all the 64 times 32, times 32 again
images that we got from all these convolutions.
So we flatten all the pixels of these images
and we gain one huge vector that will become the input
of a new, fully connected neural network.
And so that's when we have to make these full connections
between first this huge vector and a hidden layer,
and then a second full connection between the hidden layer
and the output layer composed of the output neurons
each one corresponding to a Q value of the possible actions.
So let's make these two full connections.
You know how to do that.
That's exactly what we did for the self-driving car.
So let's do that again.
Well, first we take our NN module,
then we take the linear class because again
the full connection we create is an object
of the linear class.
And then in parenthesis, well, that's the same.
First, we input the input features,
that is the number of them, then the output features.
And so the input features for the first full connection
where is it going to be?
Well, that's going to be equal to the number
of pixels there are in this huge vector obtained
after flattening all the processed images
after the three convolutions.
And so what is this number?
Well, actually, there is a trick here.
This number is actually hard to get.
We actually need to make a function to compute that number.
We don't have a variable that will get us this number.
We have to compute it.
And therefore, what we're gonna do now,
and now it's very important to understand
the mindset of programming that we must have
and trying to bring to you the mindset that is
what you must be thinking right now
to do to overcome this obstacle.
Because the first time you might say, hey
I don't have this number of neurons in the flatten vector.
What should I do?
I'm stuck here.
Well, no, actually
because what you can do now is simply input any
name here that will represent this number of neurons.
So I'm calling it number neurons, number of neurons.
And then we will simply make a function that will return
in this number of neurons variable
this number of pixels we're looking for.
So we can totally do that.
We can totally put this variable.
Well, of course we'll get a warning
because it doesn't exist yet,
but we will create it afterwards with a function
and we are totally allowed to do that even
if the function comes afterwards.
So that's a typical programming thinking
you must have when you get that kind of obstacle.
Well you can make a function to get what you're missing.
All right, and then out features,
and out features that the number of neurons
in the hidden layer and that this time is up to you.
That depends on the architecture
of the neural network you want to create.
And so a good number would be not a too small number.
So for example, 40 neurons might be fine.
We can try to increase it.
If the training is not too slow, you can try to increase it.
Maybe that will improve the predictions.
But let's start with 40,
maybe we'll increase that afterwards.
All right, so that's it for the first full connection.
Then we'll copy this,
paste that here for the second full connection.
That is the connection
between the hidden layer and the output layer.
And so the in features here becomes the out features
of the previous layer, and that is 40.
So here we input 40.
That's of course the number of neurons in the hidden layer.
And our features here is going to be equal to the number
of output neurons there should be in our neural network.
And since each output neuron corresponds
to one Q value,
and one Q value corresponds to one action,
well the number of alpha neurons here is of course
the number of actions.
And we have one variable for this, which is number actions.
And therefore here we input number actions.
And there we go.
Congratulations.
We defined the architecture
of our neural network.
Our neural network is composed
of three convolutional layers and one hidden layer.
All this in one big CNN.
And this CNN will detect the features in the game
so that the AI will know what it has to do
where it has to go, and where it needs to shoot.
So there we go for this step,
that's a first very important step done.
Now we're gonna move on the next step, which is of course
to get this number of neurons that is still missing.
That's actually why we have the warning here.
And define name number neurons, but no worries.
Now we will make a function that will return the number
of neurons in this huge vector, and we will put that number
in a variable that we'll call number neurons.
So let's do this in the next tutorial.
That's our next step.
And until then, enjoy AI.