diff --git a/tensorflow/python/ops/numpy_ops/np_array_ops.py b/tensorflow/python/ops/numpy_ops/np_array_ops.py index f1f930ce70b..78f9c51f917 100644 --- a/tensorflow/python/ops/numpy_ops/np_array_ops.py +++ b/tensorflow/python/ops/numpy_ops/np_array_ops.py @@ -1204,6 +1204,11 @@ def moveaxis(a, source, destination): # pylint: disable=missing-docstring and builtins.all(isinstance(x, (int, np.integer)) for x in source) and builtins.all(isinstance(x, (int, np.integer)) for x in destination) ): + # NumPy rejects a repeated axis in `source` or `destination`. + if len(set(source)) != len(source): + raise ValueError('repeated axis in `source` argument') + if len(set(destination)) != len(destination): + raise ValueError('repeated axis in `destination` argument') perm = [i for i in range(a_rank) if i not in source] for dest, src in sorted(zip(destination, source)): assert dest <= len(perm) diff --git a/tensorflow/python/ops/numpy_ops/np_array_ops_test.py b/tensorflow/python/ops/numpy_ops/np_array_ops_test.py index 43efda8d56d..3a8e0f5c6c2 100644 --- a/tensorflow/python/ops/numpy_ops/np_array_ops_test.py +++ b/tensorflow/python/ops/numpy_ops/np_array_ops_test.py @@ -1577,6 +1577,22 @@ class ArrayMethodsTest(test.TestCase): np_array_ops.moveaxis(a, -8, 0) with self.assertRaisesRegex(ValueError, 'out of bounds'): np_array_ops.moveaxis(a, 0, 8) + with self.assertRaisesRegex( + ValueError, 'repeated axis in `source` argument' + ): + np_array_ops.moveaxis(a, (0, 0), (1, 2)) + with self.assertRaisesRegex( + ValueError, 'repeated axis in `source` argument' + ): + np_array_ops.moveaxis(a, (0, -6), (1, 2)) + with self.assertRaisesRegex( + ValueError, 'repeated axis in `destination` argument' + ): + np_array_ops.moveaxis(a, (0, 1), (2, 2)) + with self.assertRaisesRegex( + ValueError, 'repeated axis in `destination` argument' + ): + np_array_ops.moveaxis(a, (0, 1), (2, -4)) def testFlip(self): np.random.seed(0)