PyTorch torch.chunk 函数

约 326 字 更新于 2026-11-18 5iyo.com

PyTorch torch.chunk 函数


torch.chunk 是 PyTorch 中用于将张量沿指定维度分块的函数。

函数定义

torch.chunk(tensor, chunks, dim)

使用示例

实例

import torch

x = torch.arange(12).reshape(3, 4)

# 分成 3 块
result = torch.chunk(x, 3, dim=0)

print("块数:", len(result))
for i, t in enumerate(result):
    print(f"Chunk {i}:", t.shape)