-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【Hackathon 6th No.39】XPINN 迁移至 PaddleScience #849
【Hackathon 6th No.39】XPINN 迁移至 PaddleScience #849
Conversation
Thanks for your contribution! |
examples/xpinn/xpinn.py
Outdated
|
||
import ppsci | ||
|
||
# For the use of the second derivative: paddle.cos, paddle.exp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以删除注释里的paddle.exp,默认模式已经支持无限阶微分
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,已修改
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好像没有上传配置文件?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已上传
examples/xpinn/xpinn.py
Outdated
def xpinn_2d( | ||
x1: paddle.Tensor, | ||
y1: paddle.Tensor, | ||
u1: paddle.Tensor, | ||
x2: paddle.Tensor, | ||
y2: paddle.Tensor, | ||
u2: paddle.Tensor, | ||
x3: paddle.Tensor, | ||
y3: paddle.Tensor, | ||
u3: paddle.Tensor, | ||
xi1: paddle.Tensor, | ||
yi1: paddle.Tensor, | ||
u1i1: paddle.Tensor, | ||
u2i1: paddle.Tensor, | ||
xi2: paddle.Tensor, | ||
yi2: paddle.Tensor, | ||
u1i2: paddle.Tensor, | ||
u3i2: paddle.Tensor, | ||
ub: paddle.Tensor, | ||
ub_pred: paddle.Tensor, | ||
): | ||
u1_x = get_grad(u1, x1) | ||
u1_y = get_grad(u1, y1) | ||
u1_xx = get_grad(u1_x, x1) | ||
u1_yy = get_grad(u1_y, y1) | ||
|
||
u2_x = get_grad(u2, x2) | ||
u2_y = get_grad(u2, y2) | ||
u2_xx = get_grad(u2_x, x2) | ||
u2_yy = get_grad(u2_y, y2) | ||
|
||
u3_x = get_grad(u3, x3) | ||
u3_y = get_grad(u3, y3) | ||
u3_xx = get_grad(u3_x, x3) | ||
u3_yy = get_grad(u3_y, y3) | ||
|
||
u1i1_x = get_grad(u1i1, xi1) | ||
u1i1_y = get_grad(u1i1, yi1) | ||
u1i1_xx = get_grad(u1i1_x, xi1) | ||
u1i1_yy = get_grad(u1i1_y, yi1) | ||
|
||
u2i1_x = get_grad(u2i1, xi1) | ||
u2i1_y = get_grad(u2i1, yi1) | ||
u2i1_xx = get_grad(u2i1_x, xi1) | ||
u2i1_yy = get_grad(u2i1_y, yi1) | ||
|
||
u1i2_x = get_grad(u1i2, xi2) | ||
u1i2_y = get_grad(u1i2, yi2) | ||
u1i2_xx = get_grad(u1i2_x, xi2) | ||
u1i2_yy = get_grad(u1i2_y, yi2) | ||
|
||
u3i2_x = get_grad(u3i2, xi2) | ||
u3i2_y = get_grad(u3i2, yi2) | ||
u3i2_xx = get_grad(u3i2_x, xi2) | ||
u3i2_yy = get_grad(u3i2_y, yi2) | ||
|
||
uavgi1 = (u1i1 + u2i1) / 2 | ||
uavgi2 = (u1i2 + u3i2) / 2 | ||
|
||
# Residuals | ||
f1 = u1_xx + u1_yy - (paddle.exp(x1) + paddle.exp(y1)) | ||
f2 = u2_xx + u2_yy - (paddle.exp(x2) + paddle.exp(y2)) | ||
f3 = u3_xx + u3_yy - (paddle.exp(x3) + paddle.exp(y3)) | ||
|
||
# Residual continuity conditions on the interfaces | ||
fi1 = (u1i1_xx + u1i1_yy - (paddle.exp(xi1) + paddle.exp(yi1))) - ( | ||
u2i1_xx + u2i1_yy - (paddle.exp(xi1) + paddle.exp(yi1)) | ||
) | ||
fi2 = (u1i2_xx + u1i2_yy - (paddle.exp(xi2) + paddle.exp(yi2))) - ( | ||
u3i2_xx + u3i2_yy - (paddle.exp(xi2) + paddle.exp(yi2)) | ||
) | ||
|
||
loss1 = ( | ||
20 * paddle.mean(paddle.square(ub - ub_pred)) | ||
+ paddle.mean(paddle.square(f1)) | ||
+ 1 * paddle.mean(paddle.square(fi1)) | ||
+ 1 * paddle.mean(paddle.square(fi2)) | ||
+ 20 * paddle.mean(paddle.square(u1i1 - uavgi1)) | ||
+ 20 * paddle.mean(paddle.square(u1i2 - uavgi2)) | ||
) | ||
|
||
loss2 = ( | ||
paddle.mean(paddle.square(f2)) | ||
+ 1 * paddle.mean(paddle.square(fi1)) | ||
+ 20 * paddle.mean(paddle.square(u2i1 - uavgi1)) | ||
) | ||
|
||
loss3 = ( | ||
paddle.mean(paddle.square(f3)) | ||
+ 1 * paddle.mean(paddle.square(fi2)) | ||
+ 20 * paddle.mean(paddle.square(u3i2 - uavgi2)) | ||
) | ||
return loss1, loss2, loss3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 函数内重复代码过多,建议化简下
- 这样的XPINN写法感觉不太具备可扩展性,换一个案例这个函数就不能用了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
重新实现了XPINN的loss函数
examples/xpinn/xpinn.py
Outdated
def loss_fun( | ||
output_dict: Dict[str, paddle.Tensor], | ||
label_dict: Dict[str, paddle.Tensor], | ||
*args, | ||
): | ||
loss1, loss2, loss3 = xpinn_2d( | ||
output_dict["x_f1"], | ||
output_dict["y_f1"], | ||
output_dict["u1"], | ||
output_dict["x_f2"], | ||
output_dict["y_f2"], | ||
output_dict["u2"], | ||
output_dict["x_f3"], | ||
output_dict["y_f3"], | ||
output_dict["u3"], | ||
output_dict["xi1"], | ||
output_dict["yi1"], | ||
output_dict["u1i1"], | ||
output_dict["u2i1"], | ||
output_dict["xi2"], | ||
output_dict["yi2"], | ||
output_dict["u1i2"], | ||
output_dict["u3i2"], | ||
label_dict["ub"], | ||
output_dict["ub_pred"], | ||
) | ||
|
||
return loss1 + loss2 + loss3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上,代码化简
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
examples/xpinn/xpinn.py
Outdated
) | ||
|
||
error_u_total = paddle.linalg.norm( | ||
paddle.squeeze(u_exact) - u_pred.flatten(), 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
squeeze改为flatten,两边语义一致
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
examples/xpinn/xpinn.py
Outdated
"input_keys": ( | ||
"x_f1", | ||
"y_f1", | ||
"x_f2", | ||
"y_f2", | ||
"x_f3", | ||
"y_f3", | ||
"xi1", | ||
"yi1", | ||
"xi2", | ||
"yi2", | ||
"xb", | ||
"yb", | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
分解后的input_keys可以由基本的input_keys+XPINN生成,不建议使用hard code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
input_keys 放在了配置文件中
examples/xpinn/xpinn.py
Outdated
"xb", | ||
"yb", | ||
), | ||
"label_keys": ("ub", "u_exact", "u_exact2", "u_exact3"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
u_exact==>u_exact1?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
数据集中的名字是 u_exact
examples/xpinn/xpinn.py
Outdated
x1=input_["x_f1"], | ||
y1=input_["y_f1"], | ||
x2=input_["x_f2"], | ||
y2=input_["y_f2"], | ||
x3=input_["x_f3"], | ||
y3=input_["y_f3"], | ||
xi1=input_["xi1"], | ||
yi1=input_["yi1"], | ||
xi2=input_["xi2"], | ||
yi2=input_["yi2"], | ||
xb=input_["xb"], | ||
yb=input_["yb"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不建议使用x_f1、xb、yi1这类命名方式,看不太出来每个字段的含义
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,修改了命名
examples/xpinn/xpinn.py
Outdated
"input_keys": ( | ||
"x_f1", | ||
"y_f1", | ||
"x_f2", | ||
"y_f2", | ||
"x_f3", | ||
"y_f3", | ||
"xi1", | ||
"yi1", | ||
"xi2", | ||
"yi2", | ||
"xb", | ||
"yb", | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不建议使用x_f1、xb、yi1这类命名方式,看不太出来每个字段的含义
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,修改了命名
examples/xpinn/xpinn.py
Outdated
): | ||
for key in in_: | ||
in_[key] = paddle.cast(in_[key], paddle.float64) | ||
return in_, _label, _weight |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上,下划线前后保持一致
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
ppsci/utils/reader.py
Outdated
if not np.issubdtype(data_dict[key].dtype, np.integer): | ||
if ( | ||
not np.issubdtype(data_dict[key].dtype, np.integer) | ||
and data_dict[key].dtype != np.float64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里这么改会导致float64类型的数据不能被转换为float32,模型训练时数据与权重参数类型不匹配而报错。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
之前误以为有些算子只支持float64,已经修改。文档还在编写中。
examples/xpinn/xpinn.py
Outdated
def get_grad(outputs: paddle.Tensor, inputs: paddle.Tensor) -> paddle.Tensor: | ||
grad = paddle.grad(outputs, inputs, retain_graph=True, create_graph=True) | ||
return grad[0] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个函数看起来只被单独调用,建议放到get_second_derivatives上方,并且函数名前加上单个下划线
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
examples/xpinn/xpinn.py
Outdated
residual_func (Callable, optional): residual calculation function. Defaults to lambda x,y : x - y. | ||
""" | ||
|
||
def get_second_derivatives( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
函数前面加上单下划线
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
examples/xpinn/xpinn.py
Outdated
return grad[0] | ||
|
||
|
||
def xpinn_loss( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
函数前面加上单下划线
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
] | ||
) | ||
|
||
# the shape of label_dict["residual_u_exact"] is [22387, 1], and be cut into [18211, 1] `_eval_by_dataset`(ppsci/solver/eval.py). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个注释是什么意思呢?是输入的样本点数不等于输出的样本点数,导致在_eval_by_dataset
被丢掉?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是的,输入的样本点数小于标签点数,数据集提供的label_dict["residual_u_exact"] 本来包含了label_dict["residual2_u_exact"] 和 label_dict["residual3_u_exact"]。
examples/xpinn/xpinn.py
Outdated
error_u_total = paddle.linalg.norm( | ||
u_exact.flatten() - u_pred.flatten(), 2 | ||
) / paddle.linalg.norm(u_exact.flatten(), 2) | ||
return {"total": error_u_total} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
total==>l2_error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- cfg.TRAIN_DATA_FILE好像没有出现在cfg里面,应该是cfg.DATA_FILE?
- 精度本地测试没有问题,辛苦修改一下第一点
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* add XPINNs example * comment * add conf file * refine code * fix comment * fix data type * fix data type * Update examples/xpinn/plotting.py * Update examples/xpinn/conf/xpinn.yaml * refine code * refine doc * refine doc * fix doc * fix bugs --------- Co-authored-by: HydrogenSulfate <490868991@qq.com>
PR types
Others
PR changes
Others
Describe
原案例精度:0.04685
复现精度:0.04177