PyTorch torch.split_with_sizes 関数
torch.split_with_sizesPyTorch で指定されたサイズに従ってテンソルを分割するための関数です。
関数定義
torch.split_with_sizes(input, split_sizes, dim=0)
使用例
実例
import torch
x = torch.arange(10)
print("元のテンソル:")
print(x)
# サイズ [2, 3, 5] で分割
result = torch.split_with_sizes(x, [2, 3, 5])
print("分割結果:")
for i, t in enumerate(result):
print(f" ブロック {i}: {t}")
# 2次元テンソルを行ごとに分割
y = torch.arange(12).reshape(4, 3)
print("n元の2次元テンソル:")
print(y)
result = torch.split_with_sizes(y, [1, 2, 1], dim=0)
print("行ごとに分割 [1, 2, 1]:")
for i, t in enumerate(result):
print(f" ブロック {i}:n{t}")
x = torch.arange(10)
print("元のテンソル:")
print(x)
# サイズ [2, 3, 5] で分割
result = torch.split_with_sizes(x, [2, 3, 5])
print("分割結果:")
for i, t in enumerate(result):
print(f" ブロック {i}: {t}")
# 2次元テンソルを行ごとに分割
y = torch.arange(12).reshape(4, 3)
print("n元の2次元テンソル:")
print(y)
result = torch.split_with_sizes(y, [1, 2, 1], dim=0)
print("行ごとに分割 [1, 2, 1]:")
for i, t in enumerate(result):
print(f" ブロック {i}:n{t}")
出力結果は次のとおりです:
原始张量:
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
分割结果:
块 0: tensor([0, 1])
チャンク1: tensor([2, 3, 4])
チャンク2: tensor([5, 6, 7, 8, 9])
原始二维张量:
tensor([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
按行分割 [1, 2, 1]:
块 0:
tensor([[0, 1, 2]])
チャンク1:
tensor([[3, 4, 5],
[6, 7, 8]])
チャンク2:
tensor([[ 9, 10, 11]])
その他の拡張
Pytorch torch リファレンスマニュアル