.. _deeponet: Deep-O Nets ================================================ Introduction -------------- This tutorial uses Modulus to solve anti-derivative problems. It uses both data and physics informed DeepONet to solve this problem. .. note:: This tutorial assumes that you have completed the tutorial :ref:ldc and are familiar with Modulus APIs. Problem Description --------------------- Assume a continuous function :math:u has been defined on :math:[0,1]. Define the anti-derivative operator over :math:[0,1] as .. math:: G:\quad u\mapsto G(u)(y):= \int_0^y u(s)ds. In other words, .. math:: \frac{d}{dy}G(u)(y) = u(y), \qquad G(u)(0) = 0. You're going to setup a DeepONet to learn the operator :math:G. In this case, the :math:u will be the input of branch net and the :math:y will be the input of trunk net. As the input of branch net, :math:u will be evaluated at some point in :math:[0,1]. They are not necessary to be the same as :math:y, which is the evaluation points of the output. For example, you may give the data of :math:u as :math:\{u(0),\ u(0.5),\ u(1)\} but evaluate the output at :math:\{G(u)(0.1), G(u)(0.8), G(u)(0.9)\}. This is one of the advantages of DeepONet compared with Fourier neural operator. With this structure, there are two options to train the network: data informed and physics informed. Problem 1: Data informed DeepONet --------------------------------- .. note:: The python script for this problem can be found at /examples/anti_derivative/data_informed.py. Date Preparation ~~~~~~~~~~~~~~~~ As a preparation, generate :math:10,000 1D Gaussian random field (GRF) on :math:[0,1]. Then use the quadrature tool in the scipy package to obtain their anti-derivatives which go across the origin. The data generation code can be found in examples/anti_derivate/utils.py. With this data, you can start the data informed DeepONet code. Case Setup ~~~~~~~~~~ Let us first import the necessary packages. .. literalinclude:: /examples/anti_derivative/data_informed.py :language: python :lines: 1-14 In the run function, setup the branch and trunk nets, respectively. In this case, they are all fully-connected networks. Then use these two networks to setup the DeepONet structure. The Key("u1", 1000) in branch net and the Key("u2", 1000) in the trunk net indicate the outputs of them. The size :math:1,000 are the feature size, which can be adjusted in different problems. The input key Key("x", 100) means the each input sample function has been evaluated on :math:100 points. .. literalinclude:: /examples/anti_derivative/data_informed.py :language: python :lines: 24-39 The output key Key("u", 100) means the output function has been evaluated on :math:100 points, which is the size of trunk net's input. Then import the data from the .npz file. .. literalinclude:: /examples/anti_derivative/data_informed.py :language: python :lines: 41-44 To add the data constraint, use DeepONetConstraint_Data. .. literalinclude:: /examples/anti_derivative/data_informed.py :language: python :lines: 50-59 Here, since we have :math:10,000 totally, we use :math:9,900 of them for training. In the hydra config file, set the batch size (cfg.batch_size.supervised) as :math:100. You can set a validator to verify the results. Use the rest :math:100 data to validate the results. The batch size is still :math:100. To visualize the result, draw the result with the first :math:10 data. This can be done by means of DeepONetValidatorPlotter. .. literalinclude:: /examples/anti_derivative/data_informed.py :language: python :lines: 61-69 The validation results (ground truth, DeepONet prediction, and difference, respectively) are shown as below (:numref:fig-data-deeponet-0, :numref:fig-data-deeponet-1, :numref:fig-data-deeponet-2). .. _fig-data-deeponet-0: .. figure:: /figures/data_deeponet_0.png :alt: DeepONet validation result :width: 50.0% :align: center Data informed DeepONet validation result, sample 1 .. _fig-data-deeponet-1: .. figure:: /figures/data_deeponet_1.png :alt: DeepONet validation result :width: 50.0% :align: center Data informed DeepONet validation result, sample 2 .. _fig-data-deeponet-2: .. figure:: /figures/data_deeponet_2.png :alt: DeepONet validation result :width: 50.0% :align: center Data informed DeepONet validation result, sample 3 Problem 2: Physics informed DeepONet ------------------------------------ This section uses the physics informed DeepONet to resolve the anti-derivative problem. In physics informed approach, there is no need for training, but you do need some data for validation. .. note:: The python script for this problem can be found at /examples/anti_derivative/physics_informed.py. Case Setup ~~~~~~~~~~ Most of the setup for physics informed DeepONet is the same as the data informed version. First you import the needed packages. .. literalinclude:: /examples/anti_derivative/physics_informed.py :language: python :lines: 1-15 In the run function, setup the branch and trunk nets, respectively. This part is the same as the data informed version. .. literalinclude:: /examples/anti_derivative/physics_informed.py :language: python :lines: 26-44 Then, import the data, this is same as the data informed version. .. literalinclude:: /examples/anti_derivative/physics_informed.py :language: python :lines: 46-49 Next, the main different of physics informed version compared with data informed is highlighted. First, impose the derivative constraint that :math:\frac{d}{dy}G(u)(y) = u(y). .. literalinclude:: /examples/anti_derivative/physics_informed.py :language: python :lines: 55-64 Note here that u__s is the derivative of u w.r.t s. Also impose the initial value constraint that :math:u(0)=0. The way to achieve this is to set the input of the trunk net as all zero data. Then the output function will be evaluated at only :math:0. .. literalinclude:: /examples/anti_derivative/physics_informed.py :language: python :lines: 66-75 Finally, add the validator. This is the same as data informed version. For the sake of comparison, we again use the last :math:100 data as validation. .. literalinclude:: /examples/anti_derivative/physics_informed.py :language: python :lines: 77-85 The validation results (ground truth, DeepONet prediction, and difference, respectively) are shown as below (:numref:fig-physics-deeponet-0, :numref:fig-physics-deeponet-1, :numref:fig-physics-deeponet-2). .. _fig-physics-deeponet-0: .. figure:: /figures/physics_deeponet_0.png :alt: DeepONet validation result :width: 50.0% :align: center Physics informed DeepONet validation result, sample 1 .. _fig-physics-deeponet-1: .. figure:: /figures/physics_deeponet_1.png :alt: DeepONet validation result :width: 50.0% :align: center Physics informed DeepONet validation result, sample 2 .. _fig-physics-deeponet-2: .. figure:: /figures/physics_deeponet_2.png :alt: DeepONet validation result :width: 50.0% :align: center Physics informed DeepONet validation result, sample 3