-
permute函数
2020-05-21 15:09:41permute函数:可以同时多次交换tensor的维度 示例:b = a.permute(0,2 ,1) 将a的维度索引1和维度索引2交换位置展开全文 -
permute函数_Pytorch之permute函数
2020-11-25 09:15:471 先看看官方中英文doc: torch.Tensor.permute (Python method, in torch.Tensor)1.1 permute(dims)将tensor的维度换位。参数: - __dims__ (int ..*) - 换位顺序例:>>> x = torch.randn(2, 3, 5) >&...1 先看看官方中英文doc:
torch.Tensor.permute (Python method, in torch.Tensor)
1.1 permute(dims)
将tensor的维度换位。
参数: - __dims__ (int ..*) - 换位顺序
例:
>>> x = torch.randn(2, 3, 5) >>> x.size() torch.Size([2, 3, 5]) >>> x.permute(2, 0, 1).size() torch.Size([5, 2, 3])
1.2 permute(*dims) → Tensor
Permute the dimensions of this tensor.
Parameters: *dims (int...) – The desired ordering of dimensions
Example:
>>> x = torch.randn(2, 3, 5) >>> x.size() torch.Size([2, 3, 5]) >>> x.permute(2, 0, 1).size() torch.Size([5, 2, 3])
2 pytorch permute的使用
permute函数功能还是比较简单的,下面主要介绍几个细节点:
2.1 transpose与permute的异同
Tensor.permute(a,b,c,d, ...):permute函数可以对任意高维矩阵进行转置,但没有 torch.permute() 这个调用方式, 只能 Tensor.permute():
>>> torch.randn(2,3,4,5).permute(3,2,0,1).shape torch.Size([5, 4, 2, 3])
torch.transpose(Tensor, a,b):transpose只能操作2D矩阵的转置,有两种调用方式;
另:连续使用transpose也可实现permute的效果:
>>> torch.randn(2,3,4,5).transpose(3,0).transpose(2,1).transpose(3,2).shape torch.Size([5, 4, 2, 3]) >>> torch.randn(2,3,4,5).transpose(1,0).transpose(2,1).transpose(3,1).shape torch.Size([3, 5, 2, 4])
从以上操作中可知,permute相当于可以同时操作于tensor的若干维度,transpose只能同时作用于tensor的两个维度;
2.2 permute函数与contiguous、view函数之关联
contiguous:view只能作用在contiguous的variable上,如果在view之前调用了transpose、permute等,就需要调用contiguous()来返回一个contiguous copy;
一种可能的解释是:有些tensor并不是占用一整块内存,而是由不同的数据块组成,而tensor的view()操作依赖于内存是整块的,这时只需要执行contiguous()这个函数,把tensor变成在内存中连续分布的形式;
判断ternsor是否为contiguous,可以调用torch.Tensor.is_contiguous()函数:
import torch x = torch.ones(10, 10) x.is_contiguous() # True x.transpose(0, 1).is_contiguous() # False x.transpose(0, 1).contiguous().is_contiguous() # True
另:在pytorch的最新版本0.4版本中,增加了torch.reshape(),与 numpy.reshape() 的功能类似,大致相当于 tensor.contiguous().view(),这样就省去了对tensor做view()变换前,调用contiguous()的麻烦;
3 permute与view函数功能demo
import torch import numpy as np a=np.array([[[1,2,3],[4,5,6]]]) unpermuted=torch.tensor(a) print(unpermuted.size()) # ——> torch.Size([1, 2, 3]) permuted=unpermuted.permute(2,0,1) print(permuted.size()) # ——> torch.Size([3, 1, 2]) view_test = unpermuted.view(1,3,2) print(view_test.size()) # ——> torch.Size([1, 3, 2])
利用函数 permute(2,0,1) 可以把 Tensor([[[1,2,3],[4,5,6]]]) 转换成:
tensor([[[ 1, 4]], [[ 2, 5]], [[ 3, 6]]]) # print(permuted)
如果使用view(1,3,2) 可以得到:
tensor([[[ 1, 2], [ 3, 4], [ 5, 6]]]) # print(view_test)
5 参考
https://zhuanlan.zhihu.com/p/64376950
https://pytorch.org/docs/stable/tensors.html?highlight=permute#torch.Tensor.permute
https://pytorch-cn.readthedocs.io/zh/latest/package_references/Tensor/#permutedims
-
permute函数(Pytorch)
2021-02-17 09:57:08permute函数的作用是对tensor进行转置。 import torch import torch.nn as nn x = torch.randn(1, 2, 3, 4) print(x.size()) print(x.permute(2, 1, 0, 3).size()) 随机生成一个1X2X3X4的四维向量,permute函数...- permute函数的作用是对tensor进行转置。
import torch import torch.nn as nn x = torch.randn(1, 2, 3, 4) print(x.size()) print(x.permute(2, 1, 0, 3).size())
随机生成一个1X2X3X4的四维向量,permute函数的参数表示的是转置后的向量位置。比如原向量中(1, 2, 3, 4),1的下标是0,2的下标是1,3的下标是2,4的下标是3;在x.permute(2, 1, 0, 3)中,2代表原来下表为2的数字3放在第一位(也就是N),1代表原来下表为1的数字2放在第二位(也就是C),0代表原来下表为0的数字1放在第三位(也就是H),3代表原来下表为3的数字4放在第四位(也就是W)。这样产生的结果如下:
torch.Size([1, 2, 3, 4]) #原来的tensor torch.Size([3, 2, 1, 4]) #转置后的tensor
- **torch.transpose()只能操作二维转置。**这个意思不是torch.transpose()只能作用于二维向量,它的意思是一次只能进行两个维度的转置,如果需要多个维度的转置,那么需要多次调用transpose()。比如上述的tensor[1,2,3,4]转置为tensor[3,4,1,2],使用transpose需要做如下
x.transpose(0,2).transpose(1,3)
- view()函数作用的内存必须是连续的,如果操作数不是连续存储的,必须在操作之前执行contiguous(),把tensor变成在内存中连续分布的形式;
- 我们来直观看一下tensor转置的实际效果。
import torch import torch.nn as nn import numpy as np y = np.array([[[1, 2, 3], [4, 5, 6]]]) # 1X2X3 y_tensor = torch.tensor(y) y_tensor_trans = y_tensor.permute(2, 0, 1) # 3X1X2 print(y_tensor.size()) print(y_tensor_trans.size()) print(y_tensor) print(y_tensor_trans) print(y_tensor.view(1, 3, 2))
torch.Size([1, 2, 3]) torch.Size([3, 1, 2]) tensor([[[1, 2, 3], [4, 5, 6]]]) tensor([[[1, 4]], [[2, 5]], [[3, 6]]]) tensor([[[1, 2], [3, 4], [5, 6]]])
- view()函数是连续操作内存的,他会将1、2、3、4、5、6连续分配,上述是将1X2X3变成1X3X2。如果变成1X2X3,结果如下:
tensor([[[1, 2, 3], [4, 5, 6]]])
-
pytorch transpose与permute函数
2020-12-20 15:20:17pytorch transpose与permute函数 pemute可以对高维,2阶或以上矩阵进行重排列,或者说转置,而transpose只能对两个维度进行调换 b = torch.tensor([[[1,4],[2,5]],[[3,7],[4,6]]]) b.shape Out[36]: torch.Size([2, 2...pytorch transpose与permute函数
pemute可以对高维,2阶或以上矩阵进行重排列,或者说转置,而transpose只能对两个维度进行调换
b = torch.tensor([[[1,4],[2,5]],[[3,7],[4,6]]]) b.shape Out[36]: torch.Size([2, 2, 2]) b Out[37]: tensor([[[1, 4], [2, 5]], [[3, 7], [4, 6]]]) b.permute(0,2,1) Out[38]: tensor([[[1, 2], [4, 5]], [[3, 4], [7, 6]]]) b.transpose(2,1) Out[40]: tensor([[[1, 2], [4, 5]], [[3, 4], [7, 6]]])
-
Pytorch中的permute函数和transpose,contiguous,view函数的关联
2020-03-16 17:25:33在进行深度学习的过程中,经常遇到permute函数,transpose函数,view函数,contiguous函数等,他们起什么作用,之间又有什么联系呢? 二、主要内容 2.1、permute函数和transpose函数 Tensor.permute(a,b,c,d, …):... -
matlab permute函数
2017-09-26 21:23:37A=[1 3 2 2 8 7] permute(A,[1,2,3])=A permute(A,[2,1,3])=A' permute(A,[1,3,2])= [1 2]',[3 8]',[2,7]' permute(B,[3,1,2])= [1 2],[3 8],[2,7] permute(B,[2,3,1])= [1 3 2]' -
Pytorch之permute函数:用于调换不同维度的顺序,BCHW -> NCHW
2020-12-22 20:56:06非常方便 MY transpose与permute的异同 ...Tensor.permute(a,b,c,d, ...):permute函数可以对任意高维矩阵进行转置,但没有 torch.permute() 这个调用方式, 只能 Tensor.permute(): 可以从链接中看到 ... -
python中permute_Pytorch之permute函数
2020-12-10 21:17:071 先看看官方中英文doc:torch.Tensor.permute (Python method, in torch.Tensor)1.1 permute(dims)将tensor的维度换位。参数: - __dims__ (int ..*) - 换位顺序例:>>> x = torch.randn(2, 3, 5)>>... -
python pytorch permute函数
2018-12-18 18:52:34permute(多维数组,[维数的组合]) 比如: a=rand(2,3,4); %这是一个三维数组,各维的长度分别为:2,3,4 %现在交换第一维和第二维: permute(A,[2,1,3]) %变成3*2*4的矩阵 import torch import numpy as np ... -
Pytorch之permute函数
2020-03-31 17:08:471、主要作用:变换tensor维度 example: import torch x = torch.randn(2, 3, ...print(x.permute(2, 0, 1).size()) >>>torch.Size([2, 3, 5]) >>>torch.Size([5, 2, 3]) 2、介绍一下transpo... -
## pytorch中view和permute函数改变张量维度方式的不同
2019-09-04 20:19:06pytorch中view和permute函数改变张量维度方式的不同 说的有点太抽象了。结合代码自己理解一下吧。。 实验代码: import torch import numpy as np a=np.array([ [ [1,2,3], [4,5,6] ] ... -
permute函数_permute()和view()
2020-12-06 15:04:06前几天使用pytorch时,需要对tensor进行转置,请注意要使用函数permute()而不是view()很简单,只需要举两个例子就明白区别,view()的运行方式是这样的:a = torch.tensor([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])... -
permute函数_Pytorch之contiguous函数
2020-12-06 15:04:08示例如下: x = torch.Tensor(2,3) y = x.permute(1,0) # permute:二维tensor的维度变换,此处功能相当于转置transpose y.view(-1) # 报错,view使用前需调用contiguous()函数 y = x.permute(1,0).contiguous() y.... -
python transpose与permute函数详解
2020-11-04 10:23:59permute用法:tensor.permute(a,b,c) transpose仅可以进行二维转置,而permute则不仅限于二维,可以进行多维度转置 举例: import torch y = torch.randn(5, 10, 15) print(y.size()) print(y.view(-1, ... -
permute函数_Pytorch常用函数之一_数据类型
2020-12-06 15:04:09机器学习需要掌握数据处理工具Pandas、Numpy,同理,深度学习也需要掌握相应的工具,在Pytorch中数据存储在Tensor之中,本篇将介绍它们的基本用法以及与之相关的常用函数。查看版本信息包含头文件1. import torch 1... -
Matlab中repmat、permute函数用法
2018-01-16 21:36:13repmat函数用法 复制和平铺矩阵 函数repmat 格式: B = repmat(A, m, n) %将矩阵A复制m*n块,即B由m*n块A平铺而成 B = repmat(A, [m n])%与上面一致 B = repmat(A, [m n p...]) %B由m*n*p*...个A块平铺而... -
matlab中repmat函数, ndims 函数 与 expand 函数 reshape函数,shiftdim函数和permute函数的用法
2017-07-31 13:45:58B = repmat(A,m,n) B = repmat(A,[m n]) ...permute的功能比shiftdim强大,可以实现维号的任意顺序排列(或称置换),而shiftdim只是permute按左移(或右移)排列(数学上称为轮换)的一个特例。 -
matlab 交换矩阵维度------ permute函数
2018-07-08 20:18:03permute(A,[2,1,3]),若A是一个3维矩阵,此行代码交换矩阵A的第一维与第二维。 -
permute函数_Dynamic ReLU(ECCV'20):一种根据输入自适应动态调节的激活函数
2020-12-06 15:04:07导读:ReLU是深度神经网络中常用的激活函数。到目前为止,ReLU及其推广(非参数或参数)都是静态的,对所有的输入样本执行相同的操作。在本文中,作者提出了Dynamic ReLU激活函数(DY-ReLU),它的参数依赖于所有输入。... -
permute函数_pytorch的基本使用, 函数实现SDG法训练网络, 梯度下降法(DG)案例
2020-11-27 23:02:40数学含义是: 目标函数在此点上的梯度(一阶导数)值为 0, 但从改点出发的一个方向是函数的极大值点,而在另一个方向是函数的极小值点 鞍点的例子 , 函数图形为 的(0,0)也是鞍点 2.3 Stochastic Gradient Descent ...
-
安卓数据库案例
-
文本分析项目-源码
-
2016年上半年 网络工程师 上午试卷 综合知识 软考真题【含答案和答案解析】
-
2019年下半年 软件设计师 上午试卷 综合知识 软考真题【含答案和答案解析】
-
C++MFC开发远程控制软件教程(VS2013)
-
项目管理工具与方法
-
区块链应用开发实战(Go语言方向)
-
Jenkins软件开发持续集成及自动构建
-
scala:分别使用懒汉式和饿汉式实现单例模式
-
利用qt对数据库进行操作
-
通过成形InGaN / GaN纳米棒来修改远场辐射图
-
MHA 高可用 MySQL 架构与 Altas 读写分离
-
使用dockerfile-maven-plugin将java项目打包并推送到阿里云私服
-
CS3100-UNO-源码
-
【布道者】Linux极速入门
-
2015年下半年 信息系统监理师 上午试卷 综合知识 软考真题【含答案和答案解析】
-
java 多线程
-
全局绝热搜索算法的电路模型
-
朱老师C++课程第3部分-3.6智能指针与STL查漏补缺
-
结合使用电纺丝和微成型来调节雪旺氏细胞行为,制备取向聚己内酯支架