tf.transpose函数的用法讲解

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 (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‘ 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的第二个参数perm=[0,1,2],0代表三维数组的高(即为二维数组的个数),1代表二维数组的行,2代表二维数组的列。
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=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]]]
---------------

最后,总结下:

[0, 1, 2]是正常显示,那么交换哪两个数字,就是把对应的输入张量的对应的维度对应交换即可。
---------------------
作者:cc19
来源:CSDN
原文:https://blog.csdn.net/cc1949/article/details/78422704
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/sddai/p/10287977.html

时间: 2024-08-29 11:46:50

tf.transpose函数的用法讲解的相关文章

详解php 获取文件名basename()函数的用法

PHP 中basename()函数给出一个包含有指向一个文件的全路径的字符串,此函数返回基本的文件名,本篇文章收集了关于使用PHP basename()函数获取文件名的几篇文章,希望对大家理解使用PHP basename()函数获取文件名有所帮助. 1.详解php basename()函数获取文件名的用法 php basename()函数给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名.如果文件名是以 suffix 结束的,那这一部分也会被去掉.在 Windows 中,斜线(/

SQL2005中row_number()等函数的用法

SQL2005中row_number()等函数的用法 今天刚装了SQL Server 2005,感觉还行,2005比2000新增了几个函数,分别是row_number().rank().dense_rank().ntile(),下面以实例分别简单讲解一下. create table gg(sname varchar(10),sort varchar(10),num int) go insert into gg select '白芍','根茎类',55 union all select '法半夏'

python中open函数的用法

用法如下: name = open('errname.txt','w')name.readline()name.close() 1.看下第一行的代码 用来访问磁盘中存放的文件,可以进行读写等操作,例如上例中 'w',这里便是对errname.txt这个文件进行读操作 例如: w:以写方式打开 a:以追加方式打开 r+:以读写模式打开 w+:以读写模式打开 rb:以二进制读模式打开 wb:以二进制写模式打开 ab:以二进制追加模式打开 rb+:以二进制读写模式打开 wb+:以二进制读写模式打开 a

【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法

在计算loss的时候,最常见的一句话就是 tf.nn.softmax_cross_entropy_with_logits ,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化的值 tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None) 除去name参数用以指定该操作的name,与方法有关的一共两个参数: 第一个参数logits:就是神经网络最后一层的输出,如果有batch的话,它的大小就是[b

PHP截取字符串函数substr()函数实例用法详解

在PHP中有一项非常重要的技术,就是截取指定字符串中指定长度的字符.PHP对于字符串截取可以使用PHP预定义函数substr()函数来实现.下面就来介绍一下substr()函数的语法及其应用. substr()函数语法格式如下:大理石平台厂家 1 substr(string, start, length); substr()函数参数说明如下: 参 数 说 明 string 指定字符串对象 start 指定开始截取字符串的位置.如果参数start为负数,则从字符串的末尾开始截取 length 可选

云计算学习路线图素材、课件,mysql中函数的用法

在本篇文章中将给大家讲解下mysql中函数的用法: 今天在处理工单的时候,其中的一个需求是某商品的发货时效(即下单时间和发货时间的时间差),接触到了TIMESTAMPDIFF函数 TIMESTAMPDIFF TIMERSTAMPDIFF语法: TIMERSTAMPDIFF(interval,datetime_expr1,datetime_expr2) 说明: 该函数是返回datetime_expr1和datetime_expr2之间的整数差,其中单位有interval参数决定,interval的

云计算学习路线图素材、课件,CASE的其他用法讲解

在本篇文章中将给大家讲解下CASE的其他用法讲解: 使用带有简单CASE函数和CASE搜索函数的select语句在select语句中,CASE搜索函数允许根据比较值 select CASE WHEN good_type<2 THEN '<2' WHEN good_type>=2 AND good_type<3 THEN '>=2 && <3' ELSE '>=3' END AS good_now_type, good_type,user_id,us

tf.slice()函数详解(极详细)

目录 1.官方注释 2.参数解释 3.例子 参考 tf.slice()是TensorFlow库中分割张量的一个函数,其定义为def slice(input_, begin, size, name=None):.tf.slice()函数的那些参数设置实在是不好理解,查了好多资料才理解,所以这边记录一下. 1.官方注释 官方的注释如下: """Extracts a slice from a tensor. This operation extracts a slice of si

mysql中计算两个日期的时间差函数TIMESTAMPDIFF用法

mysql中计算两个日期的时间差函数TIMESTAMPDIFF用法: 语法: TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2) 说明: 返回日期或日期时间表达式datetime_expr1 和datetime_expr2the 之间的整数差.其结果的单位由interval 参数给出.interval 的法定值同TIMESTAMPADD()函数说明中所列出的相同. mysql> SELECT TIMESTAMPDIFF(MONTH,'200