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中的装饰器详解
Apr 13 Python
Python pickle模块用法实例
Apr 14 Python
简单介绍Python中的try和finally和with方法
May 05 Python
深入解析Python中的urllib2模块
Nov 13 Python
总结网络IO模型与select模型的Python实例讲解
Jun 27 Python
Python编程中对super函数的正确理解和用法解析
Jul 02 Python
python中defaultdict的用法详解
Jun 07 Python
Python实现求两个csv文件交集的方法
Sep 06 Python
DataFrame中的object转换成float的方法
Apr 10 Python
python十进制和二进制的转换方法(含浮点数)
Jul 07 Python
PyCharm搭建Spark开发环境实现第一个pyspark程序
Jun 13 Python
jupyter notebook 增加kernel教程
Apr 10 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保存任意网络图片到服务器的方法
2015/04/14 PHP
关于PHP开发的9条建议
2015/07/27 PHP
thinkphp在php7环境下提示Cannot use ‘String’ as class name as it is reserved的解决方法
2016/09/30 PHP
php定期拉取数据对比方法实例
2019/09/22 PHP
打豆豆小游戏 用javascript编写的[打豆豆]小游戏
2013/01/08 Javascript
js实现同一个页面多个渐变效果的方法
2015/04/10 Javascript
JavaScript实现多个重叠层点击切换效果的方法
2015/04/24 Javascript
javascript实现拖动元素交换位置
2015/11/29 Javascript
AngularJs上传前预览图片的实例代码
2017/01/20 Javascript
nodejs搭建本地服务器并访问文件的方法
2017/03/03 NodeJs
vue-resource拦截器设置头信息的实例
2017/10/27 Javascript
AngularJS select加载数据选中默认值的方法
2018/02/28 Javascript
JavaScript变量基本使用方法实例分析
2019/11/15 Javascript
Websocket 向指定用户发消息的方法
2020/01/09 Javascript
[02:11]2014DOTA2 TI专访VG战队Fenrir:队伍气氛良好
2014/07/11 DOTA
[01:28]一分钟告诉你DOTA2 TI9不朽宝藏Ⅱ中有什么!
2019/07/09 DOTA
python中随机函数random用法实例
2015/04/30 Python
在Django的模板中使用认证数据的方法
2015/07/23 Python
python 连接各类主流数据库的实例代码
2018/01/30 Python
python itchat实现调用微信接口的第三方模块方法
2019/06/11 Python
Python使用LDAP做用户认证的方法
2019/06/20 Python
python GUI库图形界面开发之PyQt5树形结构控件QTreeWidget详细使用方法与实例
2020/03/02 Python
python代码实现猜拳小游戏
2020/11/30 Python
python中time.ctime()实例用法
2021/02/03 Python
马来西亚网上购物:Youbeli
2018/03/30 全球购物
Java和Javasciprt的区别
2012/09/02 面试题
C#软件工程师英语面试题
2015/06/07 面试题
求职信的要素有哪些呢
2013/12/26 职场文书
十八届三中全会个人学习材料
2014/02/13 职场文书
业务员的岗位职责
2014/03/15 职场文书
2014年班组工作总结
2014/11/20 职场文书
乡镇党建工作总结2015
2015/05/19 职场文书
七年级之家长会发言稿范文
2019/09/04 职场文书
HTML+CSS+JS实现图片的瀑布流布局的示例代码
2021/04/22 HTML / CSS
JavaScript如何利用Promise控制并发请求个数
2021/05/14 Javascript
MySQL中正则表达式(REGEXP)使用详解
2022/07/07 MySQL