mindelec.operators.Grad

class mindelec.operators.Grad(model, argnum=0)[source]

Computes and returns the gradients of the specified column of outputs with respect to the specified column of inputs.

Parameters
  • model (Cell) – a function or network that takes Tensor inputs.

  • argnum (int) – specifies which input the output takes the first derivative of. Default: 0.

Inputs:
  • x - The input is variable-length argument. Notes that the last three inputs are column index of input (int), column index of output (int) and output of network (Tensor). Besides these inputs, the first is the network inputs (Tensor), which should be two dimensions.

Outputs:

Tensor.

Raises

TypeError – If the type of argnum is not int.

Supported Platforms:

Ascend

Examples

>>> 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.]]
construct(*x)[source]

define computation to be performed