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 linecache.getline()读取文件中特定一行的脚本
Sep 06 Python
python抓取百度首页的方法
May 19 Python
Python的爬虫框架scrapy用21行代码写一个爬虫
Apr 24 Python
Python3 中把txt数据文件读入到矩阵中的方法
Apr 27 Python
Python实现查看系统启动项功能示例
May 10 Python
Python OpenCV处理图像之滤镜和图像运算
Jul 10 Python
深入了解Django View(视图系统)
Jul 23 Python
selenium2.0中常用的python函数汇总
Aug 05 Python
python3.x 生成3维随机数组实例
Nov 28 Python
python通过opencv实现图片裁剪原理解析
Jan 19 Python
Python PIL库图片灰化处理
Apr 07 Python
【超详细】八大排序算法的各项比较以及各自特点
Mar 31 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&java(二)
2006/10/09 PHP
PHP 模拟$_PUT实现代码
2010/03/15 PHP
用PHP实现的四则运算表达式计算实现代码
2011/08/02 PHP
基于在生产环境中使用php性能测试工具xhprof的详解
2013/06/03 PHP
php抓取页面的几种方法详解
2013/06/17 PHP
解析php框架codeigniter中如何使用框架的session
2013/06/24 PHP
常见php数据文件缓存类汇总
2014/12/05 PHP
离开页面时检测表单元素是否被修改,提示保存的js代码
2010/08/25 Javascript
下载文件个别浏览器文件名乱码解决办法
2013/03/19 Javascript
JS 操作Array数组的方法及属性实例解析
2014/01/08 Javascript
js兼容pc端浏览器并有多种弹出小提示的手机端浮层控件实例
2015/04/29 Javascript
利用jQuery实现漂亮的圆形进度条倒计时插件
2015/09/30 Javascript
常用的javascript设计模式
2017/01/11 Javascript
AngularJS表格添加序号的方法
2017/03/03 Javascript
在 Node.js 中使用 async 函数的方法
2017/11/17 Javascript
layui输入框只允许输入中文且判断长度的例子
2019/09/18 Javascript
python列表操作使用示例分享
2014/02/21 Python
Python多线程编程(八):使用Event实现线程间通信
2015/04/05 Python
Python中文分词实现方法(安装pymmseg)
2016/06/14 Python
python实现支付宝当面付(扫码支付)功能
2018/05/30 Python
情人节快乐! python绘制漂亮玫瑰
2020/08/18 Python
python Pandas库基础分析之时间序列的处理详解
2019/07/13 Python
Python实现的微信红包提醒功能示例
2019/08/22 Python
python3 图片 4通道转成3通道 1通道转成3通道 图片压缩实例
2019/12/03 Python
python3 dict ndarray 存成json,并保留原数据精度的实例
2019/12/06 Python
PyTorch中的Variable变量详解
2020/01/07 Python
django在保存图像的同时压缩图像示例代码详解
2020/02/11 Python
Python轻量级web框架bottle使用方法解析
2020/06/13 Python
Python连接Mysql进行增删改查的示例代码
2020/08/03 Python
Python基于内置函数type创建新类型
2020/10/22 Python
世界领先的艺术图书出版社:TASCHEN
2018/07/23 全球购物
State Cashmere官网:半零售价可持续蒙古羊绒
2020/02/26 全球购物
2014年卫生院工作总结
2014/12/03 职场文书
党风廉政建设个人总结
2015/03/06 职场文书
2015年感恩母亲节的演讲稿
2015/03/18 职场文书
2016新年年会主持词
2015/07/06 职场文书