目錄
chunk切塊
chunk???????切塊
import torch# 創建一個形狀為 [2, 3, 4] 的張量
x = torch.arange(6).reshape(2, 3)
print("原始張量形狀:", x.shape)
print("x:", x)
# 輸出: 原始張量形狀: torch.Size([2, 3, 4])# 沿著最后一個維度分割成 2 塊
chunks = x.chunk(2, dim=-1)
print("分割后的塊數量:", len(chunks))
# 輸出: 分割后的塊數量: 2# 查看每個塊的形狀
for i, chunk in enumerate(chunks):print(f"塊 {i} 的形狀:", chunk)
結果:
原始張量形狀: torch.Size([2, 3])
x: tensor([[0, 1, 2],
? ? ? ? [3, 4, 5]])
分割后的塊數量: 2
塊 0 的形狀: tensor([[0, 1],
? ? ? ? [3, 4]])
塊 1 的形狀: tensor([[2],
? ? ? ? [5]])