Now we need to calculate the loss and actually calculating the loss in neural network is a very big
deal because what we basically do, we feed data.
Calculate the loss and change the weights.
So it's quite important and this is why we need to be very careful about it.
And it will take a big chunk of our coal.
So self.
Dot.
We start by, of course, identifying the loss.
We need a criterion.
Equals torch dot n n.
And we will use the sample mean squared error loss.
And we give an iteration value.
Of one.
So here we already finished the defining the init method.
And now what we will do is we need to define the loss function.
Function.
And we call self.
And then we, we well, we, we start by zero zero gradient.
Of course, this function is not just defining the loss, it's actually the process of calculating the
loss.
So we will actually it's it's it's many times this process or this part of the code is is they put it
in a different place like they put it in the training place.
But it like we will do different ways of writing a code but let's let's put it just in the loss function.
So, Grant.
We zero the Adam grade gradient and we will zero the optimizer gradient.
And then we will predict.
Predict equals self.
Dot model.
And self dot x.
Train.
And.
Los.
Data will equals self.
Of course, we took the extent and we predict all the values and.
Self.
Dot.
The criterion.
And we will have the predicted one and we will have also our data.
Of course not.
Why?
But not.
But why?
So here what we did is we calculate the first type of loss.
We said there are two types of losses in in pens.
The first type is the.
Data loss.
The second type is the loss.
So this is going to be the first type, which is the loss related to data.
And now what we will do is we will calculate the U.
The actual U value for the whole thing.
Dot model.
Self.
Just the same thing.
So this is going to be you.
It's actually predict it's the same.
But but we we will put it here because we need to use it to actually do the.
Derivation.
So d u over d t We said the component of burgers equation we have du over this one and we have of course
u which now we just calculated it du over.
We have du over and over x square.
Now du over and over will be calculated from this one.
I will show you how, basically.
And um.
Or do you not?
The x the whole data.
So sorry, because we have you and it will change with x and it will change with time.
So we take the data which is which is basically a grid of x and time and we will calculate the change
of you with x.
With X means with both sides the time and and the actual x.
So torch dot auto.
Auto grid.
And Dot.
Grad.
And we need to add you.
We need to add you.
Self dot x.
This is the the the the component we need to compute which is you.
This is self x, which is the component we need to basically the dominator or the the the basically
the change of you, the regarding the x in relation to x and.
The ground output.
Outputs equals torch dot ones.
Like you and this is needed in the training regarding the some change of it will measure the change
of loss when we basically to itself when we change some parameters, how the loss will change.
So this is why we put it like this.
But for this is basically it's the same.
We always put it this the same along with what's coming next.
But the most important thing for us is the change of U with change of we define it, which is in our
case, X and then retrain.
Or actually create.
Create.
Create graph.
Equals.
True.
And return retain.
The graph will also equals true.
So basically this is just a it's you want the the the computation has to to to be stored and you have
to keep it.
So this is creating the graph, creating this like storage of the computation of the over and retain
it means we keep it as as simple as this.
Now think this just housekeeping a little bit here.
I don't like.
This way, it's better.
Yeah, this looks better.
Okay.
But there's one thing here I didn't say.
Here we have to put zero.
And the reason?
Zero.
The reason If you.
If we didn't put zero, I will.
And basically show this.
So.
If we if we didn't put zero this or okay, I will just print it and I will show.
So they're putting the the actual tensor in, in in containment.
It's kind of like in a tuple.
But we don't want that.
We want the value of the tensor.
I will show it just like later.
I will show it.
So this we will print it, this one.
And we print.
Print.
Just some kind of separator and print later will show this one.
But basically is just we.
Just this way.
Later I will, of course, when we run the whole code, because it's not ready now.
Basically, this will be they will put it in in tuple and just want to get it from tuple.
It's as simple as simple as this, but I will show it anyway, like just for you to have a very clear
understanding of what's happening.
Okay, now we found the change of view to X.
Now we don't want that.
What we want is the change of U with change of T and we change of u with change of x, not x the data
this x capital x we call the data, but we're changing the whole thing.
So d u.
We take basically this one, maybe it's better than X, We just put data as I feel is better and we
need basically everything with zero.
This will be basically.
Well, a tensor everything is tensor.
But what we will get is basically we will have two values, which is the change of U in relation to
X and the change of U in relation to T.
So this we take the first one, the not this one actually the first, the second one, it will be the
time and the first one will be basically the.
The X.
Okay.
That's that's good.
That's.
That's quite useful.
Now, we need to repeat this one.
Why?
We need to repeat it?
Because we need to compute the two dimensions.
So.
We need to calculate x.
X.
So in order to do so, we do the same.
But now we change the input.
We take this input.
Which is this one should not have this.
This just looks it will have.
We want to derive the thing again so we derive it again in response to again the X and it will give
us this one.
But again, we don't want that or even that second derivative of time.
What we want is do the x x the second derivative of regarding the space and this will will be.
Everything and zero.
So this is will derive already.
The first derivative will derive it again, this means the second derivative.
And then we assign the zero.
Are we done?
Just a little bit.
We need to put the loss now.
PD.
PD equals.
And.
Again.
The list.
The Self Criterion.
Of predicted and the actual thing.
But first, let's see what is the predicted one and it will be we need to well, basically we need to
write the equation we have over dt plus u d u over d x equals a viscosity d the second derivative of
d of u and x.
This has to be all well, basically it has to be zero.
So.
So actually, we don't need this one or actually we can put here zero, but just having this way, it
will consider it zero.
So.
We need to move this guy here.
And this will be zero.
You can put it zero, but of course.
Or just by having the value itself, because at the end, what we're trying always to do is to minimize
the loss.
So by computing it as a loss is already.
Oh, well, it's already computed as a loss, so we don't really need to compare it with an actual Y
train because the value itself is the loss.
So do over.
Plus you dot.
This time we need to squeeze.
This value.
Squeeze.
And this is just again, like it's the reverse with Unsqueezed what we did, we have these values will
be separated because it's an output of the prediction.
We need to make it into a list which these losses are already formatted into this list.
Um.
So the equation just we look at it a little bit.
It's over.
This is the u over d t u d u over d.
We need to u.
And squeeze or squeeze.
Sorry.
And d u over d x.
Okay.
And.
Well, actually, we can just put it.
Minus.
0.01 divided by.
Math dot pi.
This is just a.
A basically diffusion term.
U over the x x.
Yeah.
So this is the diffusion part of the equation.
And it all has to.
B equals.
And actually we can compare it to zero.
So d u over t u d u d minus all this part.
The EU over the.
As he.
Okay.
This way.
Or the final thing.
What we need to do is we need to.
Los.
Well equals loss.
P.
D e.
Plus loss.
Date.
But actually maybe this one a little bit.
She seems dangerous.
The thing this way is better.
The U.S..
And then loss dot.
Backward.
Backpropagation.
And if self dot letter.
Divide by 100.
So every 100 steps we need to print the loss.
Print.
Self.
Dot editor.
And the loss.
And of course, we need to increase the iteration number by one.
Return.
Los.
Okay, this way.
We finished computing the loss and of course, later we will see this one.
Why I put it like this is just simply it's just the way it went out from this.
We need to a little bit modified.