YouTip LogoYouTip

Pytorch Torch Concatenate

# Concatenate multiple tensors result = torch.concatenate([a, b, c]) print(result)

Output result:

tensor([1, 2, 3, 4, 5, 6])

Example

import torch

# Create two 2D tensors

 a = torch.randn(2,3)

 b = torch.randn(2,3)

# Concatenate along the first dimension

 c = torch.concatenate([a, b], dim=0)

print("a shape of:", a.shape)

print("b shape of:", b.shape)

print("c shape of:", c.shape)

Output result:

a shape of: torch.Size([2, 3]) b shape of: torch.Size([2, 3]) c shape of: torch.Size([4, 3])

Note: torch.concatenate is an alias for torch.cat, they have exactly the same functionality. In actual code, torch.cat is more commonly used.


Image 4: Pytorch torch Reference Manual Pytorch torch Reference Manual

← Pytorch Torch CopysignPytorch Torch Complex β†’