grad#

ivy.grad(func, argnums=0)[source]#

Call function func, and return func’s gradients.

Parameters:
  • func (Callable) – Function for which we compute the gradients of the output with respect to xs input.

  • argnums (Union[int, Sequence[int]], default: 0) – Indices of the input arrays to compute gradients with respect to. Default is 0.

Return type:

Callable

Returns:

ret – the grad function

Examples

>>> x = ivy.array([[4.6, 2.1, 5], [2.8, 1.3, 6.2]])
>>> func = lambda x: ivy.mean(ivy.square(x))
>>> grad_fn = ivy.grad(func)
>>> grad = grad_fn(x)
>>> print(grad)
ivy.array([[1.53 , 0.7  , 1.67 ],
...        [0.933, 0.433, 2.07 ]])