# mindflow.pde.PDEWithLoss

class mindflow.pde.PDEWithLoss(model, in_vars, out_vars, params=None, params_val=None)[源代码]

• model (mindspore.nn.Cell) - 用于训练的网络模型。

• in_vars (List[sympy.core.Symbol]) - model 的输入参数，sympy符号表示的自变量。

• out_vars (List[sympy.core.Function]) - model 的输出参数，sympy符号表示的因变量。

• params (List[sympy.core.Function]) - 问题中非输入的可学习参数。

• params_val (List[sympy.core.Function]) - 问题中非输入的可学习参数的值。

• pde 方法必须重写，用于定义sympy符号微分方程。

• get_loss 方法必须重写，用于计算符号微分方程的损失。

`Ascend` `GPU`

```>>> import numpy as np
>>> from mindflow.pde import PDEWithLoss, sympy_to_mindspore
>>> from mindspore import nn, ops, Tensor
>>> from mindspore import dtype as mstype
>>> from sympy import symbols, Function, diff
>>> class Net(nn.Cell):
...     def __init__(self, cin=2, cout=1, hidden=10):
...         super().__init__()
...         self.fc1 = nn.Dense(cin, hidden)
...         self.fc2 = nn.Dense(hidden, hidden)
...         self.fcout = nn.Dense(hidden, cout)
...         self.act = ops.Tanh()
...
...     def construct(self, x):
...         x = self.act(self.fc1(x))
...         x = self.act(self.fc2(x))
...         x = self.fcout(x)
...         return x
>>> model = Net()
>>> class MyProblem(PDEWithLoss):
...     def __init__(self, model, loss_fn=nn.MSELoss()):
...         self.x, self.y = symbols('x t')
...         self.u = Function('u')(self.x, self.y)
...         self.in_vars = [self.x, self.y]
...         self.out_vars = [self.u]
...         super(MyProblem, self).__init__(model, in_vars=self.in_vars, out_vars=self.out_vars)
...         self.loss_fn = loss_fn
...         self.bc_nodes = sympy_to_mindspore(self.bc(), self.in_vars, self.out_vars)
...
...     def pde(self):
...         my_eq = diff(self.u, (self.x, 2)) + diff(self.u, (self.y, 2)) - 4.0
...         equations = {"my_eq": my_eq}
...         return equations
...
...     def bc(self):
...         bc_eq = diff(self.u, (self.x, 1)) + diff(self.u, (self.y, 1)) - 2.0
...         equations = {"bc_eq": bc_eq}
...         return equations
...
...     def get_loss(self, pde_data, bc_data):
...         pde_res = self.parse_node(self.pde_nodes, inputs=pde_data)
...         pde_loss = self.loss_fn(pde_res[0], Tensor(np.array([0.0]), mstype.float32))
...         bc_res = self.parse_node(self.bc_nodes, inputs=bc_data)
...         bc_loss = self.loss_fn(bc_res[0], Tensor(np.array([0.0]), mstype.float32))
...         return pde_loss + bc_loss
>>> problem = MyProblem(model)
>>> print(problem.pde())
>>> print(problem.bc())
my_eq: Derivative(u(x, t), (t, 2)) + Derivative(u(x, t), (x, 2)) - 4.0
Item numbers of current derivative formula nodes: 3
bc_eq: Derivative(u(x, t), t) + Derivative(u(x, t), x) - 2.0
Item numbers of current derivative formula nodes: 3
{'my_eq': Derivative(u(x, t), (t, 2)) + Derivative(u(x, t), (x, 2)) - 4.0}
{'bc_eq': Derivative(u(x, t), t) + Derivative(u(x, t), x) - 2.0}
```
get_loss()[源代码]

parse_node(formula_nodes, inputs=None, norm=None)[源代码]

• formula_nodes (list[FormulaNode]) - 转义后的sympy表达式，可以被MindSpore识别。

• inputs (Tensor) - 网络模型的输入数据。默认值： `None`

• norm (Tensor) - 输入数据点的法向量。对于曲面上某点P处的法向量是垂直于该点的切平面的向量。默认值： `None`

list[Tensor]，偏微分方程的计算结果。

pde()[源代码]