Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creation of hsplit entry for pytorch #5556

Merged
merged 6 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions content/pytorch/concepts/tensor-operations/terms/hsplit/hsplit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
Title: '.hsplit()'
Description: 'Splits a tensor horizontally into multiple sub-tensors.'
Subjects:
- 'AI'
- 'Data Science'
Tags:
- 'AI'
- 'Arrays'
- 'Data Structures'
- 'Deep Learning'
CatalogContent:
- 'intro-to-py-torch-and-neural-networks'
- 'paths/computer-science'
---

In PyTorch, the **`.hsplit()`** method splits a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) into multiple sub-tensors horizontally (column-wise) along the specified dimension (axis).

## Syntax

```pseudo
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:

```py
import torch

# Define a tensor
tensor = torch.tensor([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])

# Split the tensor into 2 equal parts
sub_tensors = torch.hsplit(tensor, 2)

print(sub_tensors)
```

The above code produces the following output:

```shell
(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.
Loading