PyTorch torch.flatten 関数


Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

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)

出力結果は次のとおりです。

原始形状: torch.Size([2, 3, 4])
展平后: torch.Size([24])
从 dim=1 展平: torch.Size([2, 12])

Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

その他の拡張