Hello my friends.
Welcome back.
I'm sure you feel amazing after having built your very first artificial
neural network.
But remember, that's only half part of the job.
The second half will be, of course, to train it on the whole training set.
And we are going to do this in two steps.
The first one is to compile the A and then with an optimizer,
a loss function and a metric, which will be of course the accuracy
because we're doing some classification.
And then the second step will be of course to train the A
and on the training set over a certain number of epochs.
Are you ready?
Let's do this.
Starting with this first step compiling the an.
All right.
So once again doing this will be super simple
thanks to the TensorFlow library that integrated Keras.
Because indeed, to compile our A and then we first need to start from our
A an object which I remind was created as an instance of the sequential class.
And then from this object we're going to call a new method,
which this time of course won't be the add method.
But can you actually guess what this method is going to be?
You know, there is no trap in TensorFlow nor any confusion.
Well, the method to compile an artificial neural network
is simply the compile method.
As simple as that, right?
We didn't even have to look at the TensorFlow documentation,
which by the way, I still recommend to have a look at because you will get
a lot of information on the diverse tools you have in the TensorFlow library.
But here it's super intuitive, it's super easy.
So now I have a next question for you.
According to you,
what do we have to enter as parameters inside this compile method?
Well I actually said it.
We have to enter three parameters.
The first one is the optimizer to choose and not to miser.
Then the second one is the loss to choose the loss function.
And the third one is the metrics with an s parameter.
Because.
Note that you can actually choose several metrics to evaluate your
and at the same time, but we will only choose one
and we will choose the accuracy.
But there you go.
These are the three parameters.
So I suggest that we start by entering them.
And then we will enter their values okay.
So let's start with the first one optimizer equals all right.
And comma the next one is the loss for the loss function.
And finally the third one is the metrics parameter.
All right.
So for the optimizer which one would you like to get.
Well, in the intuition lectures Kirill mentioned that the best one
are the optimizers that can perform stochastic gradient descent.
And the best of them, you know, the one that I recommend by default
is the Adam Optimizer, which is very performance
optimizer that can perform stochastic gradient descent.
And by that, let me just remind what stochastic gradient descent allows to do.
Well, you know, it is what will update the weights in order to reduce the loss
error between your predictions and the real results.
You know, when we trained in and on the training set, we will at each iteration
compare the predictions in a batch to the real results in the same batch.
And that optimizer here will update the weights through
stochastic gradient descent, because we're going to choose Adam
optimizer two at the next iteration hopefully reduce the loss.
All right.
So that's why right here we have to choose an optimizer but also a loss function
which is the way to compute the difference
between the predictions and the real results.
And then the accuracy of course because that's our final evaluation metric.
All right.
So as we said we're going to choose the Adam optimizer.
And the code name for that is simply but with no capital letter.
Adam okay. Congratulations.
Now you know how to compile an artificial neural network with an optimizer.
But then we also have to compile it with the loss function.
And now you have to know something very important.
When you are doing binary classification, you know, classification
when you have to predict a binary outcome, well, the loss function must always be
the following one entered in quotes, of course, which is binary
underscore cross entropy.
Just like that.
And now let me tell you what you would have to enter if you were doing
non binary classification.
You know, like for example predicting three different categories.
Well here you would have to enter categorical cross entropy loss okay.
For binary classification the loss must be binary cross entropy.
And for non binary classification the loss must be categorical cross entropy.
And then also you know when doing
non binary classification when predicting more than two categories.
Well the activation should not be sigmoid but softmax right.
I take this opportunity to also give you the other cases of classification
which you could encounter. Okay. So now you know everything.
And then remember that for regression because we can also do
artificial neural networks for regression.
Well we have this free course which I gave you the link.
You can just take this course for free and you will get the full implementation
of an artificial neural network for a regression case study.
So you have really everything that you can do with an artificial neural network.
All right. Great.
And now let's enter the final parameter here metrics.
As I said we can actually choose several metrics at the same time.
Therefore in order to enter the values of this parameter
well we have to enter them in a pair of square brackets,
which is supposed to be, you know, the list of the different metrics
with which you want to evaluate your in and during the training,
but we will only choose the main one, you know, the most essential one,
which is the accuracy and which you have to enter in quotes.
All right.
So accuracy, just like the classic spelling.
And now now congratulations.
You know how to do a full compile of your.
And then with an optimizer a loss and some metrics.
Perfect.
So now let's move on to the ultimate step meaning the step
where we will train the A and onto the whole training set.
So let's create a new code cell.
And now according to you how do we need to start this training.
Well once again you know it's always the same thing.
We need to take our A and object, then call a new method
which will perform the training and then enter a couple of parameters.
So let's do this.
Let's start with a and and first our object.
And then according to you what will be the method
that can train your artificial neural network on the training set.
Well nothing has changed here.
And actually I think I said it earlier in the course, the method to train
whatever machine learning model is always the same one.
It is the fit method, the fit method,
and which will take always the same parameters.
The first one is x train
for you know, the matrix of features of the training set.
Then y train for the dependent variable vector of the training set.
And then when training an artificial neural network, we actually need to enter
two more parameters which are first, the batch size.
Because indeed batch learning is always more efficient and more perform.
And when training an artificial neural network, meaning that
instead of comparing your prediction to the real result one by one,
you know to compute and reduce the loss, well, you're going to do that
with several predictions compared to several real results into a batch.
And the batch size here, you know, the batch size parameter gives exactly
the number of predictions you want to have in the batch
to be compared to that same number of real results.
And the classic value of the batch size that is usually chosen is 32, right?
If you don't want to spend too much time tuning this hyperparameter,
well, I recommend to choose the default value 32.
But anyway, I wanted to highlight that hyperparameter here because indeed,
it is very important to remember that we are doing batch learning.
Okay, so batch size equal 32.
And finally I'm sure you know which final parameter we have to add to here.
That's of course the number of epochs.
You know, a neural network has to be trained over
a certain amount of epochs so as to improve the accuracy over time.
And we will clearly see that once we execute this cell.
So the name of the parameter for the number of epochs is simply epochs.
And well, you will see that it will go very fast.
So we can just take 100 epochs.
But once again feel free to choose another number
as long as it is not too small, because you know your neural network
needs a certain amount of epochs in order to learn properly.
You know, learn the correlations to get the ultimate best predictions.
All right. Great.
So we're actually done with part three now.
So I suggest we no longer wait.
And that we execute all the cells.
We haven't executed so far which I think you know start from part two right. Yes.
This was the last cell of the data preprocessing phase.
It was run properly.
So let's actually run each cell one by one
and see what we're going to get in the end during the training.
So let's start with this one.
Initializing the and good.
Now this one adding the input layer and the first hidden layer.
Good.
Now this one adding the second hidden layer.
All good.
And now this one adding the output layer.
All good still.
Now we end to part three.
Executing first this cell compiling the an all good.
And now are you ready my friends.
We're about to train the artificial neural network
on the training set over 100 epochs.
And here we go.
The training is starting.
And as I told you, it's going pretty fast. But look at this.
Look at the accuracy and how it is evolving over the epochs.
And we can see that it is actually increasing pretty fast.
And mostly
we see that it is actually converging, you know, converging pretty quickly.
You know, we converged at oh point 86, you know, at about the 20 epochs.
We actually didn't need that 100 epochs, but 20 was fine.
But anyway, you know, it's going really fast
and I'm sure it's very over soon.
Now because indeed, yes, there we go.
The training was done in more or less 20s and the final accuracy
we get on the training set, we'll have to check the same on
the test set is averaging around oh point 86.
That's really good.
That means that out of 100 observations you have 86 correct predictions.
So congratulations, you made a very good first deep learning model.
You can be proud of yourself.
And mostly now you can take a little break because we're going to answer part four.
And not only we're going to end support for
but also you're going to see that you're going to have a little homework,
which will consist of predicting the result of a single observation,
meaning a single customer.
You will have to predict if this customer will stay in or leave the bank.
You will enter your solution here
and we will implement the solution together in the next tutorial.
So make sure to do it.
Please try at least to do it.
You actually know how to do it because we already learned how
to do a single prediction before you know the prediction of a single observation.
So you have everything.
Maybe check out again part three classification if you have a doubt.
But there you go. That's your homework.
You have to use R A and model to predict if the customer
with the following information will leave the bank yes or no.
And these following informations are that it is a French customer
with a credit score of 600 and male one.
He is four years old.
He has been in the bank for three years.
He has $60,000 in his account.
He has two products in the bank.
He has a credit card.
Indeed, he is also an active member and he has an estimated salary of $50,000.
And the question is, so should we say goodbye to that customer?
Well, please figure it out and we will see if you right in the next tutorial.
Until then, enjoy machine learning.