前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tf.transpose函数

tf.transpose函数

作者头像
周小董
发布2019-03-25 10:03:34
1.7K0
发布2019-03-25 10:03:34
举报

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年03月20日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 重点:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档