-
Notifications
You must be signed in to change notification settings - Fork 187
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
赛题十五:PIRBN 子目录 1D_sine_function #536
Conversation
Thanks for your contribution! |
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.
NTK plot needs to be implemented.
paddle.framework.core.set_prim_eager_enabled(True) | ||
|
||
|
||
class Adam: |
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 adma优化器
https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/optimizer/Adam_cn.html
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.
已修改Adam
import paddle | ||
|
||
|
||
def cal_adapt(pirbn, x): |
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.
参考:
源论文Page4,Theorem 3.1.4
论文Page8 https://www.sciencedirect.com/science/article/pii/S002199912100663X
- 此函数用于计算NTK矩阵,对应论文变量名称为Kg,请补充下相关函数注释(引用论文公式xx,页数xx)
- 对关键变量如 lambda_g lambda_b1 lambda_b2 进行注释说明(特征值,计算雅可比矩阵)
- 对NTK矩阵进行可视化,尝试复现论文Fig. 1,可以参考https://github.com/PredictiveIntelligenceLab/PINNsNTK/blob/master/PINNsNTK_Poisson1D.ipynb
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.
修改为 pirbn.cal_ntk ,变量为 Kg
已增加引用公式页
可视化为 analytical_solution.output_fig subplot(2, 3, 4)
使用原tensorflow代码https://github.com/JinshuaiBai/PIRBN/tree/main/1D_sine_function,显示图和论文图Fig.1不同,设置参数mu=4, b=10.0
### Define mu
mu = 4
### Define the number of sample points
ns = 51
### Define the sample points' interval
dx = 1./(ns-1)
### Initialise sample points' coordinates
xy = np.zeros((ns, 1)).astype(np.float32)
for i in range(0, ns):
xy[i, 0] = i * dx
xy_b = np.array([[0.], [1.]])
x = [xy, xy_b]
y = [-4*mu**2*np.pi**2*np.sin(2*mu*np.pi*xy)]
### Set up raidal basis network
n_in = 1
n_out = 1
n_neu = 61
b=10.
c = [-0.1, 1.1]
y = rbn(paddle.to_tensor(xy)) | ||
y = y.numpy() | ||
plt.plot(xy, y) | ||
plt.plot(xy, xy * np.sin(xy)) |
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.
解析解请单独命名为analytical_solution, 并进行注释
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.
1
target_dir = os.path.join(os.path.dirname(__file__), "../target") | ||
if not os.path.exists(target_dir): | ||
os.mkdir(target_dir) | ||
plt.savefig(os.path.join(target_dir, "1D_nonlinear_spring.png")) |
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.
请加一张absolute 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.
analytical_solution 子图 plt.subplot(2, 3, 2) 计算absolute error
paddle.framework.core.set_prim_eager_enabled(True) | ||
|
||
|
||
class Adam: |
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.
class 可以命名为Trainer更恰当
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.
1
default_initializer=paddle.nn.initializer.Normal(mean=0.0, std=0.05), | ||
) | ||
|
||
def forward(self, inputs): # Defines the computation from inputs to outputs |
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.
此处应该是用了类似论文Page13的激活函数
尝试单独抽象出激活函数 def activate()
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.
设置激活函数 RBF_layer1.rbf_activate
@@ -0,0 +1,7 @@ | |||
numpy |
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.
这个requirements在PaddleScience主目录下已经有了
如果有新的库需要添加,请加到主目录下的requirement.txt
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.
1
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.
当前复现代码的公式有点问题,需要复现的论文指标为:Fig 1 Fig 2 Fig 3 Fig 6,具体内容见review
|
||
- 1D sine funtion (**Eq. 1** in the manuscript) | ||
|
||
**PDE**: $\frac{\partial^2 }{\partial x^2}u(x)-4\mu^2\pi^2 sin(2\mu\pi(x))=0, x\in[0,1]$ |
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.
之前的修改都很好,都是符合要求的,但是图片结果上还有一些不清楚的地方,特此说明下:
论文图片指标:
- Fig 1 :单层PINN方法(不是PIRBN),公式(13),mu = 4, 【tanh激活函数】,论文显示勉强收敛
- Fig. 2 :单层PINN方法(不是PIRBN),公式(13),改变 mu = 8, 【tanh激活函数】,病态了,论文的图显示结果很差
- Fig. 3 :单层PINN方法(不是PIRBN),改变 公式(15), mu = 4, 【tanh激活函数】,病态了,论文的图显示结果很差
- Fig. 6: 单层PIRBN(源代码实现),改变 公式(15),改变 mu = 8, 改变【高斯激活函数】,结果非常优秀,说明PIRBN很有用
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.
不同激活函数设置
def gaussian_function(self, temp_x):
x0 = (
paddle.reshape(
paddle.arange(self.n_neu, dtype=paddle.get_default_dtype()),
(1, self.n_neu),
)
* (self.c[1] - self.c[0])
/ (self.n_neu - 1)
+ self.c[0]
)
x_new = temp_x - x0
s = self.b * self.b
return paddle.exp(-(x_new * x_new) * s)
def tanh_function(self, temp_x):
return paddle.tanh(temp_x)
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.
可能源代码有点问题Fig 1我用了这篇文章的Reference源码做了一下图
PredictiveIntelligenceLab/PINNsNTK@18ef519
可能jacobian那个函数有点问题,看看和这个仓库对齐一下
jointContribution/PIRBN/pirbn.py
Outdated
import paddle | ||
|
||
|
||
class Dif(paddle.nn.Layer): |
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.
1
jointContribution/PIRBN/pirbn.py
Outdated
return [u_xx, u_b] | ||
|
||
def cal_ntk(self, x): | ||
# Formula (4), Page5, \gamma variable |
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.
jointContribution/PIRBN/pirbn.py
Outdated
gamma_g = gamma_g + paddle.sum(j**2) / n1 | ||
temp = paddle.concat((l1t[0], l1t[1].reshape((1, n_neu))), axis=1) | ||
if i == 0: | ||
# Fig.1, Page8, Kg variable |
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.
jointContribution/PIRBN/train.py
Outdated
loss.backward() | ||
self.optimizer.step() | ||
self.optimizer.clear_grad() | ||
return loss, [self.his_l1, self.his_l2] |
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.
1
jointContribution/PIRBN/pirbn.py
Outdated
def cal_ntk(self, x): | ||
# Formula (4), Page5, \gamma variable | ||
gamma_g = 0.0 | ||
gamma_b = 0.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.
同上
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.
1
jointContribution/PIRBN/train.py
Outdated
] | ||
self.y_train = paddle.to_tensor(y_train, dtype=paddle.get_default_dtype()) | ||
self.maxiter = maxiter | ||
self.his_l1 = [] |
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.
改为loss_b 并注释是boundary 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.
1
jointContribution/PIRBN/train.py
Outdated
self.y_train = paddle.to_tensor(y_train, dtype=paddle.get_default_dtype()) | ||
self.maxiter = maxiter | ||
self.his_l1 = [] | ||
self.his_l2 = [] |
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.
改为loss_g 并注释是eq 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.
1
jointContribution/PIRBN/train.py
Outdated
|
||
def evaluate(self): | ||
# compute loss | ||
loss, l1, l2 = self.Loss(self.x_train, self.y_train, self.a_g, self.a_b) |
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.
1
jointContribution/PIRBN/rbn_net.py
Outdated
+ self.c[0] | ||
) | ||
x_new = temp_x - x0 | ||
return self.rbf_activate(x_new) |
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.
如果和PINN对比,可能会出现两种激活函数 tanh 和 这个rbf_activate在论文里名称应该叫Gaussian function,改一下激活函数名
注释 #Formula (19), Page7
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.
need debug for fig 1
|
||
- 1D sine funtion (**Eq. 1** in the manuscript) | ||
|
||
**PDE**: $\frac{\partial^2 }{\partial x^2}u(x)-4\mu^2\pi^2 sin(2\mu\pi(x))=0, x\in[0,1]$ |
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.
可能源代码有点问题Fig 1我用了这篇文章的Reference源码做了一下图
PredictiveIntelligenceLab/PINNsNTK@18ef519
可能jacobian那个函数有点问题,看看和这个仓库对齐一下
jointContribution/PIRBN/pirbn.py
Outdated
return [u_xx, u_b] | ||
|
||
def cal_ntk(self, x): | ||
# Formula (4), Page5, \gamma variable |
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.
jointContribution/PIRBN/rbn_net.py
Outdated
+ self.c[0] | ||
) | ||
x_new = temp_x - x0 | ||
return self.rbf_activate(x_new) |
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.
同上
jointContribution/PIRBN/train.py
Outdated
@@ -0,0 +1,67 @@ | |||
import paddle | |||
|
|||
# Used to calculate the second-order 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.
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.
need to fix
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.
need fix
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.
Pass
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 PIRBN * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Fix
PR types
Others
PR changes
Others
Describe
PaddlePaddle/Paddle#55663
赛题十五:PaddleScience 领域前沿论文复现
实现 子目录 1D_nonlinear_spring
测试精度a_g
Tensorflow 运行
![图片](https://private-user-images.githubusercontent.com/4617245/267278123-63dbd446-3065-4782-92a0-ff0137cc030b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1MTg3ODUsIm5iZiI6MTczOTUxODQ4NSwicGF0aCI6Ii80NjE3MjQ1LzI2NzI3ODEyMy02M2RiZDQ0Ni0zMDY1LTQ3ODItOTJhMC1mZjAxMzdjYzAzMGIucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTRUMDczNDQ1WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9OGJlMjEwNjU5Y2MwZTRlOWY1YjFjYzRjNTkxZWFhNmVjZWI4YWFhZjE2NGUyODBmYmU0YmExYjIzNGExYjlhYiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.DXSvatYvMKKty_rjcJ2WNVJMWV1QMLbOYKxRJ_rXPSY)
Paddle运行
![图片](https://private-user-images.githubusercontent.com/4617245/267278044-6bf62664-ab97-4420-95ff-39fadea3a528.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1MTg3ODUsIm5iZiI6MTczOTUxODQ4NSwicGF0aCI6Ii80NjE3MjQ1LzI2NzI3ODA0NC02YmY2MjY2NC1hYjk3LTQ0MjAtOTVmZi0zOWZhZGVhM2E1MjgucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTRUMDczNDQ1WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9OGY1Mjg4YmMxM2Y0MTNiZTJmYTE2YjNmMDc2OTVjNjE1N2NhYWE1ZjVhZTlmM2EwOGRiZDg4MWEwY2ZiOTEwZCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.5LHECd5CJYsfxGKMYqKsdtN-IRqaJ-u0TErFNXtlyQM)