From 270196bc62558920bfe7fc3c279355d77fe71495 Mon Sep 17 00:00:00 2001 From: Hugh Ku Date: Wed, 3 Jun 2020 12:39:25 +0800 Subject: [PATCH 1/5] Fix HessiansV2's docstring and its default argument colocate_gradients_with_ops forwarding to HessiansV1 --- tensorflow/python/ops/gradients_impl.py | 28 ++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/tensorflow/python/ops/gradients_impl.py b/tensorflow/python/ops/gradients_impl.py index 8575ea807e4..8169b574eb2 100644 --- a/tensorflow/python/ops/gradients_impl.py +++ b/tensorflow/python/ops/gradients_impl.py @@ -446,8 +446,30 @@ 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)`. -HessiansV2.__doc__ = hessians.__doc__ + 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`. + + 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) \ No newline at end of file From 177b9a417a4316ec55cb2079c012403266019989 Mon Sep 17 00:00:00 2001 From: Hugh Ku Date: Wed, 3 Jun 2020 12:47:24 +0800 Subject: [PATCH 2/5] Update tf.hessains function argument warnings --- tensorflow/tools/compatibility/tf_upgrade_v2.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tensorflow/tools/compatibility/tf_upgrade_v2.py b/tensorflow/tools/compatibility/tf_upgrade_v2.py index d27c75fb44e..6a4143757fb 100644 --- a/tensorflow/tools/compatibility/tf_upgrade_v2.py +++ b/tensorflow/tools/compatibility/tf_upgrade_v2.py @@ -1312,6 +1312,13 @@ class TFAPIChangeSpec(ast_edits.NoUpdateSpec): "'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, From 1f0eabb2d0d3f6e2bb3109f89694f06a20e55be9 Mon Sep 17 00:00:00 2001 From: Hugh Ku Date: Wed, 3 Jun 2020 12:57:13 +0800 Subject: [PATCH 3/5] Add test case of Hessians on tf 2.0 upgration --- tensorflow/tools/compatibility/tf_upgrade_v2_test.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tensorflow/tools/compatibility/tf_upgrade_v2_test.py b/tensorflow/tools/compatibility/tf_upgrade_v2_test.py index 47b9899a6b7..f54edbe3215 100644 --- a/tensorflow/tools/compatibility/tf_upgrade_v2_test.py +++ b/tensorflow/tools/compatibility/tf_upgrade_v2_test.py @@ -1118,6 +1118,12 @@ bazel-bin/tensorflow/tools/compatibility/update/generate_v2_reorders_map 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) From 8265d5f8ebb24450b65db42324fbe0ef6845da90 Mon Sep 17 00:00:00 2001 From: Hugh Ku Date: Fri, 5 Jun 2020 13:50:00 +0800 Subject: [PATCH 4/5] Fix pylint error --- tensorflow/python/ops/gradients_impl.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tensorflow/python/ops/gradients_impl.py b/tensorflow/python/ops/gradients_impl.py index 8169b574eb2..baa781cfd87 100644 --- a/tensorflow/python/ops/gradients_impl.py +++ b/tensorflow/python/ops/gradients_impl.py @@ -472,4 +472,5 @@ def HessiansV2(ys, """ return hessians(ys, xs, name=name, colocate_gradients_with_ops=True, - gate_gradients=gate_gradients, aggregation_method=aggregation_method) \ No newline at end of file + gate_gradients=gate_gradients, + aggregation_method=aggregation_method) \ No newline at end of file From 25fdc55b8de73fe5df03b59afe011d05c48a2d54 Mon Sep 17 00:00:00 2001 From: Hugh Ku Date: Fri, 5 Jun 2020 17:46:00 +0800 Subject: [PATCH 5/5] Fix pylint error - new line break at the end --- tensorflow/python/ops/gradients_impl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow/python/ops/gradients_impl.py b/tensorflow/python/ops/gradients_impl.py index baa781cfd87..eb60c6ea4ea 100644 --- a/tensorflow/python/ops/gradients_impl.py +++ b/tensorflow/python/ops/gradients_impl.py @@ -473,4 +473,4 @@ def HessiansV2(ys, return hessians(ys, xs, name=name, colocate_gradients_with_ops=True, gate_gradients=gate_gradients, - aggregation_method=aggregation_method) \ No newline at end of file + aggregation_method=aggregation_method)