mindelec.operators.SecondOrderGrad

class mindelec.operators.SecondOrderGrad(model, input_idx1, input_idx2, output_idx)[source]

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

Parameters
  • model (Cell) – a function or network that takes a single Tensor input and returns a single Tensor.

  • input_idx1 (int) – specifies the column index of input to take the first derivative of.

  • input_idx2 (int) – specifies the column index of input to take the second derivative of.

  • output_idx (int) – specifies the column index of output.

Inputs:
  • input - The input of given function or network model.

Outputs:

Tensor.

Raises

TypeError – If the type of input_idx1, input_idx2 or output_idx is not int.

Supported Platforms:

Ascend

Examples

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