IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]Tensorflow的逆置换操作函数tf.invert_permutation

    caimouse发表于 2017-04-26 11:15:39
    love 0

    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攻略

    http://edu.csdn.net/course/detail/4495
    2. TensorFlow入门基本教程
    http://edu.csdn.net/course/detail/4369

    3. C++标准模板库从入门到精通 

    http://edu.csdn.net/course/detail/3324

    4.跟老菜鸟学C++

    http://edu.csdn.net/course/detail/2901

    5. 跟老菜鸟学python

    http://edu.csdn.net/course/detail/2592

    6. 在VC2015里学会使用tinyxml库

    http://edu.csdn.net/course/detail/2590

    7. 在Windows下SVN的版本管理与实战 

     http://edu.csdn.net/course/detail/2579

    8.Visual Studio 2015开发C++程序的基本使用 

    http://edu.csdn.net/course/detail/2570

    9.在VC2015里使用protobuf协议

    http://edu.csdn.net/course/detail/2582

    10.在VC2015里学会使用MySQL数据库

    http://edu.csdn.net/course/detail/2672




沪ICP备19023445号-2号
友情链接