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.423332), ivy.array([[1.53, 0.7, 1.67], [0.933, 0.433, 2.07]]))