Pytorch Torch Column_Stack
# PyTorch torch.column_stack Function
* * Pytorch torch Reference Manual](#)
`torch.column_stack` is a function in PyTorch used to stack tensors by columns.
### Function Definition
torch.column_stack(tensors, *, out=None)
* * *
## Usage Examples
## Example
import torch
# 1D Tensor Column Stack
x1 = torch.tensor([1,2,3])
x2 = torch.tensor([4,5,6])
result = torch.column_stack([x1, x2])
print("1D Tensor Column Stack:")
print(f" x1: {x1}")
print(f" x2: {x2}")
print(f" column_stack:n{result}")
# 2D Tensor Column Stack
y1 = torch.tensor([,,])
y2 = torch.tensor([,,])
result = torch.column_stack([y1, y2])
print("n2D Tensor Column Stack:")
print(f" y1:n{y1}")
print(f" y2:n{y2}")
print(f" column_stack:n{result}")
# Multiple Tensors Column Stack
z1 = torch.tensor([1,2,3])
z2 = torch.tensor([4,5,6])
z3 = torch.tensor([7,8,9])
result = torch.column_stack([z1, z2, z3])
print("nMultiple 1D Tensor Column Stack:")
print(f" result:n{result}")
The output result is:
1D tensor column stacking: x1: tensor([1, 2, 3]) x2: tensor([4, 5, 6]) column_stack: tensor([[1, 4], [2, 5], [3, 6]])2D tensor column stacking: y1: tensor([, , ]) y2: tensor([, , ]) column_stack: tensor([[1, 4], [2, 5], [3, 6]])Multiple 1D tensors column stacking: result: tensor([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
* * Pytorch torch Reference Manual](#)
YouTip