PyTorch Tensors: Creation, Operations, and GPU Transfer
Learners will create tensors from Python lists and NumPy arrays, perform element-wise and matrix operations, and move tensors to GPU with .to('cuda').
What Is a PyTorch Tensor?
A tensor is the fundamental data structure in PyTorch — essentially an n-dimensional array similar to a NumPy array but with built-in GPU support and automatic differentiation. Tensors can be scalars (0D), vectors (1D), matrices (2D), or higher-dimensional structures. PyTorch tensors track computation history, enabling automatic gradient computation for training neural networks.
import torch
# Scalar (0-dimensional tensor)
scalar = torch.tensor(3.14)
print(scalar.shape) # torch.Size([])
# Vector (1D)
vector = torch.tensor([1.0, 2.0, 3.0])
print(vector.shape) # torch.Size([3])
# Matrix (2D)
matrix = torch.tensor([[1, 2], [3, 4]])
print(matrix.shape) # torch.Size([2, 2])Creating Tensors: Common Methods
PyTorch provides many factory functions to create tensors with specific values or shapes. torch.zeros and torch.ones fill tensors with constants; torch.rand samples from a uniform distribution; torch.randn samples from a standard normal distribution. These are the building blocks for weight initialisation and synthetic data generation.
import torch
zeros = torch.zeros(3, 4) # 3x4 matrix of zeros
ones = torch.ones(2, 3) # 2x3 matrix of ones
rand_uniform = torch.rand(3, 3) # uniform in [0, 1)
rand_normal = torch.randn(3, 3) # standard normal N(0,1)
arange = torch.arange(0, 10, 2) # [0, 2, 4, 6, 8]
print(zeros.dtype) # torch.float32 (default)
print(arange) # tensor([0, 2, 4, 6, 8])All lessons in this course
- PyTorch Tensors: Creation, Operations, and GPU Transfer
- Autograd: Automatic Differentiation for Backpropagation
- Building a Feedforward Network with nn.Module
- Training Loop: Loss, Optimizer, and Epochs