Narrator: Hello,
and welcome to the first part
of this AI implementation,
part one, building the AI.
And as you can see, I already added a dimension
in the structure of this implementation,
with these three sections composing part one,
which clearly show how we're gonna build this AI.
First, we're gonna make the brain,
that is nothing else than the neural network,
then we're gonna make the body,
which will define how the actions are gonna be played.
And then once we have the brain and the body,
we will assemble them to make the AI,
which will be in the last section
of this first part.
So now you can already have a good vision
of the structure of this implementation.
First, we make the brain,
then we make the body,
and then we assemble the two to make the AI.
And in this tutorial, we're gonna start
with the first section
that is about making the brain.
And this is going to take us four tutorials,
you can imagine that making a brain
is not like making a cake,
so this will require more than one tutorial.
And of course, as usual,
we are going to represent that brain with a class,
because we will need several functions.
And in order to have a structure of several functions
that will be organized in some kind of instructions,
well, of course, we need a class.
And that's perfect,
because once we make that class,
well we will be able to create as many brains as we want
by just creating some objects of this class.
So again, class is in Python,
and in general,
in object oriented programming languages are very practical
because you make your model of something you wanna build
and then you're able to create as many objects as you want,
and they will have all the features
that you define in the class.
And for our brain, the features will be, of course,
well first of all,
the architecture of the neural network,
which I remind will be a CNN,
and of course, the different functions,
like for example,
passing on the signals from the input neurons
to the output neurons.
So that way of course,
the forward function that we will make.
So let's do this.
Let's start making the brain,
this is going to be pretty exciting.
It is one of my favorite parts,
and therefore let's get straight into it.
So we're gonna start by introducing the class, of course,
and we're gonna call this class
well, I hesitated to call it brain,
but let's be more direct
and let's call it CNN,
because actually the brain is a CNN network,
a convolutional neural network.
So as you want, you can call it brain if you want,
but at least now we know what we're building.
And CNN, as for the network of the self-driving car,
is going to inherit from the nn.Module.
So remember, the nn.Module
is what we imported here
and we want to be able to use all the tools
of this nn.Module
and therefore, we want to use this technique
in object-oriented programming,
which is inheritance,
and which allows us to, you know,
well, use all the tools from a parent class.
And this parent class is going to be nn.Module
There we go.
And now we can use all the tools
and objects of the nn.Module.
All right?
So now that we have our inheritance,
we can go into the class to make our first function.
And as you probably guess,
the first function is the init function,
that will define all the variables
of the future brain objects,
you know, the future CNN objects
that will be created.
All right, so let's do this,
def__init__
and now we need to input some variables.
So first, the first variable is going to be self.
That of course, refers to the object.
Now, I guess you're pretty comfortable with this.
Then we are gonna add another variable
which will be the number of actions
in the Doom environment.
So we're gonna call it number actions, number of actions.
And actually this variable is not compulsory
for the init function,
it's just that if you want to test the AI
that we're gonna build
on other Doom environments,
well this will be very practical,
because we will import this number of actions variable
from the Doom environment wrappers with two discrete,
and when doing that, we will, you know,
input the name of the environment, Doom v0,
and so if you want to, you know,
experiment with this AI
on other Doom environments
and play on other games,
well you won't have anything to do,
because this number of actions
will directly get the number of actions
in the Doom environment you'll be playing with.
Okay.
So that's it for the two arguments of this init function,
so now we can go inside.
And now, remember what we have to do.
The first thing we have to do is activate the inheritance
with the super function.
So that's exactly like for the self-driving car,
we take the super function,
then inside we start by inputting the class
that will define the neural network,
and that is CNN.
Then we have to input self,
to refer to the object.
But then remember, that's not all,
we need to add here a dot
and then the init function
with some parenthesis.
And by doing that, we activate the inheritance,
and now we can use all the tools from the nn.Module.
All right.
So now I think it's time
to build the architecture of the neural network.
And so as you remember, we are gonna build a CNN,
a convolutional neural network,
simply because this time, the AI will have eyes,
and the eyes of the AI will be the convolutional layers
of this convolutional neural network.
And then after the AI visualizes the images
with the convolutional layers,
it will pass on the signals
into a classic artificial neural network,
so that is the one that we had before,
with fully connected layers.
And that's where it will try to predict the Q values
for each possible actions that we can play.
So you have the architecture in mind,
first, we're gonna have some convolutional layers,
and then some fully connected layers,
and this will be the brain of our AI.
So what we're gonna do to, you know,
be able to have a step back at what we're making,
well, let's just make this architecture
with the variables we want to create.
So, actually, speaking of this architecture
we are gonna build a CNN
with three convolutional layers,
and then after that, one hidden layer.
That means that we will need three convolutional connections
and two full connections.
And speaking of connections,
that's exactly what we're about to define,
that will be the variables of our CNN class,
and therefore, right now I'm going
to define five variables,
three for the convolutional connections,
and two for the full connections.
So let's do this.
We're gonna start with the convolution connections.
So I'm gonna call them self.convolution1
I'm gonna copy that
and paste them below.
And there we go,
self convolution2
and self convolution3.
That's our convolution connections.
So this first convolution1 here
will apply some convolution to the input images
to get a first convolutional layer.
Then the second convolution2 here,
will take the first convolutional layer as input,
and by applying again some convolution,
it will create a second convolutional layer.
And in this convolutional layer,
we will get some new images,
each of them detecting one specific feature.
So we will get these new images
in a convolutional layer,
then we will apply this convolution2 here
to connect these new images
from this first convolutional layer
to some new images of a second convolutional layer.
And these new images, again,
will detect some features
in the images of the first convolutional layer,
so it's just to reinforce the feature detection.
And then to the images of the second convolutional layer,
we apply the third convolution here
to get for each of them,
some more images that detect even more features
inside the input images.
And so the more we do this,
the more we apply some convolutions
to the different layers of images,
well, the more we are able
to detect some features.
And that's how by detecting the features,
the AI will understand where the monsters are,
where it has to shoot to kill them,
and where it should go to.
It will also detect the walls,
the obstacles,
well literally, where it has to go.
And that is thanks to
what all these convolutional layers detect
in the original input images.
All right.
So that's for the convolution part of the CNN,
but then remember, after the convolutional layers,
we have to flatten all the pixels obtained
by the different series
of convolutions that were applied.
And by flattening all the arrays of pixels,
we get this huge vector
that will become the input
of a classic artificial neural network,
and that's where we get our fully connected layers,
and therefore, our full connections.
And so now, what we have to do
is create two new variables
because we're gonna have one hidden layer
in this classic artificial neural network that comes next,
and therefore we need one full connection
from this huge flattened vector
to this one hidden layer.
And a second full connection between this one hidden layer
and the output layer,
composed of the output neurons
that are the Q values.
So let's make these two first connections
and then we will define all these connections.
So as for the self-driving car,
we're gonna call them self.fc1
and then self.fc2.
All right.
So now we have all our variables.
And so now what we have to do, is of course,
define them with the classes of the nn.Module.
So that means we'll basically create the architecture
of the neural network,
and that's what we'll do
in the next tutorial.
Until then, enjoy AI.