Now what we need to do is to calculate the losses.
Now losses we have the residual loss or what we call loss or the equation loss.
And the other loss we have is the data loss.
So the first thing is let's calculate the residual loss.
And the residual loss will basically depends on equation.
So in order to do that, let's have a class or not a class, actually.
Diff means like function.
And what we will have, we will have X, we have Y, we have T, and we have the model.
What do we need?
Now this a pde.
What it, what it going to to do is we need to calculate the.
The the basically the residual that is, um.
I will explain it.
So basically, you have this part of the equation, all of this part of the equation.
And then by moving this part to here, we will get an equation that will basically look like a like
this.
Minus.
I will move the things here.
Minus due.
RT and all of this will will equal to.
Zero.
Now.
The reality is it will not equal to zero.
And this.
This will have or not this one.
This will have a value.
So what we will do, we'll try to the loss will be we will have this equation that equals to zero.
Of course, it can work for any equation.
Just put everything on one side and equal it to zero.
But make sure later we will see.
Like this zero means every data point.
So we need to be careful about the size.
I will show how to do this.
So this is has to be zero.
If we calculate how much is this, then we will get a value.
And this value we will say, I want the loss to be zero.
So let's do it.
In order to do it first, we need to.
Well, basically, we will.
Start.
Just a minute.
Yeah, we will start by the input data.
Input data.
Equals torch dot.
Concatenate X, y, and T with a dimension equals one.
The main reason why we said like this because X, Y, and T is actually a tensor here.
We generated a tensor like we remember here a random tensor of x, a random tensor of Y and a random
tensor with t.
Now we will cut concatenate these x, y and T because we want to pass this these values to the model
and basically get the.
Input data.
Look, we get that you.
So the model will, will, will generate the data points.
So we have three tensors we use them to make.
Well, basically data.
And then this data we put them into the model and by that we generate a values of U.
After that, what we need to do is we need to.
And differentiate you.
And this is another.
Like another way to do it, but okay, I will write it this way.
X UX and UI equals torch dot the same we did like before auto.
Grad.
The things we want to differentiate is you risk in corresponding to x, y.
Now here x, Y, or we just write two times like x here and y here.
And actually I'll just put it.
I like the simplification.
Just like this you and then we want to like, basically two x.
And this too.
Why?
Grad.
Outputs.
Equals.
Same thing we did before.
George.
Dot.
Ones like.
You.
Create.
Graph.
Equals.
True.
She threw.
And retain.
Graph.
Equals also true.
This one we repeated.
It.
Two times.
Of course we can do it like this or just just put it this way or just make it a bit concise.
UX.
You I am just worried about might be another value that will pop up here.
And yeah, need to be careful about the dimension.
So this.
This is the first one.
And what we need to use.
Actually, we're not really using this one.
What we will do is use x x.
The second derivative we will is the same, but we need to be attention of some things.
We need to use.
To.
To to use for differentiation.
This is the second derivative.
And here it will be.
For X.
Tortured, cried like not you.
It will be UX and everything will be the same.
But.
Here.
We need only zero.
And we said about this, it will well, it will make a tuple and we don't want the tuple.
We want just the value.
It would put one value, one tensor value in the tuple.
And this is how it works.
Like it's just this is of course, using PyTorch is not made only for pens, but we don't need the tuple
and we this is why we take this value of zero you and now we do the same thing with you.
Well, we.
You y y.
And we also be careful here.
You have to put UI and we have to derivate in relation to U to Y and here we have to pass y and graph,
create, create, not like create here and then here, create, retrain, retain the graph equals true.
Everything is seems in place.
The only thing we need to care about is u t.
Which is.
The division.
With time you will be with.
You should be.
So we.
But you and we derive it with time.
And we put again.
You hear?
And create graph.
Everything is seems okay.
Heat equation.
Residual.
Equals, I will put the coefficient of one.
So we put it this is going to be the equation.
Well, we have this will be one.
So this will be one multiplied.
The second derivative of u over x and the second derivative of u over y minus du over dt.
So this is one multiplied UX x.
Plus one multiplied.
We'll just make it look nice.
You y minus.
UT.
And we return.
Of course, we just can't put it all of this here.
But anyway, return residual.
And shift enter.
So we go back to the main loop and what we will do is we are going to define residual, which is the
residual error equals.
And we just pass and model.
The model is here.
Of course, the model will be we will will define it later in the actual training process.
But basically the model in the in the training class, we have to pass it.
So now we have the residual.
Calculate the loss.
Loss is in mean squared error.
Keep it simple loss.
Of course we will keep it simple.
But in usual case it might.
We need more maybe.
Or different losses.
But this is just.
It works in this simple equations.
Model dot torch Dot.
Concatenate.
Cut.
And we will add X.
Y and T initial.
And.
Uh, we need to consider the same dimension here.
Dimension equals one.
And.
Plus.
Or just.
It's not shifting.
Anyway.
So we have we need again this loss.
Mm.
I wanted to be in this direction, but somehow it's not moving anyway, you initial.
This initial value has to equal the points.
That we calculated will equal the the output of the model itself.
And.
Yeah.
And.
The loss of you boundary.
Will do the same.
X underscore b.
Y.
Aw.
Yeah.
Y underscore V and this one.
And.
Yeah, not initial because we don't really need that.
Because the time will change.
Here is already it's only zero.
And.
And we have also the other loss we have.
Now.
It's kind of working.
Is boundary condition of you.
And here we flipped it.
So why be?
We flipped it to generate more points and dimension is the same.
And.
Here we have the last error is the residual error.
So residual.
And torch dot zero like.
A residual.
So it has to be the same size as, as I said.
Now, after that, what we will do is loss dot back.
Propagation backward.
And the Optimizer.
Optimizer.
We will have a step.
If it ration.
Residual 100 equals or equals equal zero.
Basically, we need to print it every 100 step print.
Iteration.
He.
The iteration.
And Los.
Los.
This one.
Okay, so this way I think it's quite good.
Next step, of course, shift enter and we have a little bit problem.
And.
Are here.
Extra one.
Just to be safe.
I think here it's better to have it like this.
Like this will be continuation for this.
And shift into seems okay.
Okay.
This now we already finished how to define the loss.
And next step we will train this model.