split

Documentation

treetensor.torch.split(tensor, split_size_or_sections, *args, **kwargs)[source]

Splits the tensor into chunks. Each chunk is a view of the original tensor.

Examples:

>>> import torch
>>> import treetensor.torch as ttorch
>>> t1 = torch.randint(100, (6, 2))
>>> t1
tensor([[59, 82],
        [86, 42],
        [71, 84],
        [61, 58],
        [82, 37],
        [14, 31]])
>>> ttorch.split(t1, (1, 2, 3))
(tensor([[59, 82]]), tensor([[86, 42],
        [71, 84]]), tensor([[61, 58],
        [82, 37],
        [14, 31]]))

>>> tt1 = ttorch.randint(100, {
...     'a': (6, 2),
...     'b': {'x': (6, 2, 3)},
... })
>>> tt1
<Tensor 0x7f4c8d786400>
├── a --> tensor([[ 1, 65],
│                 [68, 31],
│                 [76, 73],
│                 [74, 76],
│                 [90,  0],
│                 [95, 89]])
└── b --> <Tensor 0x7f4c8d786320>
    └── x --> tensor([[[11, 20, 74],
                       [17, 85, 44]],

                      [[67, 37, 89],
                       [76, 28,  0]],

                      [[56, 12,  7],
                       [17, 63, 32]],

                      [[81, 75, 19],
                       [89, 21, 55]],

                      [[71, 53,  0],
                       [66, 82, 57]],

                      [[73, 81, 11],
                       [58, 54, 78]]])
>>> ttorch.split(tt1, (1, 2, 3))
(<Tensor 0x7f4c8d7861d0>
├── a --> tensor([[ 1, 65]])
└── b --> <Tensor 0x7f4c8d786128>
    └── x --> tensor([[[11, 20, 74],
                       [17, 85, 44]]])
, <Tensor 0x7f4c8d7860f0>
├── a --> tensor([[68, 31],
│                 [76, 73]])
└── b --> <Tensor 0x7f4c8d7860b8>
    └── x --> tensor([[[67, 37, 89],
                       [76, 28,  0]],

                      [[56, 12,  7],
                       [17, 63, 32]]])
, <Tensor 0x7f4c8d7866d8>
├── a --> tensor([[74, 76],
│                 [90,  0],
│                 [95, 89]])
└── b --> <Tensor 0x7f4c8d786668>
    └── x --> tensor([[[81, 75, 19],
                       [89, 21, 55]],

                      [[71, 53,  0],
                       [66, 82, 57]],

                      [[73, 81, 11],
                       [58, 54, 78]]])
)

Torch Version Related

This documentation is based on torch.split in torch v1.10.0+cu102. Its arguments’ arrangements depend on the version of pytorch you installed.

If some arguments listed here are not working properly, please check your pytorch’s version with the following command and find its documentation.

1
python -c 'import torch;print(torch.__version__)'

The arguments and keyword arguments supported in torch v1.10.0+cu102 is listed below.

Description From Torch v1.10.0+cu102

torch.split(tensor, split_size_or_sections, dim=0)[source]

Splits the tensor into chunks. Each chunk is a view of the original tensor.

If split_size_or_sections is an integer type, then tensor will be split into equally sized chunks (if possible). Last chunk will be smaller if the tensor size along the given dimension dim is not divisible by split_size.

If split_size_or_sections is a list, then tensor will be split into len(split_size_or_sections) chunks with sizes in dim according to split_size_or_sections.

Args:

tensor (Tensor): tensor to split. split_size_or_sections (int) or (list(int)): size of a single chunk or

list of sizes for each chunk

dim (int): dimension along which to split the tensor.

Example:

>>> a = torch.arange(10).reshape(5,2)
>>> a
tensor([[0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9]])
>>> torch.split(a, 2)
(tensor([[0, 1],
         [2, 3]]),
 tensor([[4, 5],
         [6, 7]]),
 tensor([[8, 9]]))
>>> torch.split(a, [1,4])
(tensor([[0, 1]]),
 tensor([[2, 3],
         [4, 5],
         [6, 7],
         [8, 9]]))