mindspore.nn.Vjp

class mindspore.nn.Vjp(fn)[source]

Computes the dot product between a vector v and the Jacobian of the given network at the point given by the inputs.

Parameters

network (Cell) – The network that takes Tensor inputs and returns a tuple of Tensors or a Tensor.

Inputs:
  • inputs (Tensors) - The inputs to net. Must be a tuple or a list.

  • v (Tensors or Tuple of Tensors) - The vector for which the vector Jacobian product is computed. Must have the same size as the output of network.

Outputs:

A tuple with 2 Tensors or Tuple of Tensors: - net_output (Tensors or Tuple of Tensors) - The output of network(inputs). - vjp (Tensors or Tuple of Tensors) - The result of the dot product.

Examples

>>> from mindspore.nn import Vjp
>>> class Net(nn.Cell):
...     def construct(self, x, y):
...         return x**3 + y
>>> x = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
>>> y = Tensor(np.array([[1, 2], [3, 4]]).astype(np.float32))
>>> v = Tensor(np.array([[1, 1], [1, 1]]).astype(np.float32))
>>> output = Vjp(Net())(x, y, v)