mindspore.ops.derivative

View Source On AtomGit
mindspore.ops.derivative(fn, primals, order)[source]

This function is designed to calculate the higher-order differentiation of a given composite function. To figure out order-th order differentiation, original inputs and order must be provided together. In particular, the value of the first-order derivative of the input is set to 1, while the others are set to 0.

Note

If primals is a tensor of int type, it will be converted to a tensor of float type.

Parameters
  • fn (Union[Cell, function]) – Function to do TaylorOperation.

  • primals (Union[Tensor, tuple[Tensor]]) – The inputs to fn.

  • order (int) – The order of differentiation.

Returns

Tuple(out_primals, out_series)

  • out_primals (Union[Tensor, list[Tensor]]) - The output of fn(primals).

  • out_series (Union[Tensor, list[Tensor]]) - The order-th order of derivative of output with respect to the inputs.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import nn
>>> mindspore.set_context(mode=mindspore.GRAPH_MODE)
>>> class Net(nn.Cell):
...     def __init__(self):
...         super().__init__()
...         self.sin = mindspore.ops.Sin()
...         self.exp = mindspore.ops.Exp()
...     def construct(self, x):
...         out1 = self.sin(x)
...         out2 = self.exp(out1)
...         return out2
>>>
>>> primals = mindspore.tensor([[1, 2], [3, 4]], mindspore.float32)
>>> order = 3
>>> net = Net()
>>> out_primals, out_series = mindspore.ops.derivative(net, primals, order)
>>> print(out_primals, out_series)
[[2.319777  2.4825778]
 [1.1515628 0.4691642]] [[-4.0515366   3.6724353 ]
 [ 0.5053504  -0.52061415]]