请提出你的问题 Please ask your question
运行DeepXDE的一个官方案例时,使用pytorch的backend运行adam优化器和L-BFGS优化器能够得到想要的结果,但转换为paddle backend后训练时得到错误的结果。
代码为: https://github.com/lululxvi/deepxde/blob/master/docs/demos/pinn_forward/allen.cahn.rst
"""Backend supported: tensorflow.compat.v1, tensorflow, pytorch, paddle
Implementation of Allen-Cahn equation example in paper https://arxiv.org/abs/2111.02801.
"""
import deepxde as dde
import numpy as np
from scipy.io import loadmat
# Import tf if using backend tensorflow.compat.v1 or tensorflow
# from deepxde.backend import tf
# Import torch if using backend pytorch
# import torch
# Import paddle if using backend paddle
import paddle
def gen_testdata():
data = loadmat("../dataset/Allen_Cahn.mat")
t = data["t"]
x = data["x"]
u = data["u"]
dt = dx = 0.01
xx, tt = np.meshgrid(x, t)
X = np.vstack((np.ravel(xx), np.ravel(tt))).T
y = u.flatten()[:, None]
return X, y
geom = dde.geometry.Interval(-1, 1)
timedomain = dde.geometry.TimeDomain(0, 1)
geomtime = dde.geometry.GeometryXTime(geom, timedomain)
d = 0.001
def pde(x, y):
dy_t = dde.grad.jacobian(y, x, i=0, j=1)
dy_xx = dde.grad.hessian(y, x, i=0, j=0)
return dy_t - d * dy_xx - 5 * (y - y**3)
# Hard restraints on initial + boundary conditions
# # Backend tensorflow.compat.v1 or tensorflow
# def output_transform(x, y):
# return x[:, 0:1]**2 * tf.cos(np.pi * x[:, 0:1]) + x[:, 1:2] * (1 - x[:, 0:1]**2) * y
# # Backend pytorch
# def output_transform(x, y):
# return x[:, 0:1]**2 * torch.cos(np.pi * x[:, 0:1]) + x[:, 1:2] * (1 - x[:, 0:1]**2) * y
# Backend paddle
def output_transform(x, y):
return x[:, 0:1]**2 * paddle.cos(np.pi * x[:, 0:1]) + x[:, 1:2] * (1 - x[:, 0:1]**2) * y
data = dde.data.TimePDE(geomtime, pde, [], num_domain=8000, num_boundary=400, num_initial=800)
net = dde.nn.FNN([2] + [20] * 3 + [1], "tanh", "Glorot normal")
net.apply_output_transform(output_transform)
model = dde.Model(data, net)
model.compile("adam", lr=1e-3)
model.train(iterations=40000)
model.compile("L-BFGS")
losshistory, train_state = model.train()
dde.saveplot(losshistory, train_state, issave=True, isplot=True)
X, y_true = gen_testdata()
y_pred = model.predict(X)
f = model.predict(X, operator=pde)
print("Mean residual:", np.mean(np.absolute(f)))
print("L2 relative error:", dde.metrics.l2_relative_error(y_true, y_pred))
np.savetxt("test.dat", np.hstack((X, y_true, y_pred)))
使用pytorch backend在L-BFGS优化器开始时的loss为:
使用paddle backend在L-BFGS优化器开始时的loss为:
如果能提供帮助的话十分感谢!
1条答案
按热度按时间vxf3dgd41#
感谢您的反馈,可以拉一下DeepXDE最新代码,应该已经改了