Hello, dear friends, and welcome to this new session in which you will be building custom losses and
metrics.
The first method we look at is building a custom loss method without parameters, the next with parameters.
And finally, we'll build a custom loss class.
And then from here we'll go on to build custom metric method with parameters.
Custom metric method without parameters and custom metric classes.
Most times when building models and training them, all you need to do is to pick this last function
from one of these losses available in the documentation.
Now, in a case where you have to build a loss function from scratch or a custom loss function in which
defining the loss isn't as easy as just specifying this loss function name right here.
So right now let's look at how to build such custom loss functions.
Yeah, we're going to have this custom binary custom, let's call it custom.
B.C.E. B.c.e stands for binary cross entropy.
So we have the custom BCE loss right here, and then we'll define it just above this cell.
Now we have our custom BCE loss.
There we go.
And here's what we're going to do is we take our will define this BCE object, the binary cross entropy
loss object.
And obviously, yeah, we have binary cross entropy.
There we go.
So we've defined this object of this class, and then we return this computation of the binary cross
entropy of the Y, true, and the Y.
PRED So, yeah, this white row is the actual output labels.
And here's what the model predicts.
You could understand this better by taking a look at the documentation where you see the define this
object.
And then in computing this binary cross entropy loss, you see it specify this y true and widespread
right here.
Now, this binary cross entropy could take several arguments, for example, from logics labels, mode
in the axis reduction and the name.
So that's it.
We have this custom binary cross entropy.
And just here, all we need to do is to specify this name.
So let's take this off and we'll find.
Okay, now we just run this, we run this binary cross entropy, we run this matrix not defined.
Let's run the cell or on the cell and then finally run the training.
We get this error.
This function takes zero positional arguments back to a given.
So we have to have this.
Here's why.
True and why predicted okay, we run that and run that.
We could see here how this training process looks similar to what we've had already when we're dealing
with a preconceived binary cross entropy loss.
Now, that said, let's suppose that we are going to pass in a parameter here.
So one apartment tries the way in which we calculate this binary cross entropy that is one to have this
binary across entropy multiplied by a given factor.
Let's say one multiply this by a factor of, say, 0.5.
In that case, we could have this here as a factor and then we'll pass in this factor right here.
So we pass this in.
Let's top the training and then we train to see how that works.
We run this here and run this factor not defined.
We could include this factor here.
We run it factor still not defined.
And this brings us to the way in which we define models or rather defined loss functions in which we
have a given parameter.
So right here we're going to define a loss here, a loss function, and then we pass in a y true y pred
So it's going to be like similar to what we had with a custom B.C.E. without a factor.
We have that.
And then in here we have this BCI and then we return this right here.
Now notice how we take this to off.
So we haven't just this factor and that's what we're going to be passing here.
And then here what we return is this loss method right here.
So we define this custom binary cross entropy.
We return this last method, and this last method we take in the Y through the white print, and we
carry out the computation as we had done before.
But now note that we multiplying this by a given factor.
So here we could define our factor.
Let's have this factor and your factor.
Then before this, we'll define this factor.
Let's take this factor to be equal one, because knock is actually equal one.
Supposing you are having a different problem, you may want to modify this parameter as you wish.
Let's have this factor here.
That's fine.
Now let's rerun this and hopefully we should have no error.
So that's it?
We have no error.
That seems fine.
And then we run our training.
As you can see, the training process continues normally and we could now check out on another method
of building custom loss functions, which is that of actually building a last class which inherits from
the crash loss class.
So that said, let's add this year and then we have our class, we'll call this custom B.C.E., and
then we'll inherit from crash losses loss.
Okay, so that's it.
Now, once we have this right, we have our init method and just like we did here, we're going to pass
in a factor.
So let's have this factor.
Then we inherit methods from the parent class.
There we go.
We have our custom B.C.E. custom BCE and that's it.
So we have this right, and then we now go straight forward to creating this factor variable and takes
this value factor and that's it for the init method.
From here we're going to define a call method where this operations are going to be carried out.
Now here we have this call method and then what we take in is why true and then why Pred.
So we've taken this.
We now do exactly the same thing we did here.
We could copy this out and paste it right here.
We have our b.c.e defined operation carried out, and here we have self factor.
Okay, so everything looks fine.
We have our custom b.c.e defined invalid syntax.
There should be a dot just right here.
We run that again.
That's fine.
So we have that right.
And what we do now is take this off.
So here we have custom b.c.e, which is this class where defined all we just defined right here.
Okay.
Now we should also note that you could pass in the factor here.
We actually have to pass on the factor.
That's it.
Because we didn't we didn't specify a default value, so we have to pass that.
Now let's run this.
That's fine.
And then we rerun our training and make sure everything works well.
We get in this error.
The call takes two arguments, but three were given.
Let's get back to this and we see this error We do not put in the self.
So that's it.
And let's rerun this.
As you can see, the training went on successfully.
Now we know how to build custom loss functions with TensorFlow.
We then move on to building this custom matrix just similar to the way we did with a loss.
So here we have a custom metric we'll start with a custom method.
So we take this off and then just right here we have custom metric.
Let's add this and this.
We paste this out here and have custom accuracy.
We now get back to the documentation, TensorFlow crest and then metrics.
So right here you would have this accuracy which has been defined.
Here we go, we have binary accuracy.
So that's it.
We have the binary accuracy.
We specify why.
True, why pride and the threshold.
There we go.
We can paste this out here.
This is the loss.
Let's go back up to this metric.
Let's pick it out here.
So now we have this accuracy which takes in the white row and white bread.
So we just return.
We basically return this.
We return binary accuracy accuracy, which takes in why true and why pride.
Okay, so let's take this off and that looks fine.
Now we have our custom accuracy.
Custom accuracy.
We run this as well.
We get to this compile custom accuracy.
Fine.
We compile a model and we train the model.
Everything looks good.
You can see this custom accuracy.
Now, let's suppose we had a factor.
We are going to use this same kind of approach we had with a loss method, so we just simply copy this
out.
And then right here we have this paste stat, then we have our custom metric right here.
Here again, we have custom accuracy, custom accuracy.
We take a factor, we have this metric which changes to metric.
There we go.
And then we simply have this.
So let's just copy this out and paste right here.
So here we have this your let's copy this out.
Okay, so we have this and we now multiply this by the factor.
So yeah, again, we call the metric method and that's fine.
We run that.
We have this custom accuracy.
We haven't stopped this training year.
So let's stop this training.
That's fine.
We get back here and run.
This looks good.
Compiled and training model where all this takes one positional argument but to a given.
So let's get back to this and then include this factor on that.
And the training looks fine.
We can see our metric right here.
Our next approach will be the class based approach similar to what we did with a loss.
So here we have this custom loss class.
We'll build a custom metric class, let's add this and add this code.
So we have that and then we have our custom metric class.
This class here is a subclass of the class metric.
We have our class metric.
And then just right here we have custom accuracy.
So we have this custom accuracy and then we'll specify the name.
Let's have this name.
Let's say we want to put the custom accuracy.
Okay?
And then we pass the factor to which by default was set to one.
So that's it.
Now we have custom accuracy, we have our factor.
And then what we do is define this accuracy by calling on the add weight method, which comes with this
metric class right here.
So we have this add weight and then we'll specify the name, which is the name we've passed.
We have the initial user equals zeros.
So that's it.
You'll notice that this will look slightly different from what we saw with a custom loss, as unlike
with the last class which has this call method.
Yeah.
We would instead use three other different methods.
The first will be looking at will be the update state method we have update state.
Then next we will have result method and then the reset method.
So we have this reset right here.
The update state method here permits us, update our metrics, state the results, permits us to output
the metric values, and then the reset permits us reset the metric states.
So we have here reset states.
Here we go.
We start with us.
Update State method.
In this update state, we're going to be assigning a value to this accuracy variable.
So just right here we have several accuracy.
We assign a given value.
Now the value we're going to assign is exactly the same we had here with this binary accuracy.
So let's take this factor and then paste out your we paste it here.
So this is our self dot factor.
That's fine.
So that's it.
We assign this value to this accuracy variable, this method to takes in the Y, true, the y print,
and then with the result we have a return.
So here we have this return self, not accuracy.
That's it.
And then here for the reset states reset states.
We are going to reset the state at the end of each epoch.
So once we finish with one epoch, we reset the state and then re compute re update the state and then
output the result.
So here again we have self the accuracy, assign the value zero.
That's fine, let's run the cell.
We get this error because this is actually metrics.
So let's take this off.
We have your metrics, we run that again.
This should be fine.
Okay, so we have our custom accuracy built.
We now get into this year.
So let's take this off now and specify our custom accuracy.
Then here we specify the name.
But anyway, we've gotten default value, so we could just pass it this way.
Let's run the cell and then we start the training again.
We get this error of the state.
Got an unexpected keyword argument sample.
Wait, now we get in this error because your we didn't put the sample wait as one of our arguments.
So yeah, we should have the sample.
Wait, we're not going to be using that.
So we just set it to unknown.
Let's run this again.
Fine.
We recompile and then train our model.
We get this other error.
The input y of equal OP has type flow 32 that doesn't match type 64 of argument X.
So this is actually an error which comes up because right here when competing this binary accuracy,
we have y true and y pair of different data types.
So let's print this out and you could see that y true.
And then y pred we run this again looks.
Fine.
We get down to this year, this compilation recall or when we want to debug, we could run eagerly.
So let's run eagerly set out to true.
And that's fine.
We run this again and try to understand what's going on.
So as you could see, that's exactly what we expected.
We have this in 64.
So this is an inch and this is a float 32.
We have the Y.
True.
So let's we could cast this year.
So let's cast this value typecast and then we'll specify the D type to float 30 to float 32.
That's fine.
And then just here, we take this off.
So we run this again.
There we go.
We now compile and then train.
We get this other arrow kind of assign value to variable custom accuracy.
The variable shape and the assigned value shape aren't compatible.
We get back to this year and what we do is we're going to print out this output which we assign to the
accuracy.
Yeah, we define this output and then we simply take this out from your and have this as our output.
And now we're going to assign this output.
So since we now have the output, we could print it out so we understand exactly what we're assigning
to the accuracy.
Let's run this cell, that's fine.
And then we get to this compile and train the model.
Okay, There we go.
So now we see exactly what we're assigning.
But you'll notice that instead of just assigning a single value, we are assigning this list.
And this is the reason why we were getting that error to solve this problem.
We'll also have to understand that this output actually corresponds to whether the model's prediction
was correct or not for each and every element of the batch.
So when we have a one like this, it means the prediction was correct or the model's prediction was
correct and one and so on and so forth.
So because we've trained a model for quite a while, you see that for all this or for this particular
batch, we have kind of like 100% accuracy.
So instead of printed out 100%, what the model gives out is this list.
Now let's look at this again.
We see now that with this other batch, we have some zeros.
So out of 32, we've had two zeros.
And hence our accuracy in this case is 90% for this batch.
That said, we have to figure out a way of counting the total number of ones and then dividing by the
total all by this length, which in this case is 32 or by the batch size, which is 32, and then multiplying
by 100.
Or we could just let it like that.
So we make use of this method count non zero where yeah, we could count the number of non zeros values
very easily by just calling it and I'll be fine.
So let's copy this here and then instead of having this output will count the number of non zeros.
There we go, we count that and then we divide by the length of this output.
So we divide by the length of the output, but this lens is going to be an inch.
So we're going to cast this.
So get a float, we have that and then Z type equal to flow 32 looks fine.
Okay, so now we have that and then we could let's take off this output and then we run again.
We then compile and then train the model.
We still get another error.
So what we'll do is we're going to just check in the documentation and then you're what do we see?
We see that by default this is an end.
So your we could also cast this.
We cast this into a float.
There we go.
We have this output.
We don't really need to cast that because we could specify the data type.
So here we have the output and then the type equal to float 32.
Okay, so that's it.
Hopefully this should be okay.
Now we check, we compile the model start of training and everything looks okay.
Now we could now stop this and then get back to graph mode.
So let's run again so that we could now train even faster.
And that's it.
For this section.
We've seen how to build custom losses and metrics.
Thank you for getting right up to this point and see you next time.