tf 矩阵行和列交换_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
版权声明:本文为博主原创文章,转载请附上博文链接!
tf 矩阵行和列交换_tf.transpose函数的用法讲解相关推荐
- transpose公式_tf.transpose函数的用法讲解(图解)
tf.transpose函数中文意思是转置,对于低维度的转置问题,低维的还可以理解高维有点懵 看了博客也不是很明白 tf.transpose函数 tf.transpose( a, perm=None, ...
- tf 矩阵行和列交换_TF-搞不懂的TF矩阵加法
看谷歌的demo mnist,卷积后加偏执量的代码 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) h_pool1 = max_poo ...
- python列表split_Python-split()函数实例用法讲解
在Python中,split() 方法可以实现将一个字符串按照指定的分隔符切分成多个子串,这些子串会被保存到列表中(不包含分隔符),作为方法的返回值反馈回来. split函数用法 split(sep= ...
- python里map函数_python中map()函数的用法讲解
原博文 2018-10-26 12:59 − map函数的原型是map(function, iterable, -),它的返回结果是一个列表. 参数function传的是一个函数名,可以是python ...
- php crypt加密 盐值,PHP crypt()函数的用法讲解
PHP crypt() 函数 定义和用法 crypt() 函数返回使用 DES.Blowfish 或 MD5 算法加密的字符串. 在不同的操作系统上,该函数的行为不同,某些操作系统支持一种以上的算法类 ...
- Python输出矩阵的维度(行和列数)
目录 使用numpy中的shape()函数输出矩阵的行和列 一.shape()函数获取矩阵的行数和列数 二.len()函数获取矩阵的行数 三.使用x.ndim函数可以输出矩阵维数 使用numpy中的s ...
- 详解numpy中transpose()函数
今天在网上搜寻了许多博客,始终没有真正理解numpy中的transpose()函数, transpose 的原理其实是根据维度(shape)索引决定的,举个栗子: x = np.arange(4).r ...
- pandas使用transpose函数对dataframe进行转置、将dataframe的行和列进行互换(flip the rows and columns in dataframe)
pandas使用transpose函数对dataframe进行转置.将dataframe的行和列进行互换(flip the rows and columns in dataframe) 目录
- tf.transpose()函数(转)
tensorflow里面许多针对数组操作的函数,官方文档又看了没啥卵用,网上帖子直接copy官方文档而不解释,只能自己写个程序测试理解,以3个维度的tensor进行理解 tf.transpose()作 ...
最新文章
- 2022-2028年中国文化产业园投资分析及前景预测报告(全卷)
- 美国科技三巨头的财报为何集体爆表?原因在这里
- 【数学建模】种群竞争模型(最优化)
- java分隔符的引号,使用分隔符拆分带引号的字符串
- python打印菱形星号代码_Python打印“菱形”星号代码
- oracle数据库安装跳坑
- vue css load,vue css3loadding插件的开发以及npm包的发布管理
- 复制远程服务器的文件 报错 scp: not a regular file
- %3cphp和%3c php_phpcmsv9后台登录绕过
- 5-5图层的链接-新版本不常用
- 实录 | 旷视研究院详解COCO2017人体姿态估计冠军论文(PPT+视频)
- 解决 sql server 2005 2000 导出 script 脚本 附近有语法错误
- 木材材积表快速计算器_原木材积计算器
- MeGUI中文版x64版本使用说明
- 关于c#实现影音嗅探的问题(转)
- 上海宝付谈谈故宫瘫痪,程序员怎么办
- 浅谈Spring事件监听
- 金蝶迷你版凭证导入工具_金蝶kis迷你版如何插入凭证?
- PageAdmin CMS Sql新建数据库和用户名教程
- Java消息队列与JMS的诞生
热门文章
- python读txt写入excel_Python读取txt内容写入xls格式excel中的方法
- python如何快速导入未安装模块_如何导入安装在sitepackages中的模块
- C#面试题(.net开发人员必备)
- android 使用注解
- Jsp+SSH+Mysql实现的校园课程作业网
- mv强制覆盖 shell_生产力工具:shell 与 Bash 脚本
- 《小团团团队》第四次作业:项目需求调研与分析
- Cacti监控mysql数据库server实现过程
- 09_MySQL DQL_SQL99标准中的多表查询(外连接)
- Hibernate学习(1):查询demo