Skip to content

Commit

Permalink
Update 2023-08-23-PINN的一个小Demo代码释义.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiBei committed Aug 23, 2023
1 parent ddaf353 commit 4b9d8c6
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions _posts/2023-08-23-PINN的一个小Demo代码释义.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: PINN的一个小Demo代码释义
tags: [Python,PINN,神经网络机器学习深度学习偏微分方程]
tags: [Python,PINN,神经网络,机器学习,深度学习,偏微分方程]
style: fill
color: light
description: 学习PINN,阅读到一个小demo,将其完全吃透,涉及到的知识比较广,需要好好学习一下。
Expand All @@ -23,6 +23,7 @@ $$
我们把这里的**Loss函数**分解成**两部分**
- 一部分是不在边界时,神经网络算出的 y - y‘ (也就是ode_01(x, net))和 理想的 y - y‘ (也就是0),这两者(也就是残差)的 均方误差(mse_f);
- 另一部分是已知边界 x=0 下,神经网络算出的 y 和 理想的 y = 1 ,这两者的 均方误差(mse_i)。

思路就是这么短。

#### 导包:
Expand Down Expand Up @@ -63,7 +64,7 @@ class Net(nn.Module):
return out_final
~~~

#### 单独抽出的残差计算方法
#### 抽出的残差计算方法
计算残差:f(x) - f'(x)
如果找到了一个函数 f(x),使得残差为零,即 f(x) - f'(x) = 0,那么这个函数就是微分方程的解。
~~~python
Expand Down Expand Up @@ -102,10 +103,8 @@ for epoch in range(iterations):
# 具体迭代内容
~~~


3. 在一个迭代里面(后面所有内容都在此for循环里面):
每次迭代前需要把梯度归0,应为优化器把每个参数的grad梯度属性都更改了,重新迭代前需要归0。

~~~python
# 当进行神经网络的反向传播时,梯度值会根据损失函数的变化情况计算并累积在每个参数上。
optimizer.zero_grad() # 优化器的梯度归0, 避免上一轮迭代中的梯度影响当前迭代的参数更新。
Expand Down Expand Up @@ -145,7 +144,7 @@ loss = mse_i + mse_f
~~~

5. 接着利用Loss函数开始反向扩散,交给优化器。
~~~python
~~~python
# 然后执行反向传播计算梯度,并使用优化器更新模型参数。
# pytorch自动调用反向传播算法来计算参数梯度
loss.backward() # 反向传播
Expand Down Expand Up @@ -179,11 +178,12 @@ if epoch % 1000 == 0:
# plt.pause(0.1) 用于在每次迭代后暂停,以便动态查看图形变化。
plt.pause(0.1)


#end of for
~~~


参考资料:

[PINN学习与实验(一)\_\_刘文凯\_的博客-CSDN博客](https://blog.csdn.net/qq_24211837/article/details/124383808)

[ChatGPT](https://chat.openai.com/)

0 comments on commit 4b9d8c6

Please sign in to comment.