PyTorch .vstack()
Published Jan 30, 2025
Contribute to Docs
In PyTorch, the .vstack() function stacks a sequence of tensors along the first axis (dim=0), concatenating them row-wise. This function is part of the torch module.
Syntax
torch.vstack(tens, *, out=None)
tens: The sequence of tensors to be stacked.out(Optional): The output tensor.
Example
The following example demonstrates the usage of the .vstack() function:
import torch# Define tensorsten1 = torch.tensor([11, 22, 33])ten2 = torch.tensor([44, 55, 66])# Stack the tensors verticallyres = torch.vstack((ten1, ten2))# Print the resultprint(res)
The above code produces the following output:
tensor([[11, 22, 33],[44, 55, 66]])
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
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
- Intermediate.3 hours