-
Scalar (0D tensor):
x = torch.tensor(42) # Shape: []
-
Vector (1D tensor):
x = torch.tensor([1, 2, 3]) # Shape: [3]
-
Matrix (2D tensor):
x = torch.tensor([[1, 2], [3, 4], [5, 6]]) # Shape: [3, 2]
-
3D tensor:
x = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Shape: [2, 2, 2]
-
Reshape/View:
x = torch.randn(2, 3) y = x.view(6) # Shape of y: [6]
-
Squeeze (remove dimensions of size 1):
x = torch.randn(1, 3, 1) y = x.squeeze() # Shape of y: [3]
-
Unsqueeze (add a dimension of size 1):
x = torch.randn(3) y = x.unsqueeze(0) # Shape of y: [1, 3]
-
Stack:
x = torch.tensor([1, 2]) y = torch.tensor([3, 4]) z = torch.stack((x, y)) # Shape of z: [2, 2]
-
Concatenate:
x = torch.tensor([[1, 2]]) y = torch.tensor([[3, 4]]) z = torch.cat((x, y), dim=0) # Shape of z: [2, 2]
- Sum along a dimension:
x = torch.tensor([[1, 2, 3], [4, 5, 6]]) y = torch.sum(x, dim=0) # Shape of y: [3]
- Expand dimensions:
x = torch.randn(3) y = x[:, None] # Adds a new dimension # Shape of y: [3, 1]