Constructs the Hessian of sum of `ys` with respect to `x` in `xs`. `hessians()` adds ops to the graph to output the Hessian matrix of `ys` with respect to `xs`. It returns a list of `Tensor` of length `len(xs)` where each tensor is the Hessian of `sum(ys)`. The Hessian is a matrix of seco
(ys,
xs,
name="hessians",
colocate_gradients_with_ops=False,
gate_gradients=False,
aggregation_method=None)
| 331 | |
| 332 | @tf_export(v1=["hessians"]) |
| 333 | def hessians(ys, |
| 334 | xs, |
| 335 | name="hessians", |
| 336 | colocate_gradients_with_ops=False, |
| 337 | gate_gradients=False, |
| 338 | aggregation_method=None): |
| 339 | """Constructs the Hessian of sum of `ys` with respect to `x` in `xs`. |
| 340 | |
| 341 | `hessians()` adds ops to the graph to output the Hessian matrix of `ys` |
| 342 | with respect to `xs`. It returns a list of `Tensor` of length `len(xs)` |
| 343 | where each tensor is the Hessian of `sum(ys)`. |
| 344 | |
| 345 | The Hessian is a matrix of second-order partial derivatives of a scalar |
| 346 | tensor (see https://en.wikipedia.org/wiki/Hessian_matrix for more details). |
| 347 | |
| 348 | Args: |
| 349 | ys: A `Tensor` or list of tensors to be differentiated. |
| 350 | xs: A `Tensor` or list of tensors to be used for differentiation. |
| 351 | name: Optional name to use for grouping all the gradient ops together. |
| 352 | defaults to 'hessians'. |
| 353 | colocate_gradients_with_ops: See `gradients()` documentation for details. |
| 354 | gate_gradients: See `gradients()` documentation for details. |
| 355 | aggregation_method: See `gradients()` documentation for details. |
| 356 | |
| 357 | Returns: |
| 358 | A list of Hessian matrices of `sum(ys)` for each `x` in `xs`. |
| 359 | |
| 360 | Raises: |
| 361 | LookupError: if one of the operations between `xs` and `ys` does not |
| 362 | have a registered gradient function. |
| 363 | """ |
| 364 | xs = gradients_util._AsList(xs) # pylint: disable=protected-access |
| 365 | kwargs = { |
| 366 | "colocate_gradients_with_ops": colocate_gradients_with_ops, |
| 367 | "gate_gradients": gate_gradients, |
| 368 | "aggregation_method": aggregation_method |
| 369 | } |
| 370 | # Compute first-order derivatives and iterate for each x in xs. |
| 371 | hessians = [] |
| 372 | _gradients = gradients(ys, xs, **kwargs) |
| 373 | for gradient, x in zip(_gradients, xs): |
| 374 | # change shape to one-dimension without graph branching |
| 375 | gradient = array_ops.reshape(gradient, [-1]) |
| 376 | |
| 377 | # Declare an iterator and tensor array loop variables for the gradients. |
| 378 | n = array_ops.size(x) |
| 379 | loop_vars = [ |
| 380 | array_ops.constant(0, dtypes.int32), |
| 381 | tensor_array_ops.TensorArray(x.dtype, n) |
| 382 | ] |
| 383 | # Iterate over all elements of the gradient and compute second order |
| 384 | # derivatives. |
| 385 | _, hessian = control_flow_ops.while_loop( |
| 386 | lambda j, _: j < n, |
| 387 | lambda j, result: (j + 1, |
| 388 | result.write(j, gradients(gradient[j], x)[0])), |
| 389 | loop_vars |
| 390 | ) |
no test coverage detected