.hsplit()
Published Nov 6, 2024
Contribute to Docs
In PyTorch, the .hsplit()
method splits a tensor into multiple sub-tensors horizontally (column-wise) along the specified dimension (axis).
Syntax
torch.hsplit(tensor, indices_or_sections)
tensor
: The tensor to be split.indices_or_sections
: This can be an integer or a list of integers.- If it’s an integer, it specifies the number of equal-sized sub-tensors to split the tensor into.
- If it’s a list of integers, it specifies the sizes of each sub-tensor along the specified dimension.
Example
The following example demonstrates the usage of the .hsplit()
method:
import torch# Define a tensortensor = torch.tensor([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]])# Split the tensor into 2 equal partssub_tensors = torch.hsplit(tensor, 2)print(sub_tensors)
The above code produces the following output:
(tensor([[ 1, 2],[ 5, 6],[ 9, 10]]), tensor([[ 3, 4],[ 7, 8],[11, 12]]))
The tensor is split into two sub-tensors along the columns. The first sub-tensor contains the first two columns of the original tensor, while the second sub-tensor contains the last two columns.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours