PyTorch torch.vsplit 関数


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

torch.vsplitPyTorch で、垂直(行方向)にテンソルを分割するための関数です。

関数定義

torch.vsplit(input, indices_or_sections)

使用例

実例

import torch

# 2次元テンソルの垂直分割
x = torch.arange(12).reshape(4, 3)
print("元の2次元テンソル:")
print(x)

result = torch.vsplit(x, 2)
print("行方向に均等に2分割:")
for i, t in enumerate(result):
    print(f" ブロック {i}:n{t}")

# インデックスで分割
result = torch.vsplit(x, [1, 3])
print("nインデックス [1, 3] で分割:")
for i, t in enumerate(result):
    print(f" ブロック {i}:n{t}")

# 3次元テンソルの垂直分割
y = torch.arange(24).reshape(4, 3, 2)
print("n3次元テンソル:")
print(y)

result = torch.vsplit(y, 2)
print("n最初の次元に沿って2分割:")
for i, t in enumerate(result):
    print(f" ブロック {i}: {t.shape}")

出力結果は:

原始二维张量:
tensor([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11]])
沿行平均分为 2 份:
  块 0:
tensor([[0, 1, 2],
        [3, 4, 5]])
  チャンク1:
tensor([[ 6,  7,  8],
        [ 9, 10, 11]])

按索引 [1, 3] 分割:
  块 0:
tensor([[0, 1, 2]])
  チャンク1:
tensor([[3, 4, 5],
        [6, 7, 8]])
  チャンク2:
tensor([[ 9, 10, 11]])

三维张量:
tensor([[[ 0,  1],
         [ 3,  4],
         [ 5,  6]],

        [[ 7,  8],
         [ 9, 10],
         [11, 12]],

        [[13, 14],
         [15, 16],
         [17, 18]],

        [[19, 20],
         [21, 22],
         [23, 24]]])
沿第一维分为 2 份:
  块 0: torch.Size([2, 3, 2])
  チャンク1: torch.Size([2, 3, 2])

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

その他の拡張