Pytorch Torch Hstack
# PyTorch torch.hstack Function
* * Pytorch torch Reference Manual](#)
`torch.hstack` is a function in PyTorch used to stack tensors horizontally (along columns).
### Function Definition
torch.hstack(tensors, *, out=None)
* * *
## Usage Examples
## Example
import torch
# Horizontal Stacking of 1D Tensors
x1 = torch.tensor([1,2,3])
x2 = torch.tensor([4,5,6])
result = torch.hstack([x1, x2])
print("1D Tensor Stacking:")
print(f" x1: {x1}")
print(f" x2: {x2}")
print(f" hstack: {result}")
# Horizontal Stacking of 2D Tensors
y1 = torch.tensor([[1,2],[3,4]])
y2 = torch.tensor([[5,6],[7,8]])
result = torch.hstack([y1, y2])
print("nHorizontal Stacking of 2D Tensors:")
print(f" y1:n{y1}")
print(f" y2:n{y2}")
print(f" hstack:n{result}")
# Stacking Multiple Tensors
z1 = torch.tensor([1,2])
z2 = torch.tensor([3,4])
z3 = torch.tensor([5,6])
result = torch.hstack([z1, z2, z3])
print("nStacking Multiple 1D Tensors:")
print(f" result: {result}")
The output result is:
1D tensor stacking: x1: tensor([1, 2, 3]) x2: tensor([4, 5, 6]) hstack: tensor([1, 2, 3, 4, 5, 6])2D tensor horizontal stacking: y1: tensor([[1, 2], [3, 4]]) y2: tensor([[5, 6], [7, 8]]) hstack: tensor([[1, 2, 5, 6], [3, 4, 7, 8]])Stacking multiple 1D tensors: result: tensor([1, 2, 3, 4, 5, 6])
* * Pytorch torch Reference Manual](#)
AI is thinking...
[](#)(#)
[PyTorch torch.nn Reference Manual](#)[](#)
YouTip