Skip to content

Commit

Permalink
modified PyTorch
Browse files Browse the repository at this point in the history
  • Loading branch information
clonymontana committed Oct 20, 2024
1 parent 5c6f158 commit 0a0bae9
Showing 1 changed file with 7 additions and 25 deletions.
32 changes: 7 additions & 25 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: 'The `.hastack(tensors)` is a function used to concetante two or more tensors along horizontal axis.'
Description: 'Concatenates two or more tensors along the horizontal axis (column-wise)'
Subjects:
- 'AI'
- 'Data Science'
Expand All @@ -14,25 +14,24 @@ CatalogContent:
- 'paths/data-science'
---

In PyTorch, **`.hastack()`** (short for horizontal stack)is a function used to concatenate two or more tensors along the horizontal axis (axis1). This operation is useful for combining data with the same number of rows but differing in the number of columns. It acts similarly to numpy's 'np.hastack()' and is particulary handy when you're working with 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 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.

# Syntax

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

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

Where:

- `tensors` is a sequence of tesors with the same numbers of rows.
- The function returns a new tensor containing the horizontal concatenation of the imput tensors.

- `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.

# Exemple

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

```python
import torch
Expand All @@ -42,7 +41,7 @@ a = torch.tensor([[1, 2],[3, 4]])
b = torch.tensor([[5, 6],[7, 8]])

# Horizontal stack
c = toarch.hstack((a, b))
c = torch.hstack((a, b))

print(c)

Expand All @@ -56,22 +55,5 @@ tensor([[1, 2, 5, 6],
```
This example demonstrates concatenating two 2x2 tensors horizontally resulting in 2x4 tensor.

# Codebyte Exemple

```python
import torch

# Create 1D tensors
a = torch.tensor([1, 2])
b = torch.tensor([3, 4])

# Horizontal stack
c = torch.tensor((a, b))

print(c)
```
In this Codebyte example, two 1-dimensional tensors are horizontally concatenated, showcasing how `.hstack()`seamlessly combines tensors not just in 2D but also in 1D tensors, further emphasizing the function's utility in handling tensors of varios dimensions as long as thay share the same number of rows (or are all 1-dimensional).




0 comments on commit 0a0bae9

Please sign in to comment.