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 相关文章推荐
分享15个最受欢迎的Python开源框架
Jul 13 Python
CentOS中使用virtualenv搭建python3环境
Jun 08 Python
从请求到响应过程中django都做了哪些处理
Aug 01 Python
对python中Json与object转化的方法详解
Dec 31 Python
python实现祝福弹窗效果
Apr 07 Python
Python字符串匹配之6种方法的使用详解
Apr 08 Python
浅谈Python3中strip()、lstrip()、rstrip()用法详解
Apr 29 Python
使用Python计算玩彩票赢钱概率
Jun 26 Python
python3通过qq邮箱发送邮件以及附件
May 20 Python
python实现学生管理系统开发
Jul 24 Python
vscode调试django项目的方法
Aug 06 Python
Python批量解压&压缩文件夹的示例代码
Apr 04 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新手上路(十三)
2006/10/09 PHP
Thinkphp模板中使用自定义函数的方法
2012/09/23 PHP
destoon找回管理员密码的方法
2014/06/21 PHP
在WordPress中获取数据库字段内容和添加主题设置菜单
2016/01/11 PHP
javascript学习笔记(一) 在html中使用javascript
2012/06/18 Javascript
jQuery中element选择器用法实例
2014/12/29 Javascript
Bootstrap每天必学之模态框(Modal)插件
2016/04/26 Javascript
JavaScript实战之菜单特效
2016/08/16 Javascript
理解JavaScript原型链
2016/10/25 Javascript
jquery 手势密码插件
2017/03/17 Javascript
vue升级之路之vue-router的使用教程
2018/08/14 Javascript
Vue 框架之键盘事件、健值修饰符、双向数据绑定
2018/11/14 Javascript
Jquery的autocomplete插件用法及参数讲解
2019/03/12 jQuery
浅谈express.js框架中间件(middleware)
2019/04/07 Javascript
vue实现图片预览组件封装与使用
2019/07/13 Javascript
vue+iview框架实现左侧动态菜单功能的示例代码
2020/07/23 Javascript
vue实现下载文件流完整前后端代码
2020/11/17 Vue.js
[08:02]DOTA2牵红线 zhou神抱得美人归
2014/03/22 DOTA
Python中的urllib模块使用详解
2015/07/07 Python
python将ansible配置转为json格式实例代码
2017/05/15 Python
pandas中apply和transform方法的性能比较及区别介绍
2018/10/30 Python
python利用thrift服务读取hbase数据的方法
2018/12/27 Python
Python3.5多进程原理与用法实例分析
2019/04/05 Python
Python字典的概念及常见应用实例详解
2019/10/30 Python
Python try except异常捕获机制原理解析
2020/04/18 Python
解决pycharm编辑区显示yaml文件层级结构遇中文乱码问题
2020/04/27 Python
利用canvas实现图片压缩的示例代码
2018/07/17 HTML / CSS
力学专业毕业生自荐信
2013/11/17 职场文书
毕业生就业自荐信
2013/12/04 职场文书
三分钟演讲稿范文
2014/04/24 职场文书
小学优秀辅导员事迹材料
2014/05/11 职场文书
啦啦队口号大全
2014/06/16 职场文书
学校师德师风整改措施
2014/10/27 职场文书
试用期辞职信范文
2015/03/02 职场文书
2015年政风行风工作总结
2015/04/21 职场文书
python获取淘宝服务器时间的代码示例
2021/04/22 Python