From 3ed46c325f70e2f1521b850b43d2b174101d9472 Mon Sep 17 00:00:00 2001 From: Tamara Norman Date: Mon, 14 Jan 2019 04:06:24 -0800 Subject: [PATCH] Add conversion for count_nonzero outside of package PiperOrigin-RevId: 229158255 --- tensorflow/python/ops/math_ops.py | 2 ++ tensorflow/tools/compatibility/renames_v2.py | 1 - tensorflow/tools/compatibility/tf_upgrade_v2.py | 7 +++++++ .../tools/compatibility/tf_upgrade_v2_test.py | 14 +++++++++++++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/tensorflow/python/ops/math_ops.py b/tensorflow/python/ops/math_ops.py index 248d0925384..2ae29d0eb9b 100644 --- a/tensorflow/python/ops/math_ops.py +++ b/tensorflow/python/ops/math_ops.py @@ -1339,6 +1339,8 @@ def reduce_sum(input_tensor, axis=None, keepdims=False, name=None): @tf_export(v1=["math.count_nonzero", "count_nonzero"]) @deprecation.deprecated_args( None, "keep_dims is deprecated, use keepdims instead", "keep_dims") +@deprecation.deprecated_args( + None, "reduction_indices is deprecated, use axis instead", "axis") def count_nonzero(input_tensor, axis=None, keepdims=None, diff --git a/tensorflow/tools/compatibility/renames_v2.py b/tensorflow/tools/compatibility/renames_v2.py index f143a260b92..fb2303e9dd2 100644 --- a/tensorflow/tools/compatibility/renames_v2.py +++ b/tensorflow/tools/compatibility/renames_v2.py @@ -121,7 +121,6 @@ renames = { 'tf.container': 'tf.compat.v1.container', 'tf.convert_to_tensor_or_indexed_slices': 'tf.compat.v1.convert_to_tensor_or_indexed_slices', 'tf.convert_to_tensor_or_sparse_tensor': 'tf.compat.v1.convert_to_tensor_or_sparse_tensor', - 'tf.count_nonzero': 'tf.compat.v1.count_nonzero', 'tf.count_up_to': 'tf.compat.v1.count_up_to', 'tf.create_partitioned_variables': 'tf.compat.v1.create_partitioned_variables', 'tf.cross': 'tf.linalg.cross', diff --git a/tensorflow/tools/compatibility/tf_upgrade_v2.py b/tensorflow/tools/compatibility/tf_upgrade_v2.py index fd71103cc6d..3f9032bb330 100644 --- a/tensorflow/tools/compatibility/tf_upgrade_v2.py +++ b/tensorflow/tools/compatibility/tf_upgrade_v2.py @@ -119,6 +119,11 @@ class TFAPIChangeSpec(ast_edits.APIChangeSpec): "tf.load_file_system_library": { "library_filename": "library_location", }, + "tf.count_nonzero": { + "input_tensor": "input", + "keep_dims": "keepdims", + "reduction_indices": "axis", + }, "tf.math.count_nonzero": { "input_tensor": "input", "keep_dims": "keepdims", @@ -511,6 +516,8 @@ class TFAPIChangeSpec(ast_edits.APIChangeSpec): "tf.sort", "tf.contrib.framework.argsort": "tf.argsort", + "tf.count_nonzero": + "tf.math.count_nonzero", "tf.manip.batch_to_space_nd": "tf.batch_to_space", "tf.quantize_v2": diff --git a/tensorflow/tools/compatibility/tf_upgrade_v2_test.py b/tensorflow/tools/compatibility/tf_upgrade_v2_test.py index 0adee6a566f..23446e30b12 100644 --- a/tensorflow/tools/compatibility/tf_upgrade_v2_test.py +++ b/tensorflow/tools/compatibility/tf_upgrade_v2_test.py @@ -506,7 +506,7 @@ bazel-bin/tensorflow/tools/compatibility/update/generate_v2_reorders_map "tf.nn.dropout(x, 1 - (1 - func(3 + 4.)), name=\"foo\")\n", ) - def testCountNonZeroChanges(self): + def testMathCountNonZeroChanges(self): text = ( "tf.math.count_nonzero(input_tensor=input, dtype=dtype, name=name, " "reduction_indices=axis, keep_dims=keepdims)\n" @@ -518,6 +518,18 @@ bazel-bin/tensorflow/tools/compatibility/update/generate_v2_reorders_map ) self.assertEqual(new_text, expected_text) + def testCountNonZeroChanges(self): + text = ( + "tf.count_nonzero(input_tensor=input, dtype=dtype, name=name, " + "reduction_indices=axis, keep_dims=keepdims)\n" + ) + _, unused_report, unused_errors, new_text = self._upgrade(text) + expected_text = ( + "tf.math.count_nonzero(input=input, dtype=dtype, name=name, " + "axis=axis, keepdims=keepdims)\n" + ) + self.assertEqual(new_text, expected_text) + def testRandomMultinomialToRandomCategorical(self): text = ( "tf.random.multinomial(logits, samples, seed, name, output_dtype)\n"