tf.transpose函数的用法讲解

阅读: 评论:0

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 (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(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&#stant(x)
a&#anspose(x, [0, 1, 2])
b&#anspose(x, [0, 2, 1])
c&#anspose(x, [1, 0, 2])
d&#anspose(x, [1, 2, 0])
e&#anspose(x, [2, 1, 0])
f&#anspose(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
原文:
版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:.html

本文发布于:2024-02-04 14:49:58,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170709714656520.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:函数   tf   transpose
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23