tf.transpose函数中文意思是转置,对于低维度的转置问题,很简单,不想讨论,直接转置就好(大家看下面文档,一看就懂)。

tf.transpose(a, perm=None, name='transpose') 

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' is [[1 2 3]
# [4 5 6]]
tf.transpose(x) ==> [[1 4]
[2 5]
[3 6]] # Equivalently
tf.transpose(x perm=[1, 0]) ==> [[1 4]
[2 5]
[3 6]] # 'perm' is more useful for n-dimensional tensors, for n > 2
# 'x' is [[[1 2 3]
# [4 5 6]]
# [[7 8 9]
# [10 11 12]]]
# Take the transpose of the matrices in dimension-0
tf.transpose(b, perm=[0, 2, 1]) ==> [[[1 4]
[2 5]
[3 6]] [[7 10]
[8 11]
[9 12]]] Args:
•a: A Tensor.
•perm: A permutation of the dimensions of a.
•name: A name for the operation (optional). Returns: A transposed Tensor.

  

本文主要讨论高维度的情况:

为了形象理解高维情况,这里以矩阵组合举例:

先定义下: 2 x (3*4)表示2个3*4的矩阵,(其实,它是个3维张量)。

x = [[[1,2,3,4],[5,6,7,8],[9,10,11,12]],[[21,22,23,24],[25,26,27,28],[29,30,31,32]]]

输出:

---------------
[[[ 1  2  3  4]
  [ 5  6  7  8]
  [ 9 10 11 12]]

[[21 22 23 24]
  [25 26 27 28]
  [29 30 31 32]]]
---------------

重点来了:

tf.transpose的第二个参数perm=[0,1,2],0代表三维数组的高(即为二维数组的个数),1代表二维数组的行,2代表二维数组的列。
tf.transpose(x, perm=[1,0,2])代表将三位数组的高和行进行转置。

我们写个测试程序如下:

import tensorflow as tf

#x = tf.constant([[1, 2 ,3],[4, 5, 6]])
x = [[[1,2,3,4],[5,6,7,8],[9,10,11,12]],[[21,22,23,24],[25,26,27,28],[29,30,31,32]]]
#a=tf.constant(x)
a=tf.transpose(x, [0, 1, 2])
b=tf.transpose(x, [0, 2, 1])
c=tf.transpose(x, [1, 0, 2])
d=tf.transpose(x, [1, 2, 0])
e=tf.transpose(x, [2, 1, 0])
f=tf.transpose(x, [2, 0, 1]) # 'perm' is more useful for n-dimensional tensors, for n > 2
# 'x' is [[[1 2 3]
# [4 5 6]]
# [[7 8 9]
# [10 11 12]]]
# Take the transpose of the matrices in dimension-0
#tf.transpose(b, perm=[0, 2, 1])
with tf.Session() as sess:
print ('---------------')
print (sess.run(a))
print ('---------------')
print (sess.run(b))
print ('---------------')
print (sess.run(c))
print ('---------------')
print (sess.run(d))
print ('---------------')
print (sess.run(e))
print ('---------------')
print (sess.run(f))
print ('---------------')

  

我们期待的结果是得到如下矩阵:

a: 2 x 3*4

b: 2 x 4*3

c: 3 x 2*4

d: 3 x 4*2

e: 4 x 3*2

f: 4 x 2*3

运行脚本,结果一致,如下:

---------------
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]

[[21 22 23 24]
[25 26 27 28]
[29 30 31 32]]]
---------------
[[[ 1 5 9]
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]]

[[21 25 29]
[22 26 30]
[23 27 31]
[24 28 32]]]
---------------
[[[ 1 2 3 4]
[21 22 23 24]]

[[ 5 6 7 8]
[25 26 27 28]]

[[ 9 10 11 12]
[29 30 31 32]]]
---------------
[[[ 1 21]
[ 2 22]
[ 3 23]
[ 4 24]]

[[ 5 25]
[ 6 26]
[ 7 27]
[ 8 28]]

[[ 9 29]
[10 30]
[11 31]
[12 32]]]
---------------
[[[ 1 21]
[ 5 25]
[ 9 29]]

[[ 2 22]
[ 6 26]
[10 30]]

[[ 3 23]
[ 7 27]
[11 31]]

[[ 4 24]
[ 8 28]
[12 32]]]
---------------
[[[ 1 5 9]
[21 25 29]]

[[ 2 6 10]
[22 26 30]]

[[ 3 7 11]
[23 27 31]]

[[ 4 8 12]
[24 28 32]]]
---------------

最后,总结下:

