PyTorch torch.flatten 関数
torch.flattenPyTorch でテンソルを平坦化するための関数です。
関数定義
torch.flatten(input, start_dim, end_dim, out)
使用例
実例
import torch
x = torch.randn(2, 3, 4)
# 1次元に平坦化
y = torch.flatten(x)
print("元の形状:", x.shape)
print("平坦化後:", y.shape)
# 指定した次元から平坦化
z = torch.flatten(x, start_dim=1)
print("dim=1 から平坦化:", z.shape)
x = torch.randn(2, 3, 4)
# 1次元に平坦化
y = torch.flatten(x)
print("元の形状:", x.shape)
print("平坦化後:", y.shape)
# 指定した次元から平坦化
z = torch.flatten(x, start_dim=1)
print("dim=1 から平坦化:", z.shape)
出力結果は次のとおりです。
原始形状: torch.Size([2, 3, 4]) 展平后: torch.Size([24]) 从 dim=1 展平: torch.Size([2, 12])
その他の拡張
Pytorch torch リファレンスマニュアル