.unbind()
Anonymous contributor
Published Jan 25, 2025
Contribute to Docs
In PyTorch, the .unbind()
function removes the specified dimension and returns a tuple of tensors, each representing a slice along the given dimension.
Syntax
torch.unbind(tensor, dim=0)
tensor
: The tensor to unbind.dim
: The dimension to remove from the tensor. If not specified, the default value (0
) is used.
Example
The following example demonstrates the usage of the .unbind()
function:
import torch# Create a tensorten = torch.tensor([[1, 2, 3],[4, 5, 6],[7, 8, 9]])# Unbind the tensor along the first dimension (dim=0)unbound = torch.unbind(ten)# Print the resulting tupleprint(unbound)
The code above should output the following:
(tensor([1, 2, 3]), tensor([4, 5, 6]), tensor([7, 8, 9]))
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours