PyTorch torch.rot90 関数
torch.rot90これは PyTorch でテンソルを90度回転させるための関数です。
関数定義
torch.rot90(input, k=1, dims=[0, 1])
パラメータの説明:
input- 入力テンソルk- 回転回数。正の数は反時計回り、負の数は時計回り(デフォルトは1)dims- 回転を行う平面の次元(デフォルトは [0, 1])
使用例
例
import torch
# 2次元テンソルの回転
x = torch.arange(12).reshape(3, 4)
print("元のテンソル:")
print(x)
result = torch.rot90(x, 1)
print("反時計回りに90度回転:")
print(result)
result = torch.rot90(x, -1)
print("n時計回りに90度回転:")
print(result)
result = torch.rot90(x, 2)
print("n180度回転:")
print(result)
# 指定した平面に沿って回転(3次元テンソル)
y = torch.arange(24).reshape(2, 3, 4)
print("n3次元テンソルの形状:", y.shape)
result = torch.rot90(y, 1, dims=[1, 2])
print("[1, 2] 平面に沿って90度回転した後の形状:", result.shape)
# 2次元テンソルの回転
x = torch.arange(12).reshape(3, 4)
print("元のテンソル:")
print(x)
result = torch.rot90(x, 1)
print("反時計回りに90度回転:")
print(result)
result = torch.rot90(x, -1)
print("n時計回りに90度回転:")
print(result)
result = torch.rot90(x, 2)
print("n180度回転:")
print(result)
# 指定した平面に沿って回転(3次元テンソル)
y = torch.arange(24).reshape(2, 3, 4)
print("n3次元テンソルの形状:", y.shape)
result = torch.rot90(y, 1, dims=[1, 2])
print("[1, 2] 平面に沿って90度回転した後の形状:", result.shape)
出力結果は以下の通りです:
原始张量:
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
逆时针旋转 90 度:
tensor([[ 3, 7, 11],
[ 2, 6, 10],
[ 1, 5, 9],
[ 0, 4, 8]])
顺时针旋转 90 度:
tensor([[ 8, 4, 0],
[ 9, 5, 1],
[10, 6, 2],
[11, 7, 3]])
旋转 180 度:
tensor([[11, 10, 9, 8],
[ 7, 6, 5, 4],
[ 3, 2, 1, 0]])
三维张量形状: torch.Size([2, 3, 4])
沿 [1, 2] 平面旋转 90 度后形状: torch.Size([2, 4, 3])
その他の拡張
Pytorch torch リファレンスマニュアル