[0, 1, 2]是正常显示,那么交换哪两个数字,就是把对应的输入张量的对应的维度对应交换即可。
---------------------
作者:cc19
来源:CSDN
原文:https://blog.csdn.net/cc1949/article/details/78422704
版权声明:本文为博主原创文章,转载请附上博文链接!

tf.transpose函数的用法讲解的更多相关文章

  1. tf.transpose函数解析

    tf.transpose函数解析 觉得有用的话,欢迎一起讨论相互学习~Follow Me tf.transpose(a, perm = None, name = 'transpose') 解释 将a进 ...

  2. 【转载】 tf.split函数的用法

    原文地址: https://blog.csdn.net/uestc_c2_403/article/details/73350457 由于tensorflow 版本更新问题   用法略有修改 ----- ...

  3. C#累加器函数Aggregate用法 讲解

    Enumerable.Aggregate 扩展方法在System.Linq命名空间中,是Enumerable类的第一个方法(按字母顺序排名),但确是Enumerable里面相对复杂的方法. MSDN对 ...

  4. tensorflow 的tf.split函数的用法

    将张量进行切分 tf.split( value, num_or_size_splits, axis=0, num=None, name='split' ) value: 待切分的张量 num_or_s ...

  5. python中map()函数的用法讲解

    map函数的原型是map(function, iterable, -),它的返回结果是一个列表. 参数function传的是一个函数名,可以是python内置的,也可以是自定义的. 参数iterabl ...

  6. tf.split函数的用法(tensorflow1.13.0)

    tf.split(input, num_split, dimension): dimension指输入张量的哪一个维度,如果是0就表示对第0维度进行切割:num_split就是切割的数量,如果是2就表 ...

  7. tf.transpose()的用法

    一.tensorflow官方文档内容 transpose( a, perm=None, name='transpose' ) Defined in tensorflow/python/ops/arra ...

  8. python numpy的transpose函数用法

    #MXNET的N*C*H*W在numpy打印时比较直观#mxnet卷积层# 输入数据格式是:batch * inchannel * height * width# 输出数据格式是:batch * ou ...

  9. MySQL中 IFNULL、NULLIF和ISNULL函数的用法

    mysql 中 ifnull().nullif().isnull()函数的用法讲解: 一.IFNULL(expr1,expr2)用法: 假如expr1不为NULL,则 IFNULL() 的返回值为ex ...

随机推荐

  1. jquery.validate.js自定义表单验证

    $(document).ready(function() { //在下列位置输入页面加载的逻辑代码 $("#inputForm").validate({ rules: { seq: ...

  2. 数据库简单练习 建表+select

    create table student ( sno int primary key, sname char(20),  sex char(2), birthday datetime, class i ...

  3. rabbitmq学习(四) —— 发布订阅

    为了说明这种模式,我们将建立一个简单的日志系统.这个系统将由两个程序组成,第一个将发出日志消息,第二个将接收并处理日志消息.在我们的日志系统中,每一个运行的接收程序的副本都会收到日志消息. 交换器(E ...

  4. 修改无线wifi网络名称。注册表。windows 无线属性 windows 无线 配置文件

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha windows 无线属性 windows 无线 配置文件 ======= 修改完成,之后 ...

  5. luoguP3175 [HAOI2015]按位或 min-max容斥 + 高维前缀和

    考虑min-max容斥 \(E[max(S)] = \sum \limits_{T \subset S} min(T)\) \(min(T)\)是可以被表示出来 即所有与\(T\)有交集的数的概率的和 ...

  6. HDU.5730.Shell Necklace(分治FFT)

    题目链接 \(Description\) 有\(n\)个长度分别为\(1,2,\ldots,n\)的珠子串,每个有\(a_i\)种,每种个数不限.求有多少种方法组成长度为\(n\)的串.答案对\(31 ...

  7. [HDU5713]K个联通块

    [HDU5713]K个联通块 题目大意: 有一张\(n(n\le14)\)个点,\(m\)条边无重边的无向图,求有多少个边集,使得删掉边集里的边后,图里恰好有\(k\)个连通块. 思路: 一个显然的动 ...

  8. IEEEXtreme Practice Community Xtreme9.0 - Digit Fun!

    Xtreme9.0 - Digit Fun! 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/di ...

  9. inline关键字的作用

    一.在C&C++中,inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 如下面一宏定义表达式: #define express(v1,v2) (v1 ...

  10. nginx+uwsgi+flask 服务器配置

    注:每个机器,软件版本可能不一样,虽然网上有很多类似的帖子,但是我在搭建的时候遇到了不少的坑,此文仅供参考. 请求流程: 1.安装uwsgi uwsgi是一个应用服务器,非静态文件的网络请求就必须通过 ...