value_and_grad#
- ivy.value_and_grad(func)[source]#
Create a function that evaluates both func and the gradient of func.
- Parameters:
func (
Callable
) – Function for which we compute the gradients of the output with respect to xs input.- Return type:
Callable
- Returns:
ret – A function that returns both func and the gradient of func.
Examples
With
ivy.Array
input:>>> 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.value_and_grad(func) >>> value_grad = grad_fn(x) >>> print(value_grad) (ivy.array(16.42333412), ivy.array([[1.5333333 , 0.69999999, 1.66666675], [0.93333334, 0.43333334, 2.0666666 ]]))