mindelec.operators.Grad

查看源文件
class mindelec.operators.Grad(model, argnum=0)[源代码]

计算并返回指定输出列相对于指定输入列的梯度。

参数:
  • model (Cell) - 接受Tensor输入的函数或网络。

  • argnum (int) - 指定输出采用的一阶导数的输入。默认值: 0

输入:
  • x (list) - 输入是可变长度参数。第一个输入是二维的网络输入(Tensor),最后三个输入是输入的列索引(int)、输出的列索引(int)和网络的输出(Tensor)。

输出:

输出为Tensor,为指定的输出列对指定的输入列的梯度,由输入 x 中的第二、第三个参数指定。

异常:
  • TypeError - 如果 argnum 的类型不是int。

支持平台:

Ascend

样例:

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>> from mindelec.operators import Grad
...
>>> class Net(nn.Cell):
...    def __init__(self):
...        super(Net, self).__init__()
...    def construct(self, x):
...        return x * x
...
>>> x = Tensor(np.array([[1.0, -2.0], [-3.0, 4.0]]).astype(np.float32))
>>> net = Net()
>>> out = net(x)
>>> grad = Grad(net)
>>> print(grad(x, 0, 0, out).asnumpy())
[[ 2.]
[-6.]]