PaddlePaddle/PaddleScience

报错'paddle.incubate.autograd' has no attribute 'prim_enabled', 有没有poisson 1d 方程训练的demo看一下

hugo511 opened this issue · 7 comments

import numpy as np
import matplotlib.pyplot as plt

import paddlescience as psci
import paddle
from deepxde.backend import tf


ref_rhs = lambda x: -np.sin(x) - 2 * np.sin(2 * x) - 3 * np.sin(3 * x) - 4 * np.sin(4 * x) - 8 * np.sin(8 * x)
ref_sol = lambda x: x + 1 / 8 * np.sin(8 * x) + 1 / np.sin(1 * x) + 1 / 2 * np.sin(2 * x) + 1 / 3 * np.sin(3 * x) + 1 / 4 * np.sin(4 * x)

geom = psci.geometry.Rectangular(origin = (0.), extent = (np.pi))
geom.add_boundary(name = 'left', criteria = lambda x: x==0.)
geom.add_boundary(name = 'right', criteria = lambda x : x==np.pi)
geom_disc = geom.discretize(npoints = 100, method = 'uniform')

pde = psci.pde.Poisson(dim = 2, rhs = ref_rhs, weight=1.0)
bc_left = psci.bc.Dirichlet(name = 'u', rhs = lambda x: ref_sol)
bc_right = psci.bc.Dirichlet(name = 'u', rhs = lambda x: ref_sol)
pde.add_bc('left', bc_left)
pde.add_bc('right', bc_right)
pde_disc = pde.discretize(geo_disc = geom_disc)

net = psci.network.FCNet(num_ins = 1, num_outs = 1, num_layers = 3, hidden_size = 20, activation = 'tanh')

loss = psci.loss.L2(p=2)
algo = psci.algorithm.PINNs(net = net, loss = loss)
opt = psci.optimizer.Adam(learning_rate = 0.001, parameters = net.parameters())
solver = psci.solver.Solver(pde = pde, algo = algo, opt = opt)
solution = solver.solve(num_epoch = 20000)

提示报错

 Input In [62] in <cell line: 4>
    solver = psci.solver.Solver(pde = pde, algo = algo, opt = opt)

  File D:\Anaconda3\lib\site-packages\paddlescience-1.0.0b0-py3.9.egg\paddlescience\solver\solver.py:109 in __init__
    self.__init_static()

  File D:\Anaconda3\lib\site-packages\paddlescience-1.0.0b0-py3.9.egg\paddlescience\solver\solver.py:375 in __init_static
    if config.prim_enabled() and self.pde.geometry.user is not None:

  File D:\Anaconda3\lib\site-packages\paddlescience-1.0.0b0-py3.9.egg\paddlescience\config.py:93 in prim_enabled
    return paddle.incubate.autograd.prim_enabled()

AttributeError: module 'paddle.incubate.autograd' has no attribute 'prim_enabled'

这个怎么解决? 有没有poisson 1d 方程训练的demo看一下,2d在电脑能训练,1d就挺多地方报错,不知道是哪里的问题,有demo就好了解一点

如果程序改成poisson 1d 方程的话

pde = psci.pde.Poisson(dim = 1, rhs = ref_rhs, weight=1.0)

就会报错

Traceback (most recent call last):

  Input In [11] in <cell line: 4>
    pde.add_bc('left', bc_left)

  File D:\Anaconda3\lib\site-packages\paddlescience-1.0.0b0-py3.9.egg\paddlescience\pde\pde_base.py:151 in add_bc
    self.set_bc(name, *args)

  File D:\Anaconda3\lib\site-packages\paddlescience-1.0.0b0-py3.9.egg\paddlescience\pde\pde_base.py:143 in set_bc
    if name not in self.bc:

AttributeError: 'Poisson' object has no attribute 'bc'

十分感谢反馈。Possion 1d 的问题修复 PR 已经发了,预计今天可以合入

十分感谢反馈。Possion 1d 的问题修复 PR 已经发了,预计今天可以合入

请问你们发版之后,我要怎么做才能更新,本地的包需要重新下载吗?你们发了告诉我一下hh

代码已经合入了,pull 一下就可以了。
另外你的代码中有些错误,我修复了部分,可以运行,代码如下。但是 ref_sol 应该还有问题,当 x=0 时,会出现 1.0 / 0 的情况。

import numpy as np
import matplotlib.pyplot as plt

import paddlescience as psci
import paddle

