0
0
PyTorchml~5 mins

Tensor shapes and dimensions in PyTorch

Choose your learning style9 modes available
Introduction

Tensors are like containers for numbers. Knowing their shapes and dimensions helps us organize and use data correctly in machine learning.

When preparing images for a neural network, to make sure the data fits the model.
When checking if your data batches have the right size before training.
When debugging errors caused by mismatched input sizes.
When reshaping data to feed into different layers of a model.
When understanding output sizes after operations like convolution or pooling.
Syntax
PyTorch
tensor.shape
# or
tensor.size()

tensor.shape and tensor.size() both give the size of each dimension.

Dimensions are counted from 0, where 0 means a single number (scalar), 1 means a list (vector), 2 means a table (matrix), and so on.

Examples
A scalar tensor has no dimensions, so shape is an empty tuple ().
PyTorch
import torch
x = torch.tensor(5)
print(x.shape)
A 1D tensor (vector) with 3 elements has shape (3,).
PyTorch
x = torch.tensor([1, 2, 3])
print(x.shape)
A 2D tensor (matrix) with 2 rows and 2 columns has shape (2, 2).
PyTorch
x = torch.tensor([[1, 2], [3, 4]])
print(x.shape)
A 3D tensor with sizes 4, 3, and 2 in each dimension.
PyTorch
x = torch.randn(4, 3, 2)
print(x.shape)
Sample Model

This program creates tensors with different dimensions and prints their shapes to show how tensor sizes look in PyTorch.

PyTorch
import torch

# Create different tensors
scalar = torch.tensor(7)
vector = torch.tensor([1, 2, 3, 4])
matrix = torch.tensor([[1, 2], [3, 4], [5, 6]])
three_d = torch.randn(2, 3, 4)

# Print their shapes
print(f"Scalar shape: {scalar.shape}")
print(f"Vector shape: {vector.shape}")
print(f"Matrix shape: {matrix.shape}")
print(f"3D tensor shape: {three_d.shape}")
OutputSuccess
Important Notes

Remember that torch.Size is like a tuple showing the size of each dimension.

Changing tensor shapes without changing data is called reshaping, which is useful but must keep the total number of elements the same.

Summary

Tensors have shapes that tell how many elements are in each dimension.

Use tensor.shape or tensor.size() to check tensor dimensions.

Understanding shapes helps avoid errors and correctly prepare data for models.