Tensorflow:转置函数 transpose的使用详解


Posted in Python onFebruary 11, 2020

我就废话不多说,咱直接看代码吧!

tf.transpose

transpose(
  a,
  perm=None,
  name='transpose'
)

Defined in tensorflow/python/ops/array_ops.py.

See the guides: Math > Matrix Math Functions, Tensor Transformations > Slicing and Joining

Transposes a. Permutes the dimensions according to perm.

The returned tensor's dimension i will correspond to the input dimension perm[i]. If perm is not given, it is set to (n-1…0), where n is the rank of the input tensor. Hence by default, this operation performs a regular matrix transpose on 2-D input Tensors.

For example:

x = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.transpose(x) # [[1, 4]
         # [2, 5]
         # [3, 6]]
tf.transpose(x, perm=[1, 0]) # [[1, 4]
               # [2, 5]
               # [3, 6]]
# 'perm' is more useful for n-dimensional tensors, for n > 2
x = tf.constant([[[ 1, 2, 3],
         [ 4, 5, 6]],
         [[ 7, 8, 9],
         [10, 11, 12]]])

# Take the transpose of the matrices in dimension-0
tf.transpose(x, perm=[0, 2, 1]) # [[[1, 4],
                 #  [2, 5],
                 #  [3, 6]],
                 # [[7, 10],
                 #  [8, 11],
                 #  [9, 12]]]

a的转置是根据 perm 的设定值来进行的。

返回数组的 dimension(尺寸、维度) i与输入的 perm[i]的维度相一致。如果未给定perm,默认设置为 (n-1…0),这里的 n 值是输入变量的 rank 。因此默认情况下,这个操作执行了一个正规(regular)的2维矩形的转置

例如:

x = [[1 2 3]
   [4 5 6]]

tf.transpose(x) ==> [[1 4]
           [2 5]
           [3 6]]

tf.transpose(x) 等价于:
tf.transpose(x perm=[1, 0]) ==> [[1 4]
                 [2 5]
                 [3 6]]
a=tf.constant([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
array([[[ 1, 2, 3],
    [ 4, 5, 6]],

    [[ 7, 8, 9],
    [10, 11, 12]]])

x=tf.transpose(a,[1,0,2])
array([[[ 1, 2, 3],
    [ 7, 8, 9]],

    [[ 4, 5, 6],
    [10, 11, 12]]])

x=tf.transpose(a,[0,2,1])
array([[[ 1, 4],
    [ 2, 5],
    [ 3, 6]],

    [[ 7, 10],
    [ 8, 11],
    [ 9, 12]]]) 

x=tf.transpose(a,[2,1,0])
array([[[ 1, 7],
    [ 4, 10]],

    [[ 2, 8],
    [ 5, 11]],

    [[ 3, 9],
    [ 6, 12]]])


array([[[ 1, 7],
    [ 4, 10]],

    [[ 2, 8],
    [ 5, 11]],

    [[ 3, 9],
    [ 6, 12]]])

x=tf.transpose(a,[1,2,0])
array([[[ 1, 7],
    [ 2, 8],
    [ 3, 9]],

    [[ 4, 10],
    [ 5, 11],
    [ 6, 12]]])

以上这篇Tensorflow:转置函数 transpose的使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python 判断一个进程是否存在
Apr 09 Python
python开发中range()函数用法实例分析
Nov 12 Python
实例讲解Python设计模式编程之工厂方法模式的使用
Mar 02 Python
python爬取亚马逊书籍信息代码分享
Dec 09 Python
Python SqlAlchemy动态添加数据表字段实例解析
Feb 07 Python
解决pandas 作图无法显示中文的问题
May 24 Python
python字符串查找函数的用法详解
Jul 08 Python
Python爬取破解无线网络wifi密码过程解析
Sep 17 Python
python3中的eval和exec的区别与联系
Oct 10 Python
Python动态强类型解释型语言原理解析
Mar 25 Python
Python gevent协程切换实现详解
Sep 14 Python
python中翻译功能translate模块实现方法
Dec 17 Python
tensorflow多维张量计算实例
Feb 11 #Python
python误差棒图errorbar()函数实例解析
Feb 11 #Python
解决Python3.8用pip安装turtle-0.0.2出现错误问题
Feb 11 #Python
python scatter函数用法实例详解
Feb 11 #Python
python可视化text()函数使用详解
Feb 11 #Python
python读取图片的几种方式及图像宽和高的存储顺序
Feb 11 #Python
详解Python中的分支和循环结构
Feb 11 #Python
You might like
DOTA2 玩家自创拉野攻略 特色英雄快速成长篇
2020/04/20 DOTA
Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)
2013/06/14 PHP
解析PHP 使用curl提交json格式数据
2013/06/29 PHP
php实现文件下载(支持中文文名)
2013/12/04 PHP
php使用GD库创建图片缩略图的方法
2015/06/10 PHP
PHP连接MYSQL数据库的3种常用方法
2017/02/27 PHP
详解php中生成标准uuid(guid)的方法
2019/04/28 PHP
三级下拉菜单的js实现代码
2011/05/23 Javascript
jquery 模板的应用示例
2013/11/12 Javascript
点击按钮自动加关注的代码(sina微博/QQ空间/人人网/腾讯微博)
2014/01/02 Javascript
JavaScript中length属性的使用方法
2015/06/05 Javascript
浅析JavaScript 调试方法和技巧
2015/10/22 Javascript
实践中学习AngularJS表单
2016/03/21 Javascript
JS组件Bootstrap Table布局详解
2016/05/27 Javascript
jQuery+CSS3实现四种应用广泛的导航条制作实例详解
2016/09/17 Javascript
JavaScript运动框架 多物体任意值运动(三)
2017/05/17 Javascript
JS获取短信验证码倒计时的实现代码
2017/05/22 Javascript
JS开发中基本数据类型具体有哪几种
2017/10/19 Javascript
React中的render何时执行过程
2018/04/13 Javascript
JavaScript ES6中的简写语法总结与使用技巧
2018/12/30 Javascript
python中类变量与成员变量的使用注意点总结
2017/04/29 Python
django2+uwsgi+nginx上线部署到服务器Ubuntu16.04
2018/06/26 Python
keras自动编码器实现系列之卷积自动编码器操作
2020/07/03 Python
Python高阶函数与装饰器函数的深入讲解
2020/11/10 Python
BOSE德国官网:尽探索之力,享音乐之极
2016/12/11 全球购物
澳大利亚时尚前卫设计师珠宝在线:Amber Sceats
2017/10/04 全球购物
美国潜水装备、水肺潜水和浮潜设备商店:Leisure Pro
2018/08/08 全球购物
可持续木材、生态和铝制太阳镜:Proof Eyewear
2019/07/24 全球购物
中专自荐信
2013/10/13 职场文书
护理不良事件检讨书
2014/02/06 职场文书
危货运输企业安全生产责任书
2014/07/28 职场文书
离婚案件被告代理词
2015/05/23 职场文书
pytorch MSELoss计算平均的实现方法
2021/05/12 Python
python多线程方法详解
2022/01/18 Python
js面向对象编程OOP及函数式编程FP区别
2022/07/07 Javascript
postgresql如何找到表中重复数据的行并删除
2023/05/08 MySQL