From 9c5d331f22236b8da3f4379fa2ec87d30ac9b50d Mon Sep 17 00:00:00 2001 From: Jonathan Lee Date: Tue, 15 Nov 2022 20:45:52 -0500 Subject: [PATCH] Add documentation for Function Approximation from a Formula (#1034) --- docs/demos/function.rst | 6 ++- docs/demos/function/func.rst | 83 ++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 docs/demos/function/func.rst diff --git a/docs/demos/function.rst b/docs/demos/function.rst index 0dadc9274..d8d0785d1 100644 --- a/docs/demos/function.rst +++ b/docs/demos/function.rst @@ -6,8 +6,12 @@ Here are some demos of learning functions. Function approximation ---------------------- +.. toctree:: + :maxdepth: 1 + + function/func + - `Learning a function from a dataset `_ -- `Learning a function from the formula `_ Uncertainty quantification -------------------------- diff --git a/docs/demos/function/func.rst b/docs/demos/function/func.rst new file mode 100644 index 000000000..852a6a292 --- /dev/null +++ b/docs/demos/function/func.rst @@ -0,0 +1,83 @@ +Learning a function from a formula +=================== + +Problem setup +------------- + +We will solve a simple function approximation problem from a formula: + +.. math:: f(x) = x * \sin(5x) + +Implementation +-------------- + +This description goes through the implementation of a solver for the above function step-by-step. + +First, the DeepXDE and NumPy (``np``) modules are imported: + +.. code-block:: python + + import deepxde as dde + import numpy as np + +We begin by defining a simple function which will be approximated. + +.. code-block:: python + + def func(x): + """ + x: array_like, N x D_in + y: array_like, N x D_out + """ + return x * np.sin(5 * x) + +The argument ``x`` to ``func`` is the network input. The ``func`` simply returns the corresponding function values from the given ``x``. + +Then, we define a computational domain. We can use a built-in class ``Interval`` as follows: + +.. code-block:: python + + geom = dde.geometry.Interval(-1, 1) + +Now, we have specified the geometry, we need to define the problem using a built-in class ``Function`` + +.. code-block:: python + + num_train = 16 + num_test = 100 + data = dde.data.Function(geom, func, num_train, num_test) + +Here, we use 16 points for training sampled inside the domain, and 100 points for testing. + +Next, we choose a fully connected neural network of depth 4 (i.e., 3 hidden layers) and width 20 with ``tanh`` as the activation function and ``Glorot uniform`` as the initializer: + +.. code-block:: python + + activation = "tanh" + initializer = "Glorot uniform" + net = dde.nn.FNN([1] + [20] * 3 + [1], activation, initializer) + +Now, we have the function approximation problem and the network. We bulid a ``Model`` and choose the optimizer ``adam`` and the learning rate of ``0.001``: + +.. code-block:: python + + model = dde.Model(data, net) + model.compile("adam", lr=0.001, metrics=["l2 relative error"]) + +We then train the model for 10000 iterations: + +.. code-block:: python + + losshistory, train_state = model.train(iterations=10000) + +We also save and plot the best trained result and loss history. + +.. code-block:: python + + dde.saveplot(losshistory, train_state, issave=True, isplot=True) + +Complete code +------------- + +.. literalinclude:: ../../../examples/function/func.py + :language: python