PINN学习与实验(一)

PINN学习与实验(一)目录所用工具数学方程模型搭建所有实现代码参考文献今天第一天接触 PINN 用深度学习的方法求解 PDE 看来是非常不错的方法 做了一个简单易懂的例子 这个例子非常适合初学者 所用工具使用了 python 和 pytorch 进行实现 python3 6toch1 10 数学方程使用一个最简单的常微分方程 f x f x 1 f x 0 2 f x f x hspace 2cm 1 f x 0 hspace 2 6cm 2 f x f x 1 f x

今天第一天接触PINN,用深度学习的方法求解PDE,看来是非常不错的方法。做了一个简单易懂的例子,这个例子非常适合初学者。做了一个小demo, 大家可以参考参考

所用工具

数学方程

模型搭建

核心-使用最简单的全连接层:

class Net(nn.Module): def __init__(self, NL, NN): # NL n个l(线性,全连接)隐藏层, NN 输入数据的维数, # NL是有多少层隐藏层 # NN是每层的神经元数量 super(Net, self).__init__() self.input_layer = nn.Linear(1, NN) self.hidden_layer = nn.linear(NN,int(NN/2))  原文这里用NN,我这里用的下采样,经过实验验证,“等采样”更优。更多情况有待我实验验证。 self.output_layer = nn.Linear(int(NN/2), 1) def forward(self, x): out = torch.tanh(self.input_layer(x)) out = torch.tanh(self.hidden_layer(out)) out_final = self.output_layer(out) return out_final 

偏微分方程定义,也就是公式(1):

def ode_01(x,net): y=net(x) y_x = autograd.grad(y, x,grad_outputs=torch.ones_like(net(x)),create_graph=True)[0] return y-y_x # y-y' = 0 

所有实现代码

一下代码复制粘贴,可直接运行:

import torch import torch.nn as nn import numpy as np import matplotlib.pyplot as plt from torch import autograd """ 用神经网络模拟微分方程,f(x)'=f(x),初始条件f(0) = 1 """ class Net(nn.Module): def __init__(self, NL, NN): # NL n个l(线性,全连接)隐藏层, NN 输入数据的维数, # NL是有多少层隐藏层 # NN是每层的神经元数量 super(Net, self).__init__() self.input_layer = nn.Linear(1, NN) self.hidden_layer = nn.Linear(NN,int(NN/2))  原文这里用NN,我这里用的下采样,经过实验验证,“等采样”更优。更多情况有待我实验验证。 self.output_layer = nn.Linear(int(NN/2), 1) def forward(self, x): out = torch.tanh(self.input_layer(x)) out = torch.tanh(self.hidden_layer(out)) out_final = self.output_layer(out) return out_final net=Net(4,20) # 4层 20个 mse_cost_function = torch.nn.MSELoss(reduction='mean') # Mean squared error 均方误差求 optimizer = torch.optim.Adam(net.parameters(),lr=1e-4) # 优化器 def ode_01(x,net): y=net(x) y_x = autograd.grad(y, x,grad_outputs=torch.ones_like(net(x)),create_graph=True)[0] return y-y_x # y-y' = 0 # requires_grad=True).unsqueeze(-1) plt.ion() # 动态图 iterations= for epoch in range(iterations): optimizer.zero_grad() # 梯度归0  求边界条件的损失函数 x_0 = torch.zeros(2000, 1) y_0 = net(x_0) mse_i = mse_cost_function(y_0, torch.ones(2000, 1)) # f(0) - 1 = 0  方程的损失函数 x_in = np.random.uniform(low=0.0, high=2.0, size=(2000, 1)) pt_x_in = autograd.Variable(torch.from_numpy(x_in).float(), requires_grad=True) # x 随机数 pt_y_colection=ode_01(pt_x_in,net) pt_all_zeros= autograd.Variable(torch.from_numpy(np.zeros((2000,1))).float(), requires_grad=False) mse_f=mse_cost_function(pt_y_colection, pt_all_zeros) # y-y' = 0 loss = mse_i + mse_f loss.backward() # 反向传播 optimizer.step() # 优化下一步。This is equivalent to : theta_new = theta_old - alpha * derivative of J w.r.t theta if epoch%1000==0: y = torch.exp(pt_x_in) # y 真实值 y_train0 = net(pt_x_in) # y 预测值 print(epoch, "Traning Loss:", loss.data) print(f'times { 
           epoch} - loss: { 
           loss.item()} - y_0: { 
           y_0}') plt.cla() plt.scatter(pt_x_in.detach().numpy(), y.detach().numpy()) plt.scatter(pt_x_in.detach().numpy(), y_train0.detach().numpy(),c='red') plt.pause(0.1) 

结果展示

参考文献


















版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/219716.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午10:03
下一篇 2026年3月17日 下午10:03


相关推荐

  • dau、mau、pcu、dnu、wau、acu、uv分别是什么意思?

    dau、mau、pcu、dnu、wau、acu、uv分别是什么意思?dau mau pcu dnu wau acu uv 的意思是什么 怎么分析 DAU DailyActiveU 日活跃用户数量 常用于反映网站 互联网应用或网络游戏的运营情况 MAU monthlyactiv 月活跃用户人数 是在线游戏的一个用户数量统计名词 数量越大意味着玩这款游戏的人越多 PCU Peakconcurre 最高

    2026年3月19日
    2
  • Cursor 快捷键速查表(macOS⽠ndows):从“会用”到“能提效”的 10 个工作流

    Cursor 快捷键速查表(macOS⽠ndows):从“会用”到“能提效”的 10 个工作流

    2026年3月16日
    2
  • 绿色自适应网址发布页html源码

    绿色自适应网址发布页html源码绿色自适应网址发布页源码如图所示 十分简洁 大气好看 自适应手机 pc 复制下面的代码就可以使用了 DOCTYPE tml html head metacharset utf 8 metaname Author content Noah metaname Keywords content metaname Keywords content metaname Author content Noah metacharset utf 8 head html

    2026年3月26日
    2
  • SSDP 协议_Smb协议

    SSDP 协议_Smb协议1.组播地址2.SSDP,简单服务发现技术组播地址为了让组播源和组播组成员进行通信,需要提供网络层组播地址,即IP组播地址。同时必须存在一种技术将IP组播地址映射为链路层的组播MAC地址。1.IP组播地址(1)       IPv4组播地址IANA(InternetAssignedNumbersAuthority,互联网编号分配委员会)将D类地址空间分配给IP

    2022年10月11日
    4
  • Nano-Banana Studio部署教程:使用Podman替代Docker的无根容器化部署方案

    Nano-Banana Studio部署教程:使用Podman替代Docker的无根容器化部署方案

    2026年3月13日
    1
  • 可重入锁和不可重入锁的区别

    可重入锁和不可重入锁的区别不可重入锁示例(同一个线程不可以重入上锁后的代码段)如下是一个不可重入锁的逻辑过程,会发现执行main方法控制台会打印执行doJob方法前,然后就会一直线程阻塞,不会打印执行doJob方法过程中,原因在于第一次上锁后,由于没有释放锁,因此执行第一次lock后isLocked=true,这个时候调用doJob()内部又一次调用了lock()由于上个线程将isLocked=true,导致再次进入的时候就进入死循环。导致线程无法执行System.out.println(“执行doJob方法过程中”);这

    2022年6月26日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号