lululxvi / deepxde

A library for scientific machine learning and physics-informed learning
https://deepxde.readthedocs.io
GNU Lesser General Public License v2.1
2.66k stars 745 forks source link

Using an already trained model for predictions #299

Closed alecjervis1 closed 2 years ago

alecjervis1 commented 3 years ago

Hello,

Thank you for all previous answers lululxvi, I am really enjoying getting to grips with deepxde!

I have another very (almost embarrassingly) simple issue that I would really appreciate if someone could help me with please. I would like to use my trained model to make predictions, for use with plots and testing the model beyond its trained bounds etc, but all the related issues I have found here use the model.predict() at the end of the model forming script - surely this will result in training a new model? Is there a way I can call the model in a new script and still use the model.predict() function?

Screenshot 2021-06-02 at 16 10 44

These are the files I currently have saved from my trained model.

Many thanks in advance, Alec

lululxvi commented 3 years ago

Yes, it is doable. See FAQ "Q: How can I use a trained model for new predictions?" and "Q: How can I save a trained model and then load the model later?"

alecjervis1 commented 3 years ago

Thank you for your response - from what I can see with the FAQ threads the model is called at the end of the training script, with variables inputted after training. Is it possible to call the model in a completely separate script?

lululxvi commented 3 years ago
model = ...
model.compile("adam", lr=0)
model.restore("model.ckpt-...", verbose=1)
y_pred = model.predict(...)
alecjervis1 commented 3 years ago

Hello, thank you again for your response - I have attempted this but I am still running into issues.

Below is my amended code:

Screenshot 2021-06-09 at 16 45 14

However, after running it for the second time (the first time trains the model), I receive the following error as well as many more along the same sort of line:

Screenshot 2021-06-09 at 16 47 59

Any help would be very much appreciated!

Thank you in advance, Alec

lululxvi commented 3 years ago

It is strange. After compile, could you try train for 0 steps before restore?

alecjervis1 commented 3 years ago

Hello,

I have swapped from using anaconda, and now the same script works fine.

Thank you for your response

alecjervis1 commented 3 years ago

Hello Lu,

I am now having no problem with restoring the model and making predictions within the trained domain, thank you!

I was wondering whether it is possible with deepxde to make predictions using a geometry/ boundary conditions that are different from those that the model was trained with?

Many thanks, Alec

lululxvi commented 3 years ago

No. The standard PINN cannot do that, unless you also include geometry/BC as the network input, and train the network with different geometry/BC. But for this type of application, I may suggest DeepONet, see https://doi.org/10.1038/s42256-021-00302-5

alecjervis1 commented 3 years ago

Thank you for your response!

How exactly would I include the geometry/ BC as a network input?

And, if they can be included as an input then this would suggest that they could be altered and fed back into the model, how much scope for their variation would be possible?

I am using the same geometry as in #74 and would be interested in exploring how well the PINN approximates for different (but similar) geometries, for example, would changing the geometry to two cooling channels occupying the same overall area be possible with use of geometry as an input?

Also, in #74 you mention that the differential equation coefficient could be varied with use of tf.where. I was wondering if you think it would be possible to link the coefficient to the forming of the geometry? As in, if I were to alter the geometry to two cooling channels, the conditions would be that: the first ring around each cooling channel would be of a coefficient, the main rectangular geometry would be of another coefficient & the geometries between these two would be of a final coefficient. It seems to be that if I could link the choosing of the coefficient to the CSG geometries then this would offer a more streamline path forward than using tf.where. If this is not possible, could you give some advice regarding tf.where please?

I look forward to hearing back from you and thank you in advance! Alec

lululxvi commented 3 years ago

Let us say p is a vector to represent different conditions such as BC, then the solution is u(x1,x2,p), i.e., u is a function of p. Here, x1 and x2 are classical coordinates. p is just some extra coordinates. Then just sample different x1, x2, and p, and do the training. also see https://github.com/lululxvi/deepxde/issues/273 So you should parameterize the geometry by using some variables p.

For the specific case you discussed, it seems that you have different coefficients in different locations. see #185, #219, #79

alecjervis1 commented 3 years ago

Thank you for your response, it is very useful and the issues you have linked are also very informative.

I am currently particularly interested with #273. The script within this issue defines the pde, which has a coefficient dependent on geometry, within the list of boundary conditions with use of .inside(x) in the condition definitions. This is sort of what I was alluding to in my previous comment as to wanting to use the simplicity of the CSG geometries when defining properties of the system.

However, when I attempt this in a script of my own it raises the error: "TypeError: 'function' object is not subscriptable".

I was wondering whether defining the pde in this way is viable, and if so where you think this error arises from?

Below is my script for the heat transfer within a square block of constant temperature along the top boundary, with a disc of different thermal diffusivity in the centre:

Screenshot 2021-06-24 at 13 32 28 Screenshot 2021-06-24 at 13 34 01

I was also wondering whether you have any suggestions on good information sources for hyperparameter optimisation? \

Again, in advance for your response, thank you kindly! Alec

lululxvi commented 3 years ago

The error may be from inside, see https://github.com/lululxvi/deepxde/issues/278

alecjervis1 commented 3 years ago

Hello, thank you again for your response.

I am now no longer getting an error, I believe the error was arising from the use of both 'on_boundary' and '.inside(x)'. I have removed the on_boundary elements from the boundary conditions relating to the pde.

Although the script is now running and giving respectable loss terms, the output is not particularly physically representative. I was wondering whether this method of defining the positionally dependent PDE within the list of boundary conditions works theoretically? Is it just a case of optimising the hyperparameters or will the NN struggle to train an accurate model with this method?

Many thanks, Alec

lululxvi commented 3 years ago

Yes, the method works in theory. But, in practice, you need to tune the hyperparameters to get a well-trained network.

alecjervis1 commented 3 years ago

Ideal, thank you very much.

Which hyperparameters will be the most important to tune for this method?

lululxvi commented 3 years ago

Network size, training points, loss weights, training epochs, learning rate

gaboperezayala commented 2 years ago

Hello @alecjervis1, I am having the same problem TypeError: 'function' object is not subscriptable. And I wonder if you could help me, I still don't see clearly how you fixed the problem. Thanks for your answer and help!!!