PyTorch torch.unbind 関数


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

torch.unbindPyTorch において、指定した次元に沿ってテンソルをタプルに分割するための関数です。

関数定義

torch.unbind(input, dim=0)

使用例

サンプル

import torch

x = torch.arange(12).reshape(3, 4)
print(元のテンソル:)
print(x)

result = torch.unbind(x, dim=0)
print(dim=0 に沿って分割:)
for i, t in enumerate(result):
    print(fブロック {i}: {t})

result = torch.unbind(x, dim=1)
print(dim=1 に沿って分割:)
for i, t in enumerate(result):
    print(fブロック {i}: {t})

出力結果は次のとおりです:

原始张量:
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
沿 dim=0 分割:
  块 0: tensor([0, 1, 2, 3])
  チャンク1: tensor([4, 5, 6, 7])
  チャンク2: tensor([ 8,  9, 10, 11])
沿 dim=1 分割:
  块 0: tensor([0, 4, 8])
  チャンク1: tensor([1, 5, 9])
  チャンク2: tensor([ 2,  6, 10])
  チャンク3: tensor([ 3,  7, 11])

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

その他の拡張