.dstack()

Anonymous contributor's avatar
Anonymous contributor
Published Dec 6, 2024
Contribute to Docs

In PyTorch, the .dstack() function stacks a sequence of tensors depthwise, i.e., along the third axis (axis=2), creating a new tensor.

Syntax

torch.dstack(tensors, *, out=None)
  • tensors: A sequence (e.g., list or tuple) of tensors to be stacked depthwise along the third axis.
  • out (Optional): A tensor where the output will be stored, if provided.

Example

The following example demonstrates the usage of the .dstack() function:

import torch
# Create tensors
ten1 = torch.tensor([11, 22, 33])
ten2 = torch.tensor([12, 23, 34])
# Stack the tensors depthwise
res = torch.dstack((ten1, ten2))
print(res)

The above code produces the following output:

tensor([[[11, 12],
[22, 23],
[33, 34]]])

All contributors

Contribute to Docs

Learn PyTorch on Codecademy