|
| 1 | +"""Backend supported: tensorflow.compat.v1, tensorflow, pytorch, jax, paddle |
| 2 | +
|
| 3 | +Same problem as ``diffusion_1d.py``, but using |
| 4 | +``dde.callbacks.TrainingMonitor`` to watch the predicted solution over the |
| 5 | +2D (x, t) domain and the loss history update live during training, instead |
| 6 | +of only inspecting a plot after training finishes. |
| 7 | +""" |
| 8 | +import deepxde as dde |
| 9 | +import numpy as np |
| 10 | +# Backend tensorflow.compat.v1 or tensorflow |
| 11 | +from deepxde.backend import tf |
| 12 | +# Backend pytorch |
| 13 | +# import torch |
| 14 | +# Backend jax |
| 15 | +# import jax.numpy as jnp |
| 16 | +# Backend paddle |
| 17 | +# import paddle |
| 18 | + |
| 19 | + |
| 20 | +def pde(x, y): |
| 21 | + # Most backends |
| 22 | + dy_t = dde.grad.jacobian(y, x, j=1) |
| 23 | + dy_xx = dde.grad.hessian(y, x, j=0) |
| 24 | + # Backend jax |
| 25 | + # dy_t, _ = dde.grad.jacobian(y, x, j=1) |
| 26 | + # dy_xx, _ = dde.grad.hessian(y, x, j=0) |
| 27 | + # Backend tensorflow.compat.v1 or tensorflow |
| 28 | + return ( |
| 29 | + dy_t |
| 30 | + - dy_xx |
| 31 | + + tf.exp(-x[:, 1:]) |
| 32 | + * (tf.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * tf.sin(np.pi * x[:, 0:1])) |
| 33 | + ) |
| 34 | + # Backend pytorch |
| 35 | + # return ( |
| 36 | + # dy_t |
| 37 | + # - dy_xx |
| 38 | + # + torch.exp(-x[:, 1:]) |
| 39 | + # * (torch.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * torch.sin(np.pi * x[:, 0:1])) |
| 40 | + # ) |
| 41 | + # Backend jax |
| 42 | + # return ( |
| 43 | + # dy_t |
| 44 | + # - dy_xx |
| 45 | + # + jnp.exp(-x[:, 1:]) |
| 46 | + # * (jnp.sin(np.pi * x[..., 0:1]) - np.pi ** 2 * jnp.sin(np.pi * x[..., 0:1])) |
| 47 | + # ) |
| 48 | + # Backend paddle |
| 49 | + # return ( |
| 50 | + # dy_t |
| 51 | + # - dy_xx |
| 52 | + # + paddle.exp(-x[:, 1:]) |
| 53 | + # * (paddle.sin(np.pi * x[:, 0:1]) - np.pi ** 2 * paddle.sin(np.pi * x[:, 0:1])) |
| 54 | + # ) |
| 55 | + |
| 56 | + |
| 57 | +def func(x): |
| 58 | + return np.sin(np.pi * x[:, 0:1]) * np.exp(-x[:, 1:]) |
| 59 | + |
| 60 | + |
| 61 | +geom = dde.geometry.Interval(-1, 1) |
| 62 | +timedomain = dde.geometry.TimeDomain(0, 1) |
| 63 | +geomtime = dde.geometry.GeometryXTime(geom, timedomain) |
| 64 | + |
| 65 | +bc = dde.icbc.DirichletBC(geomtime, func, lambda _, on_boundary: on_boundary) |
| 66 | +ic = dde.icbc.IC(geomtime, func, lambda _, on_initial: on_initial) |
| 67 | +data = dde.data.TimePDE( |
| 68 | + geomtime, |
| 69 | + pde, |
| 70 | + [bc, ic], |
| 71 | + num_domain=40, |
| 72 | + num_boundary=20, |
| 73 | + num_initial=10, |
| 74 | + solution=func, |
| 75 | + num_test=10000, |
| 76 | +) |
| 77 | + |
| 78 | +layer_size = [2] + [32] * 3 + [1] |
| 79 | +activation = "tanh" |
| 80 | +initializer = "Glorot uniform" |
| 81 | +net = dde.nn.FNN(layer_size, activation, initializer) |
| 82 | + |
| 83 | +model = dde.Model(data, net) |
| 84 | + |
| 85 | +model.compile("adam", lr=0.001, metrics=["l2 relative error"]) |
| 86 | + |
| 87 | +# Points (x, t) at which the live plot evaluates and shows the predicted |
| 88 | +# solution as a color scatter, against the reference `func` for comparison. |
| 89 | +x_line = np.linspace(-1, 1, 40) |
| 90 | +t_line = np.linspace(0, 1, 40) |
| 91 | +x_grid, t_grid = np.meshgrid(x_line, t_line) |
| 92 | +x_plot = np.vstack((x_grid.ravel(), t_grid.ravel())).T |
| 93 | + |
| 94 | +monitor = dde.callbacks.TrainingMonitor( |
| 95 | + period=200, |
| 96 | + x_plot=x_plot, |
| 97 | + y_reference=func, |
| 98 | + show_loss=True, |
| 99 | +) |
| 100 | + |
| 101 | +losshistory, train_state = model.train(iterations=10000, callbacks=[monitor]) |
| 102 | + |
| 103 | +dde.saveplot(losshistory, train_state, issave=True, isplot=True) |
0 commit comments