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 time模块详解(常用函数实例讲解,非常好)
Apr 24 Python
pandas.dataframe按行索引表达式选取方法
Oct 30 Python
利用python和ffmpeg 批量将其他图片转换为.yuv格式的方法
Jan 08 Python
Python中typing模块与类型注解的使用方法
Aug 05 Python
python破解bilibili滑动验证码登录功能
Sep 11 Python
Python 字符串、列表、元组的截取与切片操作示例
Sep 17 Python
在Django下创建项目以及设置settings.py教程
Dec 03 Python
Python代码块及缓存机制原理详解
Dec 13 Python
零基础学python应该从哪里入手
Aug 11 Python
python中append函数用法讲解
Dec 11 Python
python tkinter实现下载进度条及抖音视频去水印原理
Feb 07 Python
Python机器学习之基于Pytorch实现猫狗分类
Jun 08 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
点评山进PR-D3L三波段收音机
2021/03/02 无线电
关于访问控制的一首PHP面试题(对属性或方法的访问控制)
2012/09/13 PHP
php环境套包 dedeampz 伪静态设置示例
2014/03/26 PHP
php中rename函数用法分析
2014/11/15 PHP
PHP实现返回JSON和XML的类分享
2015/01/28 PHP
yii用户注册表单验证实例
2015/12/26 PHP
PHP5.5基于mysqli连接MySQL数据库和读取数据操作实例详解
2019/02/16 PHP
Laravel Eloquent ORM 实现查询表中指定的字段
2019/10/17 PHP
两种简单实现菜单高亮显示的JS类代码
2010/06/27 Javascript
JavaScript高级程序设计(第3版)学习笔记4 js运算符和操作符
2012/10/11 Javascript
图标线性回归斜着移动到指定的位置
2013/08/16 Javascript
js写的方法实现上传图片之后查看大图
2014/03/05 Javascript
Bootstrap每天必学之基础排版
2015/11/20 Javascript
微信公众号 客服接口的开发实例详解
2016/09/28 Javascript
CSS3 动画卡顿性能优化的完美解决方案
2018/09/20 Javascript
Node.js 进程平滑离场剖析小结
2019/01/24 Javascript
如何基于vue-cli3.0构建功能完善的移动端架子
2019/04/24 Javascript
使用RxJS更优雅地进行定时请求详析
2019/06/02 Javascript
django js 实现表格动态标序号的实例代码
2019/07/12 Javascript
python执行等待程序直到第二天零点的方法
2015/04/23 Python
python中sys.argv参数用法实例分析
2015/05/20 Python
Python使用内置json模块解析json格式数据的方法
2017/07/20 Python
Python3实现统计单词表中每个字母出现频率的方法示例
2019/01/28 Python
Python datetime包函数简单介绍
2019/08/28 Python
Python字符串格式化f-string多种功能实现
2020/05/07 Python
解决python3中os.popen()出错的问题
2020/11/19 Python
白俄罗斯大卖场:21vek.by
2019/07/25 全球购物
意大利体育用品和运动服网上商店:Maxi Sport
2019/09/14 全球购物
shell程序中如何注释
2012/02/17 面试题
三八妇女节演讲稿
2014/05/27 职场文书
自查自纠工作总结
2014/10/15 职场文书
党员学习新党章思想汇报
2014/10/25 职场文书
2014年平安创建工作总结
2014/11/24 职场文书
刮痧观后感
2015/06/05 职场文书
神州牡丹园的导游词
2019/11/20 职场文书
Python使用UDP实现720p视频传输的操作
2021/04/24 Python