tf.transpose用法

tf.transpose:意为转置

转置`a`。 根据`perm`排列尺寸。
返回的张量的维度i将对应于输入维`perm [i]`。 如果未给出`perm`,则将其设置为(n-1 ... 0),其中n是输入张量的秩。 因此,默认情况下,此操作会在2D输入张量上执行常规矩阵转置。 如果共轭是True,而a.dtype是complex64或complex128,则将a的值进行共轭和转置。

transpose(a, perm=None, name="transpose", conjugate=False):
  """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. If conjugate is True and
  `a.dtype` is either `complex64` or `complex128` then the values of `a`
  are conjugated and transposed.

  @compatibility(numpy)
  In `numpy` transposes are memory-efficient constant time operations as they
  simply return a new view of the same data with adjusted `strides`.

  TensorFlow does not support strides, so `transpose` returns a new tensor with
  the items permuted.
  @end_compatibility

  For example:

  ```python
  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 `linalg.transpose`)
  tf.transpose(x, 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).
    conjugate: Optional bool. Setting it to `True` is mathematically equivalent
      to tf.conj(tf.transpose(input)).

  Returns:
    A transposed `Tensor`.

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

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

重点:

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

import tensorflow as tf

import numpy as np

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

with tf.Session() as sess:
    print('---------------')
    print(sess.run(a)) # [0, 1, 2]
    # [[[1  2  3  4]
    #   [5  6  7  8]
    #  [9 10 11 12]]
    #
    # [[21 22 23 24]
    #  [25 26 27 28]
    #  [29 30 31 32]]]
    print('---------------')
    print(sess.run(b)) # [0, 2, 1]
    # [[[1  5  9]
    #   [2  6 10]
    #   [3  7 11]
    #   [4  8 12]]
    #
    #  [[21 25 29]
    #   [22 26 30]
    #   [23 27 31]
    #   [24 28 32]]]
    print('---------------')
    print(sess.run(c)) # [1, 0, 2]
    # [[[1  2  3  4]
    #   [21 22 23 24]]
    #
    #  [[5  6  7  8]
    #   [25 26 27 28]]
    #
    #  [[9 10 11 12]
    #   [29 30 31 32]]]
    print('---------------')
    print(sess.run(d)) # [1, 2, 0]
    # [[[1 21]
    #   [2 22]
    #   [3 23]
    #   [4 24]]
    #
    #  [[5 25]
    #   [6 26]
    #   [7 27]
    #   [8 28]]
    #
    #  [[9 29]
    #   [10 30]
    #   [11 31]
    #   [12 32]]]
    print('---------------')
    print(sess.run(e)) # [2, 1, 0]
    # [[[1 21]
    #   [5 25]
    #   [9 29]]
    #
    #  [[2 22]
    #   [6 26]
    #   [10 30]]
    #
    #  [[3 23]
    #   [7 27]
    #   [11 31]]
    #
    #  [[4 24]
    #   [8 28]
    #   [12 32]]]

    print('---------------')
    print(sess.run(f)) # [2, 0, 1]
    # [[[1  5  9]
    #   [21 25 29]]
    #
    #  [[2  6 10]
    #   [22 26 30]]
    #
    #  [[3  7 11]
    #   [23 27 31]]
    #
    #  [[4  8 12]
    #   [24 28 32]]]
    print('---------------')

总结:[0, 1, 2]是正常显示,那么交换哪两个数字,就是把对应的输入张量的对应的维度对应交换即可。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值