,
torch.atleast_3dは、PyTorch で入力テンソルを少なくとも3次元に変換するために使用される関数です。入力の次元が3次元未満の場合、自動的に次元を追加して3次元にします。
]]) 形状: torch.Size([3, 1, 1])
torch.atleast_3d(*tensors)
使用例
]]) 形状: torch.Size([1, 1, 1])
import torch
# スカラーを3次元テンソルに変換
x = torch.atleast_3d(5)
print("スカラー変換後:", x, "形状:", x.shape)
# 出力: スカラー変換後: tensor([[
# 1次元テンソルを3次元に変換
x = torch.tensor([1, 2, 3])
y = torch.atleast_3d(x)
print("1次元から3次元へ:", y, "形状:", y.shape)
# 出力: 1次元から3次元へ: tensor([[
# 2次元テンソルを3次元に変換
x = torch.tensor([[1, 2], [3, 4]])
y = torch.atleast_3d(x)
print("2次元から3次元へ:", y, "形状:", y.shape)
# 出力: 2次元から3次元へ: tensor([[[1, 2], [3, 4]]]) 形状: torch.Size([1, 2, 2])
# すでに3次元のテンソルはそのまま保持
x = torch.tensor([[[1, 2], [3, 4]]])
y = torch.atleast_3d(x)
print("3次元テンソル:", y, "形状:", y.shape)
# スカラーを3次元テンソルに変換
x = torch.atleast_3d(5)
print("スカラー変換後:", x, "形状:", x.shape)
# 出力: スカラー変換後: tensor([[
# 1次元テンソルを3次元に変換
x = torch.tensor([1, 2, 3])
y = torch.atleast_3d(x)
print("1次元から3次元へ:", y, "形状:", y.shape)
# 出力: 1次元から3次元へ: tensor([[
# 2次元テンソルを3次元に変換
x = torch.tensor([[1, 2], [3, 4]])
y = torch.atleast_3d(x)
print("2次元から3次元へ:", y, "形状:", y.shape)
# 出力: 2次元から3次元へ: tensor([[[1, 2], [3, 4]]]) 形状: torch.Size([1, 2, 2])
# すでに3次元のテンソルはそのまま保持
x = torch.tensor([[[1, 2], [3, 4]]])
y = torch.atleast_3d(x)
print("3次元テンソル:", y, "形状:", y.shape)
その他の拡張
Pytorch torch リファレンスマニュアル