|  View source on GitHub | 
Computes f(*args, **kwargs) and its gradients wrt to args, kwargs.
tfp.math.value_and_gradient(
    f,
    *args,
    output_gradients=None,
    use_gradient_tape=False,
    auto_unpack_single_arg=True,
    has_aux=False,
    name=None,
    **kwargs
)
Used in the notebooks
| Used in the tutorials | 
|---|
The function f is invoked according to one of the following rules:
- If - fis a function of no arguments then it is called as- f().
- If - len(args) == 1,- len(kwargs) == 0,- auto_unpack_single_arg == Trueand- isinstance(args[0], (list, tuple))then- argsis presumed to be a packed sequence of args, i.e., the function is called as- f(*args[0]).
- Otherwise, the function is called as - f(*args, **kwargs).
Regardless of how f is called, gradients are computed with respect to args
and kwargs.
Examples
tfd = tfp.distributions
tfm = tfp.math
# Case 1: argless `f`.
x = tf.constant(2.)
tfm.value_and_gradient(lambda: tf.math.log(x), x)
# ==> [log(2.), 0.5]
# Case 2: packed arguments.
tfm.value_and_gradient(lambda x, y: x * tf.math.log(y), [2., 3.])
# ==> [2. * np.log(3.), (np.log(3.), 2. / 3)]
# Case 3: default.
tfm.value_and_gradient(tf.math.log, [1., 2., 3.],
                       auto_unpack_single_arg=False)
# ==> [(log(1.), log(2.), log(3.)), (1., 0.5, 0.333)]
| Args | |
|---|---|
| f | Python callableto be differentiated. Iffreturns a scalar, this
scalar will be differentiated. Iffreturns a tensor or list of tensors,
the gradient will be the sum of the gradients of each part. If desired the
sum can be weighted byoutput_gradients(see below). | 
| *args | Arguments as in f(*args, **kwargs)and basis for gradient. | 
| output_gradients | A Tensoror structure ofTensors the same size as the
resultys = f(*args, **kwargs)and holding the gradients computed for
eachyinys. This argument is forwarded to the underlying gradient
implementation (i.e., either thegrad_ysargument oftf.gradientsor
theoutput_gradientsargument oftf.GradientTape.gradient).
Default value:None. | 
| use_gradient_tape | Python boolindicating thattf.GradientTapeshould be
used rather thantf.gradientand regardless oftf.executing_eagerly().
(It is only possible to usetf.gradientwhennot use_gradient_tape and
not tf.executing_eagerly().)
Default value:False. | 
| auto_unpack_single_arg | Python boolwhich whenFalsemeans the single
arg case will not be interpreted as a list of arguments. (See case 2.)
Default value:True. | 
| has_aux | Whether f(*args, **kwargs)actually returns two outputs, the
first beingyand the second being an auxiliary output that does not get
gradients computed. | 
| name | Python strname prefixed to ops created by this function.
Default value:None(i.e.,'value_and_gradient'). | 
| **kwargs | Named arguments as in f(*args, **kwargs)and basis for gradient. |