📜  Python – tensorflow.math.invert_permutation()

📅  最后修改于: 2022-05-13 01:54:47.963000             🧑  作者: Mango

Python – tensorflow.math.invert_permutation()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

invert_permutation()用于计算张量的逆排列。

示例 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)


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)


输出:

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

示例 2:此示例使用超出范围的值。它将引发 InvalidArgument 错误

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)

输出:

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]