Replies: 1 comment 1 reply
-
|
I hope this Poisson equation in 1D example can help you understand how to implement gPINN and RAS(RAR is one of the RAS methods) based on DeepXDE import numpy as np
import deepxde as dde
import deepxde.backend as bkd
import matplotlib.pyplot as plt
def pde(x, y):
# PINN
# dy_xx = dde.grad.hessian(y, x)
# res = -dy_xx - np.pi ** 2 * bkd.sin(np.pi * x)
# return res
# gPINN
dy_xx = dde.grad.hessian(y, x)
res = -dy_xx - np.pi**2 * bkd.sin(np.pi * x)
g_res = dde.grad.jacobian(res, x)
return [res, g_res]
def boundary(x, on_boundary):
return on_boundary
def func(x):
return np.sin(np.pi * x)
geom = dde.geometry.Interval(-1, 1)
bc = dde.icbc.DirichletBC(geom, func, boundary)
data = dde.data.PDE(geom, pde, bc, 16, 2, solution=func, num_test=100)
layer_size = [1] + [50] * 3 + [1]
activation = "tanh"
initializer = "Glorot uniform"
net = dde.nn.FNN(layer_size, activation, initializer)
model = dde.Model(data, net)
# PINN
# model.compile("adam", lr=0.001, metrics=["l2 relative error"])
# losshistory, train_state = model.train(iterations=10000)
# dde.saveplot(losshistory, train_state, issave=True, isplot=True)
# RAR
model.compile("adam", lr=1.0e-3)
model.train(iterations=10000)
model.compile("L-BFGS")
model.train()
X = geom.random_points(100000)
err = 1
while err > 0.005:
f = model.predict(X, operator=pde)
err_eq = np.absolute(f)
err = np.mean(err_eq)
print("Mean residual: %.3e" % (err))
x_id = np.argmax(err_eq)
print("Adding new point:", X[x_id], "\n")
data.add_anchors(X[x_id])
early_stopping = dde.callbacks.EarlyStopping(min_delta=1e-4, patience=2000)
model.compile("adam", lr=1e-3)
model.train(epochs=10000, disregard_previous_best=True, callbacks=[early_stopping])
model.compile("L-BFGS")
losshistory, train_state = model.train()
dde.saveplot(losshistory, train_state, issave=True, isplot=True) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
Is there already a framework for gradient-enhanced physics-informed neural network (gPINN) and residual-based adaptive sampling (RAS) in DeepXDE?
I read the paper related to these topics that are mentioned in the introduction markdown of DeepXDE and was wondering how we can implement them within DeepXDE.
Thanks in advance :)
Beta Was this translation helpful? Give feedback.
All reactions