tf_quant_finance.math.value_and_gradient

Contents

Last updated: 2023-03-16.

tf_quant_finance.math.value_and_gradient#

View source

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

tf_quant_finance.math.value_and_gradient(
    f, xs, output_gradients=None, use_gradient_tape=False,
    unconnected_gradients=None, 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.

  • unconnected_gradients: An enum tf.UnconnectedGradients which specifies the gradient value returned when the given input tensors are unconnected. Default value: None, which maps to tf.UnconnectedGradients.NONE.

  • name: Python str name prefixed to ops created by this function. Default value: None (i.e., 'value_and_gradient').

Returns:#

A tuple of two elements. The first one is a Tensor representing the value of the function at xs and the second one is either a Tensor or a list of Tensors representing the gradient of f(*xs) wrt xs.

  • y: y = f(*xs).

  • dydx: Gradient of y wrt each of xs.