-: Hello, and welcome to the last step
of our process to make the brain.
We have one function to make left.
This is the forward function that will propagate the signals
in all the layers of the neural network,
including the three convolution layers
and the fully connected layer.
And so this function is the forward function
exactly like for the self-driving car,
except this time we have to propagate the signals
in the convolution layers before the fully connected layer.
And the good news is we already did that
in the previous step with the count neurons function.
So we already have the code to propagate this signal
in the convolution layers.
And so this will be very quick.
We will just combine what we did here
and what we did for the self driving car
and we'll get our forward function for our brain.
So let's do this.
We introduce a new function here.
The last one for the brain
and this function is the forward function,
which states as argument,
well exactly like before self
to refer to the object.
And X, which will be first the input images,
and then you know, X will be updated,
as the signal is propagated into the neural network.
All right, so colon, and then let's go inside the function.
So as I just said, we already made the code
to propagate the signals in the three convolution layers.
That's exactly these three lines of code.
So I'm copying them
and facing them here.
And there we go.
We already have our propagation of the signal
in the three convolution layers.
And so now we just need to propagate the signal
from the convolution layers to the hidden layer
and then eventually to the output layer
that is at the very end of the neural network.
And to do this, we first need to flatten
the third convolution layer that we obtained here.
Remember X at first is the input image.
Then here X becomes the first convolution layer.
Then here X becomes the second convolution layer,
and then here X becomes the third convolution layer.
So right now at this stage X is the third convolution layer.
And now to obtain the flattening layer
we need to flatten this third convolution layer X.
And to do this, we are gonna do
something quite similar as we did here.
Only this time, we don't need the number of neurons.
We simply need to flatten the channels
in the third convolutional layer.
So this will be quite more simple, but very similar.
And to do this while we're going to take X again,
because X is going to become the flattening layer.
So we're just updating X,
so X equal, then we take X again,
but this X is the old X,
that is the third convolution layer.
So we take the third convolution layer,
then dot, then we take the view function
to which we apply two arguments.
The first one is X dot size zero.
So again, we take the size function to
take all the pixels of all the channels
in the third convolutional layer.
And we put them one after the other in this huge vector
that is going to become this X here.
And this X then will become the input
of the fully connected network.
But that's not all we need to add here,
comma and minus one.
So that trick, you can find it in the PyTorch Tutorials.
That's how you can flatten a convolution layer
composed of several channels by using the size function.
And of course, if you want more details on how this works
you can go to the PyTorch Tutorials,
I will provide the link.
So now that we got our flattening layer,
well you know this flattening layer
is going to become the input of a classic,
fully connected network with a simple linear
transmission of the signal.
And so now we're not gonna use a convolution function
to pass on the signal.
We're gonna use a linear transmission with the linear class
and then to break the linearity,
because you know we're working with images,
and images have non-linear relationships,
well we're gonna use a rectify function
to be able to learn these non-linear relationships.
So let's do this.
This is actually the next step.
And so now that's exactly like what we did
for the self-driving car.
We take X because we want to update it again,
we want to get the hidden layer now.
And so first what we do is we take our full
connection FC one,
because the full connection FC one is the one
that connects the flattening layer to the hidden layer.
And therefore we need to take FC one,
and apply it to the X that we have right now,
which is the flattening layer.
And we don't forget the self, of course,
because FC one is a variable of our in it function.
So self.FCone x.
And so that passes on linearly the signal
from the flattening layer to the hidden layer.
But now we need to activate these neurons,
while at the same time breaking the linearity.
And that's exactly what we do
with the rectifier activation function.
So now what we have to do
is take our functional module
and from this functional module,
we take of course our rectify function,
that is the relu
and we put cell.FC1 inside the parenthesis.
All right, So what happens in this line of code is that,
first we propagate the signals
from the flattening layer to the hidden layer
of the fully connected network.
And then we activate the neuron of this hidden layer
by breaking the linearity with this rectify
your activation function.
And we get our hidden layer that is X here.
Perfect, and now we have one less step to do.
It is of course, to propagate the signal
from the hidden layer to the output layer
with the final output neurons.
And to do this well, that's very simple.
That's exactly like what we did for the self-driving car.
We take our second full connection, FC two,
and we apply it to, of course,
the neurons of the hidden layer that is right now, X.
So X here is the neurons of the hidden layer,
this old X.
And X here becomes, of course, the output neurons
of the output layer containing the Q values.
And finally, we simply return the output neurons
that is X with the Q values.
So perfect, congratulations.
We just made a brain.
We just made the brain of our AI
with the eyes and the rest of the cells.
So congratulations.
Now it's time to make the body that is defining how
we're gonna play the action.
After all, the signals are processed in the brain.
So that's our second big step.
Let's do this in the next tutorials.
And until then, enjoy AI.