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标准库内置函数complex介绍
Nov 25 Python
python中global用法实例分析
Apr 30 Python
python文件操作之目录遍历实例分析
May 20 Python
Python实现批量检测HTTP服务的状态
Oct 27 Python
Django Web开发中django-debug-toolbar的配置以及使用
May 06 Python
Python3利用Dlib19.7实现摄像头人脸识别的方法
May 11 Python
PyQt5组件读取参数的实例
Jun 25 Python
python3调用windows dos命令的例子
Aug 14 Python
Win10 安装PyCharm2019.1.1(图文教程)
Sep 29 Python
如何在python中实现随机选择
Nov 02 Python
python主线程与子线程的结束顺序实例解析
Dec 17 Python
Python操作Excel工作簿的示例代码(\*.xlsx)
Mar 23 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
PHP远程连接MYSQL数据库非常慢的解决方法
2008/07/05 PHP
PHP 一个页面执行时间类代码
2010/03/05 PHP
PHP处理CSV表格文件的常用操作方法总结
2016/07/01 PHP
Javascript 代码也可以变得优美的实现方法
2009/06/22 Javascript
13 个JavaScript 性能提升技巧分享
2012/07/26 Javascript
Asp.Net alert弹出提示信息的几种方法总结
2014/01/29 Javascript
js 模式窗口(模式对话框和非模式对话框)的使用介绍
2014/07/17 Javascript
使用jQuery仿苹果官网焦点图特效
2014/12/23 Javascript
深入理解JavaScript系列(45):代码复用模式(避免篇)详解
2015/03/04 Javascript
JavaScript里实用的原生API汇总
2015/05/14 Javascript
Vue之Watcher源码解析(2)
2017/07/19 Javascript
js 两个日期比较相差多少天的实例
2017/10/19 Javascript
vue-cli项目中使用echarts图表实例
2018/10/22 Javascript
vue使用原生js实现滚动页面跟踪导航高亮的示例代码
2018/10/25 Javascript
浅谈KOA2 Restful方式路由初探
2019/03/14 Javascript
jQuery实现的移动端图片缩放功能组件示例
2020/05/01 jQuery
javascript实现左右缓动动画函数
2020/11/25 Javascript
[00:32]DOTA2上海特级锦标赛 Ehome战队宣传片
2016/03/03 DOTA
[52:41]OG vs IG 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/20 DOTA
[02:02]特效爆炸!DOTA2珍宝之瓶待你开启
2018/08/21 DOTA
[54:09]RNG vs Liquid 2019国际邀请赛淘汰赛 败者组 BO3 第一场 8.23
2019/09/05 DOTA
有关wxpython pyqt内存占用问题分析
2014/06/09 Python
Python入门篇之列表和元组
2014/10/17 Python
Python中暂存上传图片的方法
2015/02/18 Python
pycharm 将python文件打包为exe格式的方法
2019/01/16 Python
python3应用windows api对后台程序窗口及桌面截图并保存的方法
2019/08/27 Python
python进度条显示-tqmd模块的实现示例
2020/08/23 Python
python+selenium实现12306模拟登录的步骤
2021/01/21 Python
法国综合购物网站:RueDuCommerce
2016/09/12 全球购物
Canal官网:巴西女性时尚品牌
2019/10/16 全球购物
CAD制图人员的自荐信
2014/02/07 职场文书
护士实习求职信
2014/06/22 职场文书
大学生推广普通话演讲稿
2014/09/21 职场文书
查摆剖析材料范文
2014/09/30 职场文书
专业技术职务聘任证明
2015/03/02 职场文书
2016年中秋节寄语大全
2015/12/07 职场文书