mindelec.loss.NetWithEval

class mindelec.loss.NetWithEval(net_without_loss, constraints, loss='l2', dataset_input_map=None)[源代码]

具有评估损失的网络封装类。

参数:
  • net_without_loss (Cell) - 无损失定义的训练网络。

  • constraints (Constraints) - pde问题的约束函数。

  • loss (Union[str, dict, Cell]) - 损失函数的名称,例如”l1”,”l2”和”mae”等。默认值:”l2”。

  • dataset_input_map (dict) - 数据集的输入映射,如果输入为None,第一列将被设置为输入。默认值:None。

输入:
  • inputs (Tensor) - 输入是可变长度参数,包含网络输入和标签。

输出:

Tuple,包含标量损失Tensor、shape为 \((N, \ldots)\) 的网络输出Tensor和shape为 \((\) 的标签Tensor。

支持平台:

Ascend

样例:

>>> import numpy as np
>>> from mindelec.loss import Constraints, NetWithEval
>>> from mindspore import Tensor, nn
>>> class Net(nn.Cell):
...     def __init__(self, input_dim, output_dim):
...         super(Net, self).__init__()
...         self.fc1 = nn.Dense(input_dim, 64)
...         self.fc2 = nn.Dense(64, output_dim)
...
...     def construct(self, *input):
...         x = input[0]
...         out = self.fc1(x)
...         out = self.fc2(out)
...         return out
>>> net = Net(3, 3)
>>> # For details about how to build the Constraints, please refer to the tutorial
>>> # document on the official website.
>>> constraints = Constraints(dataset, pde_dict)
>>> loss_network = NetWithEval(net, constraints)
>>> input = Tensor(np.ones([1000, 3]).astype(np.float32) * 0.01)
>>> label = Tensor(np.ones([1000, 3]).astype(np.float32))
>>> output_data = loss_network(input, label)