tfp.math.value_and_gradient

View source on GitHub

Computes f(*xs) and its gradients wrt to *xs.

tfp.math.value_and_gradient(
    f, xs, output_gradients=None, use_gradient_tape=False, name=None
)

Args:

  • f: Python callable to be differentiated. If f returns a scalar, this scalar will be differentiated. If f returns a tensor or list of tensors, by default a scalar will be computed by adding all their values to produce a single scalar. If desired, the tensors can be elementwise multiplied by the tensors passed as the dy keyword argument to the returned gradient function.
  • xs: Python list of parameters of f for which to differentiate. (Can also be single Tensor.)
  • output_gradients: A Tensor or list of Tensors the same size as the result ys = f(*xs) and holding the gradients computed for each y in ys. This argument is forwarded to the underlying gradient implementation (i.e., either the grad_ys argument of tf.gradients or the output_gradients argument of tf.GradientTape.gradient).
  • use_gradient_tape: Python bool indicating that tf.GradientTape should be used regardless of tf.executing_eagerly() status. Default value: False.
  • name: Python str name prefixed to ops created by this function. Default value: None (i.e., 'value_and_gradient').

Returns:

  • y: y = f(*xs).
  • dydx: Gradient of y wrt each of xs.