torch : transpose,transpose_
import torch import torchvision import torch.nn as nn import torch.nn.functional as F # x = torch.randn(2, 3) x = torch.randn(2, 3,4) print(x) print() y = torch.transpose(x, 0, 1) print(y) print() y = torch.transpose(x, 2, 1) print(y) y.transpose_(2, 1) #In-place version of transpose() print(y)
D:\ProgramData\Miniconda3\python.exe E:/新脚本主文件夹/训练测试项目/test_torch/TRANSPOSE.py tensor([[[ 0.0745, -0.1144, -0.4868, 2.2905], [-0.0286, 0.6754, 0.4262, -0.8590], [ 0.3684, -0.2710, 0.6782, -0.5027]], [[ 0.9876, 1.1178, 1.1179, 0.6800], [ 0.4692, 0.5668, -0.6782, 0.6920], [-0.9431, 1.5257, 0.1621, 1.5187]]]) tensor([[[ 0.0745, -0.1144, -0.4868, 2.2905], [ 0.9876, 1.1178, 1.1179, 0.6800]], [[-0.0286, 0.6754, 0.4262, -0.8590], [ 0.4692, 0.5668, -0.6782, 0.6920]], [[ 0.3684, -0.2710, 0.6782, -0.5027], [-0.9431, 1.5257, 0.1621, 1.5187]]]) tensor([[[ 0.0745, -0.0286, 0.3684], [-0.1144, 0.6754, -0.2710], [-0.4868, 0.4262, 0.6782], [ 2.2905, -0.8590, -0.5027]], [[ 0.9876, 0.4692, -0.9431], [ 1.1178, 0.5668, 1.5257], [ 1.1179, -0.6782, 0.1621], [ 0.6800, 0.6920, 1.5187]]]) tensor([[[ 0.0745, -0.1144, -0.4868, 2.2905], [-0.0286, 0.6754, 0.4262, -0.8590], [ 0.3684, -0.2710, 0.6782, -0.5027]], [[ 0.9876, 1.1178, 1.1179, 0.6800], [ 0.4692, 0.5668, -0.6782, 0.6920], [-0.9431, 1.5257, 0.1621, 1.5187]]]) Process finished with exit code 0