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获得时间的实例说明
Mar 25 Python
初步讲解Python中的元组概念
May 21 Python
Python入门教程之运算符与控制流
Aug 17 Python
Python错误提示:[Errno 24] Too many open files的分析与解决
Feb 16 Python
一个基于flask的web应用诞生 flask和mysql相连(4)
Apr 11 Python
详解django自定义中间件处理
Nov 21 Python
python 实现selenium断言和验证的方法
Feb 13 Python
Python实现直方图均衡基本原理解析
Aug 08 Python
Python函数必须先定义,后调用说明(函数调用函数例外)
Jun 02 Python
使用Numpy对特征中的异常值进行替换及条件替换方式
Jun 08 Python
python如何安装下载后的模块
Jul 03 Python
matplotlib bar()实现多组数据并列柱状图通用简便创建方法
Feb 24 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
关于手调机和数调机的选择
2021/03/02 无线电
用PHP实现验证码功能
2006/10/09 PHP
PHP中根据IP地址判断城市实现城市切换或跳转代码
2012/09/04 PHP
基于curl数据采集之正则处理函数get_matches的使用
2013/04/28 PHP
js继承 Base类的源码解析
2008/12/30 Javascript
XHTML下,JS浮动代码失效的问题
2009/11/12 Javascript
jQuery .tmpl(), .template()学习资料小结
2011/07/18 Javascript
jquery提交form表单时禁止重复提交的方法
2014/02/13 Javascript
window.setInterval()方法的定义和用法及offsetLeft与style.left的区别
2015/11/11 Javascript
javascript实现方法调用与方法触发小结
2016/03/26 Javascript
AngularJs验证重复密码的方法(两种)
2016/11/25 Javascript
详解JS对象封装的常用方式
2016/12/30 Javascript
详解webpack和webpack-simple中如何引入css文件
2017/06/28 Javascript
nodejs基于mssql模块连接sqlserver数据库的简单封装操作示例
2018/01/05 NodeJs
详解Vue webapp项目通过HBulider打包原生APP
2018/06/29 Javascript
微信小程序Page中data数据操作和函数调用方法
2019/05/08 Javascript
JavaScript实现猜数字游戏
2020/05/20 Javascript
python采用getopt解析命令行输入参数实例
2014/09/30 Python
python输出当前目录下index.html文件路径的方法
2015/04/28 Python
python利用有道翻译实现"语言翻译器"的功能实例
2017/11/14 Python
Python学生信息管理系统修改版
2018/03/13 Python
Python实现基于PIL和tesseract的验证码识别功能示例
2018/07/11 Python
python使用turtle绘制国际象棋棋盘
2019/05/23 Python
pyqt5实现按钮添加背景图片以及背景图片的切换方法
2019/06/13 Python
Python list运算操作代码实例解析
2020/01/20 Python
Python基于Serializer实现字段验证及序列化
2020/11/04 Python
进修护士自我鉴定
2013/10/14 职场文书
毕业生自荐书
2014/02/02 职场文书
师说教学反思
2014/02/07 职场文书
商场促销活动方案
2014/02/08 职场文书
责任胜于能力演讲稿
2014/05/20 职场文书
节约每一滴水演讲稿
2014/09/09 职场文书
房产协议书范本
2014/10/18 职场文书
2014年结对帮扶工作总结
2014/12/17 职场文书
我在伊朗长大观后感
2015/06/16 职场文书
心得体会该怎么写呢?
2019/06/27 职场文书