Pytorch Torch Unbind
# PyTorch torch.unbind Function
* * Pytorch torch Reference Manual](#)
`torch.unbind` is a function in PyTorch used to split a tensor along a specified dimension into a tuple.
### Function Definition
torch.unbind(input, dim=0)
* * *
## Usage Example
## Example
import torch
x = torch.arange(12).reshape(3,4)
print("Original Tensor:")
print(x)
result = torch.unbind(x, dim=0)
print("Split along dim=0:")
for i, t in enumerate(result):
print(f" Chunk {i}: {t}")
result = torch.unbind(x, dim=1)
print("Split along dim=1:")
for i, t in enumerate(result):
print(f" Chunk {i}: {t}")
Output:
Original Tensor: tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])Split along dim=0: Chunk 0: tensor([0, 1, 2, 3]) Chunk 1: tensor([4, 5, 6, 7]) Chunk 2: tensor([ 8, 9, 10, 11])Split along dim=1: Chunk 0: tensor([0, 4, 8]) Chunk 1: tensor([1, 5, 9]) Chunk 2: tensor([ 2, 6, 10]) Chunk 3: tensor([ 3, 7, 11])
* * Pytorch torch Reference Manual](#)
YouTip