Python TensorFlow:``tensorflow.math.invert_permutation()`` 函数详解


发布日期 : 2021-08-04 00:19:59 UTC

访问量: 10 次浏览

Python – tensorflow.math.invert\_permutation()

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

invert\_permutation()是用来计算张量的反置换的。

语法:
tensorflow.math.invert_permutation( x, name)

参数:

  • x:它是一个一维张量。允许的 dtypes 是int32和int64。张量值应该在[0, 4]范围内。
  • name(可选):它定义了该操作的名称

返回:
它返回一个dtype为x的张量。

示例 1:

# 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)

输出:

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

例子2:
这个例子使用超出范围的值。它将引发 InvalidArgument 错误

# 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]