ref_rhs = lambda x: -np.sin(x) - 2 * np.sin(2 * x) - 3 * np.sin(3 * x) - 4 * np.sin(4 * x) - 8 * np.sin(8 * x)
ref_sol = lambda x: x + 1 / 8 * np.sin(8 * x) + 1 / np.sin(1 * x) + 1 / 2 * np.sin(2 * x) + 1 / 3 * np.sin(3 * x) + 1 / 4 * np.sin(4 * x)

geom = psci.geometry.Rectangular(origin = (0.), extent = (np.pi))
geom.add_boundary(name = 'left', criteria = lambda x: x==0.)
geom.add_boundary(name = 'right', criteria = lambda x : x==np.pi)
geom_disc = geom.discretize(npoints = 100, method = 'uniform')

pde = psci.pde.Poisson(dim = 1, rhs = ref_rhs, weight=1.0)
bc_left = psci.bc.Dirichlet(name = 'u', rhs = ref_sol)
bc_right = psci.bc.Dirichlet(name = 'u', rhs = ref_sol)

pde.add_bc('left', bc_left)
pde.add_bc('right', bc_right)
pde = pde.discretize(geo_disc = geom_disc)

net = psci.network.FCNet(num_ins = 1, num_outs = 1, num_layers = 3, hidden_size = 20, activation = 'tanh')

loss = psci.loss.L2(p=2)
algo = psci.algorithm.PINNs(net = net, loss = loss)
opt = psci.optimizer.Adam(learning_rate = 0.001, parameters = net.parameters())
solver = psci.solver.Solver(pde = pde, algo = algo, opt = opt)
solution = solver.solve(num_epoch = 1)

我重新安装了github最新的paddlescience, 你的代码我重新跑了一下,还是报错prim_enabled。当 x = 0, 我改了一下代码ref_sol。

import numpy as np
import matplotlib.pyplot as plt

import paddlescience as psci
import paddle

ref_rhs = lambda x: -np.sin(x) - 2 * np.sin(2 * x) - 3 * np.sin(3 * x) - 4 * np.sin(4 * x) - 8 * np.sin(8 * x)
ref_sol = lambda x: x + 1 / 8 * np.sin(8 * x) + 1 / 1 * np.sin(1 * x) + 1 / 2 * np.sin(2 * x) + 1 / 3 * np.sin(3 * x) + 1 / 4 * np.sin(4 * x)

geom = psci.geometry.Rectangular(origin = (0.), extent = (np.pi))
geom.add_boundary(name = 'left', criteria = lambda x: x==0.)
geom.add_boundary(name = 'right', criteria = lambda x : x==np.pi)
geom_disc = geom.discretize(npoints = 100, method = 'uniform')

pde = psci.pde.Poisson(dim = 1, rhs = ref_rhs, weight=1.0)
bc_left = psci.bc.Dirichlet(name = 'u', rhs = ref_sol)
bc_right = psci.bc.Dirichlet(name = 'u', rhs = ref_sol)

pde.add_bc('left', bc_left)
pde.add_bc('right', bc_right)
pde = pde.discretize(geo_disc = geom_disc)

net = psci.network.FCNet(num_ins = 1, num_outs = 1, num_layers = 3, hidden_size = 20, activation = 'tanh')

loss = psci.loss.L2(p=2)
algo = psci.algorithm.PINNs(net = net, loss = loss)
opt = psci.optimizer.Adam(learning_rate = 0.001, parameters = net.parameters())
solver = psci.solver.Solver(pde = pde, algo = algo, opt = opt)
solution = solver.solve(num_epoch = 1)

报错

Traceback (most recent call last):

  Input In [28] in <cell line: 1>
    solver = psci.solver.Solver(pde = pde, algo = algo, opt = opt)

  File D:\Anaconda3\lib\site-packages\paddlescience-1.0.0b0-py3.9.egg\paddlescience\solver\solver.py:109 in __init__
    self.__init_static()

  File D:\Anaconda3\lib\site-packages\paddlescience-1.0.0b0-py3.9.egg\paddlescience\solver\solver.py:375 in __init_static
    if config.prim_enabled() and self.pde.geometry.user is not None:

  File D:\Anaconda3\lib\site-packages\paddlescience-1.0.0b0-py3.9.egg\paddlescience\config.py:93 in prim_enabled
    return paddle.incubate.autograd.prim_enabled()

AttributeError: module 'paddle.incubate.autograd' has no attribute 'prim_enabled'

应该是环境问题,需要安装 develop 版本的 paddle。是否方便发 PR 上来呢 ?方便沟通,另外验证后可以合入 repo。

你好,PaddleScience 1.0 正式版已经发布,提供了全新设计的 API 供开发使用,可以参考用户文档进行开发
https://paddlescience-docs.readthedocs.io/zh/latest/