DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

split in PyTorch

Buy Me a Coffee

*Memos:

split() can get the one or more 1D or more D splitted view tensors of zero or more elements from the 1D or more D tensor of zero or more elements as shown below:

*Memos:

  • split() can be used with torch or a tensor.
  • The 1st argument(tensor) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
  • The 2nd argument with torch or the 1st argument with a tensor is split_size_or_sections(Required-Type:int, tuple of int or list of int). *Don't use split_size_or_sections= with a tensor.
  • The 3rd argument with torch or the 2nd argument with a tensor is dim(Optional-Default:0-Type:int).
  • The total number of the zero or more elements of one or more returned tensors changes.
  • One or more returned tensors keep the dimension of the original tensor.
import torch

my_tensor = torch.tensor([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]])

torch.split(tensor=my_tensor, split_size_or_sections=1)
my_tensor.split(1)
torch.split(tensor=my_tensor, split_size_or_sections=1, dim=0)
torch.split(tensor=my_tensor, split_size_or_sections=1, dim=-2)
# (tensor([[0, 1, 2, 3]]),
#  tensor([[4, 5, 6, 7]]),
#  tensor([[8, 9, 10, 11]]))

torch.split(tensor=my_tensor, split_size_or_sections=1, dim=1)
torch.split(tensor=my_tensor, split_size_or_sections=1, dim=-1)
# (tensor([[0], [4], [8]]),
#  tensor([[1], [5], [9]]),
#  tensor([[2], [6], [10]]),
#  tensor([[3], [7], [11]]))

torch.split(tensor=my_tensor, split_size_or_sections=2)
torch.split(tensor=my_tensor, split_size_or_sections=2, dim=0)
torch.split(tensor=my_tensor, split_size_or_sections=2, dim=-2)
# (tensor([[0, 1, 2, 3], [4, 5, 6, 7]]),
#  tensor([[8, 9, 10, 11]]))

torch.split(tensor=my_tensor, split_size_or_sections=2, dim=1)
torch.split(tensor=my_tensor, split_size_or_sections=2, dim=-1)
# (tensor([[0, 1], [4, 5], [8, 9]]),
#  tensor([[2, 3], [6, 7], [10, 11]]))

torch.split(tensor=my_tensor, split_size_or_sections=3)
torch.split(tensor=my_tensor, split_size_or_sections=3, dim=0)
torch.split(tensor=my_tensor, split_size_or_sections=3, dim=-2)
# (tensor([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]),)

torch.split(tensor=my_tensor, split_size_or_sections=3, dim=1)
torch.split(tensor=my_tensor, split_size_or_sections=3, dim=-1)
# (tensor([[0, 1, 2], [4, 5, 6], [8, 9, 10]]),
#  tensor([[3], [7], [11]]))

torch.split(tensor=my_tensor, split_size_or_sections=(0, 3))
torch.split(tensor=my_tensor, split_size_or_sections=(0, 3), dim=0)
torch.split(tensor=my_tensor, split_size_or_sections=(0, 3), dim=-2)
# (tensor([], size=(0, 4), dtype=torch.int64),
#  tensor([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]))

torch.split(tensor=my_tensor, split_size_or_sections=(1, 2))
torch.split(tensor=my_tensor, split_size_or_sections=(1, 2), dim=0)
torch.split(tensor=my_tensor, split_size_or_sections=(1, 2), dim=-2)
# (tensor([[0, 1, 2, 3]]),
#  tensor([[4, 5, 6, 7], [8, 9, 10, 11]]))

torch.split(tensor=my_tensor, split_size_or_sections=(2, 1))
torch.split(tensor=my_tensor, split_size_or_sections=(2, 1), dim=0)
torch.split(tensor=my_tensor, split_size_or_sections=(2, 1), dim=-2)
# (tensor([[0, 1, 2, 3], [4, 5, 6, 7]]),
#  tensor([[8, 9, 10, 11]]))

torch.split(tensor=my_tensor, split_size_or_sections=(3, 0))
torch.split(tensor=my_tensor, split_size_or_sections=(3, 0), dim=0)
# (tensor([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]),
#  tensor([], size=(0, 4), dtype=torch.int64))

torch.split(tensor=my_tensor, split_size_or_sections=(1, 1, 1))
torch.split(tensor=my_tensor, split_size_or_sections=(1, 1, 1), dim=0)
torch.split(tensor=my_tensor, split_size_or_sections=(1, 1, 1), dim=-2)
# (tensor([[0, 1, 2, 3]]),
#  tensor([[4, 5, 6, 7]]), 
#  tensor([[ 8,  9, 10, 11]]))

my_tensor = torch.tensor([[0., 1., 2., 3.],
                          [4., 5., 6., 7.],
                          [8., 9., 10., 11.]])
torch.split(tensor=my_tensor, split_size_or_sections=1)
# (tensor([[0., 1., 2., 3.]]),
#  tensor([[4., 5., 6., 7.]]),
#  tensor([[8., 9., 10., 11.]]))

my_tensor = torch.tensor([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j],
                          [4.+0.j, 5.+0.j, 6.+0.j, 7.+0.j],
                          [8.+0.j, 9.+0.j, 10.+0.j, 11.+0.j]])
torch.split(tensor=my_tensor, split_size_or_sections=1)
# (tensor([[0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j]]),
#  tensor([[4.+0.j, 5.+0.j, 6.+0.j, 7.+0.j]]),
#  tensor([[8.+0.j, 9.+0.j, 10.+0.j, 11.+0.j]]))

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True],
                          [True, False, True, False]])
torch.split(tensor=my_tensor, split_size_or_sections=1)
# (tensor([[True, False, True, False]]),
#  tensor([[False, True, False, True]]),
#  tensor([[True, False, True, False]]))
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay