Hello and welcome to the store.
All right so now we are going to implement artificial intelligence from scratch.
We're going to cut it line by line.
And in this first good section we're going to import the libraries.
But before we start with this first code section I would like to explain the connection between the
AI and our map.
That is why we're implementing this for them.
What is the purpose of our AI and where will we be using it.
So it's actually very simple.
We're only making our AI to select the right action and each time.
So OK we import the class from our file so we will be making this D2 in class in this file.
But then we import it only to select the right action to play at each time and we select this action
exactly at this line.
Action equals brain update less reward less signal less signal will be the input of the neural network.
You know it's composed of the three signals of the sensors Plus the orientation and minus orientation.
So that's the inputs but then the output is the action to play.
And that's only what we'll be taking from our AI I felt that we're about to make.
So keep that in mind.
It's very simple.
We first import the decriminalise from the AI AI then we create the object brain from the human class
which takes as inputs the encoded vectors for the states of five dimensions the three signals plus orientation
plus minus orientation.
Reactions go left go straight or go right.
And then this gamma parameter.
That's the only parameters of the class that we'll be making.
And then once we create that object we select in the game class the action to play at each time.
And that depends on the last word.
And the Lusignan which is the input and that's all that's the only purpose of making this say that in
order to have a real artificial intelligence playing the right actions then each time the right move
instead of having random actions like we observed in the previous tutorial.
All right so let's do this let's implement our artificial intelligence.
And as we said we are going to start by importing all the libraries that we'll be using to implement.
So that way we will have all the tools we need.
All right so let's start with the first one.
The first one is the inevitable the non-pay library the non-Thai library.
I always recommend to import it it's the library which allows us to play and work with the Iraqis.
And this here is just a shortcut more convenience when we want to use number.
All right then second library is random.
So this is just because we will be taking some random samples from different batches when implementing
experience replay.
So we have to import this random library as well.
Then we will import os that will be just useful when we want to load the model because you know once
the model is ready we will implement some code to save the model and then another code to load the model.
That's when we want to you know save the brain and load the brain whenever you want to shut down your
computer and we use the brain that was trained before for some new experiment.
So that's important.
Then we are going to import the torch library essential.
That's because we will be implementing our neural network with my torch which I recommend much more
than the other ones for artificial intelligence because it can handle dynamic graphs.
So there we go with torche then from torche we are going to import torche start and end the end module
is the most essential one that's the module that contains all the tools to implement some neural networks.
And of course there will be a deep neural network that will take as inputs the three signals of the
three sensors plus orientation and minus orientation and will return as output the action to play.
Well actually to return the q values of the different actions and using a soft Max.
We will return the action to play only one the most relevant one to accomplish the cards go to torture
and in most essential one then we are going to give a short cut to the functional package.
From here we go with the functional package from the end and Maggio.
So this functional package contains the different functions that we use when implementing a neural network.
So typically the last function we will be using the Google loss because that improves convergence and
the loss is contained in this functional submodule from the end module.
And since all this is pretty long we're going to give it a shortcut and we're going to call it f simply.
Then only 3 modules to import left.
So the next one is another central one which is up to him and we take it from still the torture library
and then up there we go and let's just call it up to him instead of torture atom.
That's of course for the optimizer.
We will be importing some optimizers to perform C-grade in the sense so we will definitely need it and
then we need to import autographs.
And that's only to take the variable class from our regret.
So the purpose of it is a little bit technical.
Basically we need to import the variable class to make some conversion from tensors which are like more
advanced arrays to avoid all that contains a gradient.
So it's like we don't want to have only a tensor by itself.
We want to put the tensor into a variable that will also contain a gradient and to do this we need to
use the variable class to convert this tensor into a variable containing the tensor and the gradient.
So that's a little bit technical but that's what we have to do when working with PI torch.
And we do this thanks to the variable class.
But before getting the variable class we need to import torche that grad and let's give a short cut
as well undergrad and then from torch that undergrad we import Roybal There we go.
And now we have all the libraries that we'll be using to implement our AI.
So we won't bother importing any other library.
We have all the tools we need.
And now we are ready to create the architecture of the neural network.
So that's exactly what we will do in the next tutorial.
And until then enjoy AI.