Tensorflow:转置函数 transpose的使用详解

作者:abclhq2005 时间:2021-01-17 20:36:37 

我就废话不多说,咱直接看代码吧!

tf.transpose


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

Defined in tensorflow/python/ops/array_ops.py.

See the guides: Math > Matrix Math Functions, Tensor Transformations > Slicing and Joining

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 = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.transpose(x) # [[1, 4]
        # [2, 5]
        # [3, 6]]


tf.transpose(x, perm=[1, 0]) # [[1, 4]
              # [2, 5]
              # [3, 6]]

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

a的转置是根据 perm 的设定值来进行的。

返回数组的 dimension(尺寸、维度) i与输入的 perm[i]的维度相一致。如果未给定perm,默认设置为 (n-1…0),这里的 n 值是输入变量的 rank 。因此默认情况下,这个操作执行了一个正规(regular)的2维矩形的转置

例如:


x = [[1 2 3]
  [4 5 6]]

tf.transpose(x) ==> [[1 4]
          [2 5]
          [3 6]]

tf.transpose(x) 等价于:
tf.transpose(x perm=[1, 0]) ==> [[1 4]
                [2 5]
                [3 6]]

a=tf.constant([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
array([[[ 1, 2, 3],
   [ 4, 5, 6]],

[[ 7, 8, 9],
   [10, 11, 12]]])

x=tf.transpose(a,[1,0,2])
array([[[ 1, 2, 3],
   [ 7, 8, 9]],

[[ 4, 5, 6],
   [10, 11, 12]]])

x=tf.transpose(a,[0,2,1])
array([[[ 1, 4],
   [ 2, 5],
   [ 3, 6]],

[[ 7, 10],
   [ 8, 11],
   [ 9, 12]]])

x=tf.transpose(a,[2,1,0])
array([[[ 1, 7],
   [ 4, 10]],

[[ 2, 8],
   [ 5, 11]],

[[ 3, 9],
   [ 6, 12]]])

array([[[ 1, 7],
   [ 4, 10]],

[[ 2, 8],
   [ 5, 11]],

[[ 3, 9],
   [ 6, 12]]])

x=tf.transpose(a,[1,2,0])
array([[[ 1, 7],
   [ 2, 8],
   [ 3, 9]],

[[ 4, 10],
   [ 5, 11],
   [ 6, 12]]])

来源:https://blog.csdn.net/abclhq2005/article/details/78677044

标签:Tensorflow,转置,transpose
0
投稿

猜你喜欢

  • 解读tf.keras.layers模块中的函数

    2023-04-02 04:26:29
  • Python使用 TCP协议实现智能聊天机器人功能

    2022-08-19 03:49:48
  • asp简单的仿图片验证码

    2008-03-12 11:54:00
  • Python命令启动Web服务器实例详解

    2022-10-09 11:53:42
  • python中的decimal类型转换实例详解

    2022-05-01 15:05:21
  • 两个JS之间的函数互相调用方式

    2024-04-10 10:39:45
  • Mysql数据库百万级数据测试索引效果

    2024-01-24 01:30:03
  • python多线程方法详解

    2023-10-16 02:46:31
  • python下PyGame的下载与安装过程及遇到问题

    2021-09-24 00:12:15
  • python 获取等间隔的数组实例

    2023-05-21 15:07:16
  • python django入门

    2022-05-10 02:46:19
  • sql查询表中根据某列排序的任意行语句

    2024-01-13 02:13:18
  • Python实现数值积分方式

    2022-01-23 14:00:37
  • Mac OS下PHP环境搭建及PHP操作MySQL常用方法小结

    2024-05-08 10:16:31
  • pandas to_excel 添加颜色操作

    2021-07-19 19:49:57
  • 2019最新的Pycharm激活码(推荐)

    2023-11-17 00:49:37
  • 详细说明关于Java的数据库连接(JDBC)

    2024-01-18 09:00:16
  • Python的装饰器用法学习笔记

    2021-05-06 03:03:36
  • SQL进行排序、分组、统计的10个新技巧分享

    2024-01-17 22:44:12
  • 从浅入深带你掌握Golang数据结构map

    2023-06-21 16:42:43
  • asp之家 网络编程 m.aspxhome.com