.vstack()

Anonymous contributor's avatar
Anonymous contributor
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 tensors
ten1 = torch.tensor([11, 22, 33])
ten2 = torch.tensor([44, 55, 66])
# Stack the tensors vertically
res = torch.vstack((ten1, ten2))
# Print the result
print(res)

The above code produces the following output:

tensor([[11, 22, 33],
[44, 55, 66]])

All contributors

Contribute to Docs

Learn PyTorch on Codecademy