Okay, so now what we need to do is to write the training function.
So we start by Def Train Self.
And then self.
And of course the model is quite big.
Or we we wrote a lot of code without actually running.
So most likely we'll have some trouble.
Or like debugging.
So first one is to train the model and for I in range.
Range 1000.
We will do the Adam selfie.
Adam.
Step.
Self dot loss.
Function.
Uh, loss function.
And after that we will self dot.
Basically optimizer.
Dot step.
And actually, this guy will do his own.
The iteration.
But we have to also.
Thus the loss, which is always important.
And then death.
Evaluation eval uation.
Self.
And just a little.
Self-taught mode eval.
And.
That's it.
It seems fast, but of course, actually I didn't put.
And it's just.
Myself.
We didn't put the model declared the model.
Model equals.
Neural network.
This.
Neural network.
We didn't put it here.
To device.
Yeah, this is.
We should put it just.
Okay.
Seems good.
This one.
This one, I change its location.
And here.
Okay.
Now the moment of truth will be actual train.
Three training.
Net equals.
Net.
Basically the whole this.
And train.
Knit dot train.
And shift enter and
47.
Let's just.
Syntax.
A trained self optimizer loss function.
It.
Adam, Step.
97.
Adam step.
This seems no problem.
We go to the Adam part?
Torch Optim.
Adam.
Okay.
The Los.
Modules are.
Autograd.
Not autograd gradient.
Not grad.
So.
To.
Grant just means we have another one because we could best it.
The execs here.
Also two grand.
Total grand.
Okay.
Well, let's run it again.
All of it.
The first one is.
The grad location, this auto grad, which is.
Um.
Do we already have this size?
And this one's like, is different size.
So we have to put it here and like this and this should match, so.
Uh, we also have the error.
And we have also.
This size is not matching, so it's better.
Move.
We said we move this one here.
We just keep it as is.
So this side of the equation should be equal this side of the equation.
It's, um.
It's.
This is better because this have as a tensor it will have a different size than the other one.
So it's better to this one rather than minus.
We just separate it like this and remove this zero.
And this one just.
Another issue here.
I found that it's quite strange, actually.
Here we have when we declare.
The.
And here at.
This one has to be u X, not x train.
So line 56.
Line 56.
It's strange that we use Xtrain.
It's actually the whole domain.
We need the you the value of U for the whole domain.
So this is why it should be X, I think.
I think now it will run.
So just run it again.
Yeah, now it's running and it will take some not so much time.
And basically it's a lot of moving parts, a lot of coding.
We have to be careful just as it runs.
I will just summarize what we did so far.
It's basically we have imported the libraries, we have defined the network.
After that, we set here the model to be a neural network.
This is what we forgot before.
And the first thing is here we after the setting the model, we start computing the we start creating
the domain to actually create the data and define the initial condition and the boundary condition we
set.
The initial condition and boundary condition as we wish, which is in boundary condition, is zeros.
And the initial condition is a sine wave.
Uh, this is the.
The values of all of this.
We later after that, we define the optimizer functions, and then we define the method to calculate
the loss.
The loss means, uh, like when we have two, the two components we need to use the mean squared error.
Now, the type of loss we need to calculate is basically.
And the data loss, which we need to create a data, which is why predict and the loss of data is simply
the difference between y predicted and Y train.
And then we will calculate you and the X and the D and d, u d, t, and the second derivative of x
with a with u.
With x.
All this has to be calculated in order to put into a loss, which is simply the whole equation.
And here is this part D dt plus Udu has to equal this part.
We can move it to this area here.
But if we want to do that, we have to make sure that this side has the same tensor size as all of these
data.
So to avoid this, just keep the equation as is and put the right side to be this way.
And then this is the loss.
We calculate the whole loss based on that.
And with that we already finished and the actual convergence has been achieved.
A one thing I didn't note is basically this one why we put the zero and we just now test it here.
Here and here.
And just let's remove the zero and see how it looks without the zero and then how it looks with zero.
So we shift enter and we run it.
Of course it will blow up because it's simply.
And not.
Yeah, it's not the size is not compatible, but you can see here.
That the tensor here will will be into a tuple and we don't want that.
So we need to take this one out.
And once we put this zero, the tensor here, we don't have this, um, this parentheses thing here.
So this is why we put zero.