Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sriparno08 committed Nov 2, 2024
1 parent 643ec38 commit 3e9ccdc
Showing 1 changed file with 11 additions and 28 deletions.
39 changes: 11 additions & 28 deletions content/pytorch/concepts/tensor-operations/terms/hstack/hstack.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Title: '.hstack()'
Description: 'Concatenates two or more tensors along the horizontal axis (column-wise)'
Description: 'Concatenates two or more tensors along the horizontal axis (column-wise).'
Subjects:
- 'AI'
- 'Data Science'
Expand All @@ -14,57 +14,40 @@ CatalogContent:
- 'paths/data-science'
---

In PyTorch, **`.hstack()`** (short for horizontal stack) is a function used to
concatenate two or more tensors along the horizontal axis (`axis=1`).
This operation is helpful in combining data with the same number of rows
but differing in the number of columns. It acts similarly to NumPy's `np.
hstack()` and is particularly handy for data that needs to be concatenated side
by side before being fed into a model for training or inference.
In PyTorch, **`.hstack()`** (short for horizontal stack) is a function used to concatenate two or more [tensors](https://www.codecademy.com/resources/docs/pytorch/tensors) along the horizontal axis (`axis=1`). This operation is helpful in combining data with the same number of rows but differing in the number of columns. It acts similarly to **`np.hstack()`** in [NumPy](https://www.codecademy.com/resources/docs/numpy) and is particularly handy for data that needs to be concatenated side by side before being fed into a model for training or inference.

## Syntax

The basic syntax of `.hstack()` in PyTorch is as falows:

```pseudo
torch.hstack(tensors) -> Tensor
```

Where:
- `tensors`: A sequence of tensors with the same number of rows. All tensors must have the same number of dimensions and the same size in all dimensions except for the dimension corresponding to the horizontal stacking.

- `tensors`: A sequence of tensors with the same number of rows.
All tensors must have the same number of dimensions and the same
size in all dimensions except for the dimension corresponding to
the horizontal stacking.
- The function returns a new tensor containing the horizontal concatenation of the
input tensors.
The function returns a new tensor containing the horizontal concatenation of the input tensors.

## Exemple
## Example

Here's an example demonstrating how `.hstack()` can be used to concatenate tensors:

```python
```py
import torch

# Create two tensors
a = torch.tensor([[1, 2],[3, 4]])
b = torch.tensor([[5, 6],[7, 8]])

# Horizontal stack
# Stack the tensors horizontally
c = torch.hstack((a, b))

print(c)

```

Output:
The above code produces the following output:

```pseudo
```shell
tensor([[1, 2, 5, 6],
[3, 4, 7, 8]])
[3, 4, 7, 8]])
```

This example demonstrates concatenating two 2x2 tensors horizontally resulting in
2x4 tensor.
This example demonstrates concatenating two 2x2 tensors horizontally resulting in 2x4 tensor.

0 comments on commit 3e9ccdc

Please sign in to comment.