Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the docs and parameters warning of Hessians with tf 2.0 upgrader #40109

Merged
merged 5 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 26 additions & 3 deletions tensorflow/python/ops/gradients_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,31 @@ def HessiansV2(ys,
gate_gradients=False,
aggregation_method=None,
name="hessians"):
return hessians(ys, xs, name=name, gate_gradients=gate_gradients,
aggregation_method=aggregation_method)
"""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 second-order partial derivatives of a scalar
tensor (see https://en.wikipedia.org/wiki/Hessian_matrix for more details).

Args:
ys: A `Tensor` or list of tensors to be differentiated.
xs: A `Tensor` or list of tensors to be used for differentiation.
name: Optional name to use for grouping all the gradient ops together.
defaults to 'hessians'.
gate_gradients: See `gradients()` documentation for details.
aggregation_method: See `gradients()` documentation for details.

Returns:
A list of Hessian matrices of `sum(ys)` for each `x` in `xs`.

HessiansV2.__doc__ = hessians.__doc__
Raises:
LookupError: if one of the operations between `xs` and `ys` does not
have a registered gradient function.
"""
return hessians(ys, xs, name=name,
colocate_gradients_with_ops=True,
gate_gradients=gate_gradients,
aggregation_method=aggregation_method)
7 changes: 7 additions & 0 deletions tensorflow/tools/compatibility/tf_upgrade_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,13 @@ def __init__(self, import_rename=False, upgrade_compat_v1_import=False):
"'colocate_gradients_with_ops' argument, it behaves as if it "
"was set to True."),
},
"tf.hessians": {
("colocate_gradients_with_ops", 3): (
ast_edits.INFO,
"tf.hessians no longer takes "
"'colocate_gradients_with_ops' argument, it behaves as if it "
"was set to True."),
},
"*.minimize": {
("colocate_gradients_with_ops", 5): (
ast_edits.INFO,
Expand Down
6 changes: 6 additions & 0 deletions tensorflow/tools/compatibility/tf_upgrade_v2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,12 @@ def testColocateGradientsWithOpsComputeGradients(self):
self.assertEqual("optimizer.compute_gradients(a)\n", new_text)
self.assertIn("Optimizer.compute_gradients no longer takes", report)

def testColocateGradientsWithHessians(self):
text = "tf.hessians(ys=a, xs=b, colocate_gradients_with_ops=False)\n"
_, report, unused_errors, new_text = self._upgrade(text)
self.assertEqual("tf.hessians(ys=a, xs=b)\n", new_text)
self.assertIn("tf.hessians no longer takes", report)

def testExportSavedModelRename(self):
text = "self.est.export_savedmodel(path)"
_, report, unused_errors, unused_new_text = self._upgrade(text)
Expand Down