# Python – tensorflow.math.invert_permutation()

• Last Updated : 16 Jun, 2021

TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks.

invert_permutation() is used to compute inverse permutation of a tensor.

Syntax: tensorflow.math.invert_permutation( x, name)

Parameters:

• x: It is a 1-D tensor. Allowed dtypes are int32 and int64. Tensor values should be in range [0, 4).
• name(optional): It defines the name of the operation

Returns: It returns a tensor of dtype as x.

Example 1:

## Python3

 `# importing the library` `import` `tensorflow as tf`   `# Initializing the input tensor` `a ``=` `tf.constant([``1``, ``2``, ``3``, ``0``], dtype ``=` `tf.int64)`   `# Printing the input tensor` `print``(``'a: '``, a)`   `# Calculating the result` `res ``=` `tf.math.invert_permutation(a)`   `# Printing the result` `print``(``'Result: '``, res)`

Output:

```a:  tf.Tensor([1 2 3 0], shape=(4, ), dtype=int64)
Result:  tf.Tensor([3 0 1 2], shape=(4, ), dtype=int64)```

Example 2: This example uses out of range value. It will raise InvalidArgument Error

## Python3

 `# Importing the library` `import` `tensorflow as tf`   `# Initializing the input tensor` `a ``=` `tf.constant([``1``, ``2``, ``3``, ``4``], dtype ``=` `tf.int64)`   `# Printing the input tensor` `print``(``'a: '``, a)`   `# Calculating the result` `res ``=` `tf.math.invert_permutation(a)`   `# Printing the result` `print``(``'Result: '``, res)`

Output:

```a:  tf.Tensor([1 2 3 4], shape=(4, ), dtype=int64)

---------------------------------------------------------------------------

InvalidArgumentError                      Traceback (most recent call last)

in ()
9
10 # Calculating the result
---> 11 res = tf.math.invert_permutation(a)
12
13 # Printing the result

2 frames

/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: 4 is not between 0 and 4 [Op:InvertPermutation]```

My Personal Notes arrow_drop_up
Related Articles