PyTorch torch.hsplit 関数
torch.hsplitこれは、PyTorch で水平方向(列に沿って)テンソルを分割するための関数です。
関数定義
torch.hsplit(input, indices_or_sections)
使用例
実例
import torch
# 1次元テンソルの水平分割
x = torch.arange(10)
print("元の1次元テンソル:")
print(x)
result = torch.hsplit(x, 2)
print("2等分:")
for i, t in enumerate(result):
print(f" ブロック {i}: {t}")
# 2次元テンソルの水平分割
y = torch.arange(12).reshape(3, 4)
print("n元の2次元テンソル:")
print(y)
result = torch.hsplit(y, 2)
print("列に沿って2つに分割:")
for i, t in enumerate(result):
print(f" ブロック {i}:n{t}")
# インデックスで分割
result = torch.hsplit(y, [1, 3])
print("nインデックス [1, 3] で分割:")
for i, t in enumerate(result):
print(f" ブロック {i}:n{t}")
# 1次元テンソルの水平分割
x = torch.arange(10)
print("元の1次元テンソル:")
print(x)
result = torch.hsplit(x, 2)
print("2等分:")
for i, t in enumerate(result):
print(f" ブロック {i}: {t}")
# 2次元テンソルの水平分割
y = torch.arange(12).reshape(3, 4)
print("n元の2次元テンソル:")
print(y)
result = torch.hsplit(y, 2)
print("列に沿って2つに分割:")
for i, t in enumerate(result):
print(f" ブロック {i}:n{t}")
# インデックスで分割
result = torch.hsplit(y, [1, 3])
print("nインデックス [1, 3] で分割:")
for i, t in enumerate(result):
print(f" ブロック {i}:n{t}")
出力結果は次のとおりです:
原始一维张量:
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
平均分为 2 份:
块 0: tensor([0, 1, 2, 3, 4])
チャンク1: tensor([5, 6, 7, 8, 9])
原始二维张量:
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
沿列分为 2 份:
块 0:
tensor([[0, 1],
[4, 5],
[8, 9]])
チャンク1:
tensor([[ 2, 3],
[ 6, 7],
[10, 11]])
按索引 [1, 3] 分割:
块 0:
tensor([[0],
[4],
[8]])
チャンク1:
tensor([[ 1, 2],
[ 5, 6],
[ 9, 10]])
チャンク2:
tensor([[ 3],
[ 7],
[11]])
その他の拡張
Pytorch torch リファレンスマニュアル