tf.invert_permutation(x,name=None)
计算序列的逆置换(inverse permutation)。
本操作是计算张量的索引的逆置换。x是一维的整数张量,表示一个以0为开始的索引数组,然后根据这里元素的值来放入整数什,计算公式如下:
y[x[i]] = i for i in [0, 1, ..., len(x) -1]
从0开始,没有重复和负数值。
例如:
# 张量 `x` 是 [3, 4, 0, 2, 1]
invert_permutation(x) ==> [2, 4, 3, 0, 1]
参数:
· x: 张量,必须是下面类型之一: int32, int64。 1-D。
· name: 操作的名称,可选。
返回值:
张量,与x类型相同。1-D。
#python 3.5.3/TensorFlow 1.0.1/win 10
#2017-04-26 蔡军生 http://blog.csdn.net/caimouse
#
#导入要使用的库
import tensorflow as tf
import numpy as np
#演示API的类
class DemoTF:
def __init__(self):
'''构造函数'''
self.num1 = tf.Variable([1, 2, 0, 3], dtype = tf.int32)
self.num2 = tf.Variable([1, 3, 0, 2], dtype = tf.int32)
self.invert1 = tf.invert_permutation(self.num1)
self.invert2 = tf.invert_permutation(self.num2)
def run_graph(self):
init = tf.global_variables_initializer() # 初始化变量
with tf.Session() as sess:
sess.run(init)
self.main(sess) #运行主函数
def main(self, sess):
'''主函数'''
print(sess.run(self.num1))
print(sess.run(self.invert1))
print('num2: \n', sess.run(self.num2))
print(sess.run(self.invert2))
#主程序入口
if __name__ == "__main__":
demo = DemoTF()
demo.run_graph()
结果输出如下:
[1 2 0 3]
[2 0 1 3]
num2:
[1 3 0 2]
[2 0 3 1]
>>>
1. TensorFlow API攻略
2. TensorFlow入门基本教程