What is a Tensor?
A tensor is a multi-dimensional array. Itβs the fundamental data structure in PyTorch.
- 0D tensor: scalar
- 1D tensor: vector
- 2D tensor: matrix
- nD tensor: higher-dimensional array
Creating Tensors
import torch
# From data
x = torch.tensor([1, 2, 3])
# Zeros and ones
z = torch.zeros(3, 4)
o = torch.ones(2, 3)
# Random
r = torch.randn(3, 3) # normal distribution
# Like another tensor
y = torch.zeros_like(x)
Operations
# Element-wise
a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([4.0, 5.0, 6.0])
c = a + b # [5, 7, 9]
d = a * b # [4, 10, 18]
# Matrix multiplication
A = torch.randn(2, 3)
B = torch.randn(3, 4)
C = A @ B # shape: (2, 4)
# Reshaping
x = torch.randn(6)
y = x.view(2, 3)
z = x.reshape(3, 2)
GPU Acceleration
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
x = torch.randn(1000, 1000, device=device)
Moving between devices:
x_cpu = x.cpu()
x_gpu = x.to("cuda")
Autograd Integration
Tensors track operations for automatic differentiation:
x = torch.tensor(3.0, requires_grad=True)
y = x ** 2
y.backward()
print(x.grad) # 6.0