PyTorch Torch.permute()根据所需顺序重新排列原始张量,并返回新的多维旋转张量。返回的张量的大小与原始大小相同。
用法: torch.permute(*dims)
参数:
- dims:按张量的所需尺寸顺序排列的索引序列(索引从零开始)。
Return:具有所需尺寸顺序的张量。
让我们借助几个示例来了解这个概念:
范例1:创建大小为2×4的二维张量,然后进行排列。
Python3
# import pytorch library
import torch
# create a tensor of size 2 x 4
input_var = torch.randn(2,4)
# print size
print(input_var.size())
print(input_var)
# dimensions permuted
input_var = input_var.permute(1, 0)
# print size
print(input_var.size())
print(input_var)输出:
torch.Size([2, 4])
tensor([[ 0.9801, 0.5296, 0.5449, -1.1481],
[-0.6762, -0.1161, 0.6360, -0.5371]])
torch.Size([4, 2])
tensor([[ 0.9801, -0.6762],
[ 0.5296, -0.1161],
[ 0.5449, 0.6360],
[-1.1481, -0.5371]])
范例2:创建大小为3×5×2的三维张量,然后进行排列。
Python3
# import pytorch library
import torch
# creating a tensor with random
# values of dimension 3 X 5 X 2
input_var = torch.randn(3, 5, 2)
# print size
print(input_var.size())
print(input_var)
# dimensions permuted
input_var = input_var.permute(2, 0, 1)
# print size
print(input_var.size())
print(input_var)输出:
torch.Size([3, 5, 2])
tensor([[[ 0.2059, -0.7165],
[-1.1305, 0.5886],
[-0.1247, -0.4969],
[-0.5788, 0.0159],
[ 1.4304, 0.6014]],
[[ 2.4882, -0.3910],
[-0.5558, 0.6903],
[-0.4219, -0.5498],
[-0.5346, -0.0703],
[ 1.1497, -0.3252]],
[[-0.5075, 0.5752],
[ 1.3738, -0.3321],
[-0.3317, -0.9209],
[-1.6677, -1.1471],
[-0.9269, -0.6493]]])
torch.Size([2, 3, 5])
tensor([[[ 0.2059, -1.1305, -0.1247, -0.5788, 1.4304],
[ 2.4882, -0.5558, -0.4219, -0.5346, 1.1497],
[-0.5075, 1.3738, -0.3317, -1.6677, -0.9269]],
[[-0.7165, 0.5886, -0.4969, 0.0159, 0.6014],
[-0.3910, 0.6903, -0.5498, -0.0703, -0.3252],
[ 0.5752, -0.3321, -0.9209, -1.1471, -0.6493]]])
相关用法
- Python PyTorch sin()用法及代码示例
- Python PyTorch sinh()用法及代码示例
- Python PyTorch cosh()用法及代码示例
- Python PyTorch tanh()用法及代码示例
- Python PyTorch cos()用法及代码示例
- Python PyTorch tan()用法及代码示例
- Python PyTorch asin()用法及代码示例
- Python PyTorch acos()用法及代码示例
- Python PyTorch atan()用法及代码示例
- Python PyTorch zeros()用法及代码示例
- Python Pytorch ones()用法及代码示例
- Python Pytorch arange()用法及代码示例
- Python Pytorch linspace()用法及代码示例
- Python Pytorch range()用法及代码示例
- Python Pytorch logspace()用法及代码示例
- Python Pytorch eye()用法及代码示例
- Python Pytorch empty()用法及代码示例
- Python Pytorch full()用法及代码示例
- Python PyTorch floor()用法及代码示例
- Python PyTorch fmod()用法及代码示例
注:本文由纯净天空筛选整理自danf7861大神的英文原创作品 Python – Pytorch permute() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
