[579]tf.transpose函数

tf.transpose(input, [dimension_1, dimenaion_2,…,dimension_n]):这个函数主要适用于交换输入张量的不同维度用的,如果输入张量是二维,就相当是转置。dimension_n是整数,如果张量是三维,就是用0,1,2来表示。这个列表里的每个数对应相应的维度。如果是[2,1,0],就把输入张量的第三维度和第一维度交换。

重点:

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

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

函数参数:

  • a:一个 Tensor.
  • perm:a 的维数的排列.
  • name:操作的名称(可选).
  • conjugate:可选 bool,将其设置为 True 在数学上等同于 tf.conj(tf.transpose(input)).

返回:

  • tf.transpose 函数返回一个转置 Tensor.

置换 a,根据 perm 重新排列尺寸.
返回的张量的维度 i 将对应于输入维度 perm[i].如果 perm 没有给出,它被设置为(n-1 … 0),其中 n 是输入张量的秩.因此,默认情况下,此操作在二维输入张量上执行常规矩阵转置.如果共轭为 True,并且 a.dtype 是 complex64 或 complex128,那么 a 的值是共轭转置和.

x = tf.constant([[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]]

# If x is complex, setting conjugate=True gives the conjugate transpose
x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j],
                 [4 + 4j, 5 + 5j, 6 + 6j]])
tf.transpose(x, conjugate=True)  # [[1 - 1j, 4 - 4j],
                                 #  [2 - 2j, 5 - 5j],
                                 #  [3 - 3j, 6 - 6j]]

# '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
# (this common operation has a shorthand `matrix_transpose`)
tf.transpose(x, perm=[0, 2, 1])  # [[[1,  4],
                                 #   [2,  5],
                                 #   [3,  6]],
                                 #  [[7, 10],
                                 #   [8, 11],
                                 #   [9, 12]]]

test

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]]]
---------------

参考:https://www.w3cschool.cn/tensorflow_python/tensorflow_python-z61d2no5.html
https://blog.csdn.net/uestc_c2_403/article/details/73350498
https://blog.csdn.net/cc1949/article/details/78422704

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周小董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值