A tensor is NumPy’s array with two superpowers: it runs on the GPU and tracks gradients. Create tensors, do tensor math, and move computation to the GPU.
Why: PyTorch’s tensor is like a NumPy array but can live on a GPU and record operations for automatic differentiation — the two things deep learning needs. When: everything in PyTorch is a tensor: data, weights, gradients. Where: install the build matching your hardware (CPU or a CUDA GPU) from pytorch.org.
pip install torch # CPU build; see pytorch.org for CUDA/GPU buildsWhy: tensor creation and math mirror NumPy almost exactly, so your array skills carry over — shape and dtype matter just as much. When: build tensors from data, or with zeros/ones/randn for weights and tests. Where: most shape bugs are here; print .shape constantly.
import torch
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]]) # from data
print(x.shape, x.dtype) # torch.Size([2, 2]) float32
w = torch.randn(2, 2) # random normal — like an initial weight matrix
print(x @ w) # matrix multiply
print(x.sum(), x.mean()) # reductions, like NumPy
print(x.T) # transposeWhy: the GPU is what makes deep learning fast, and you opt in by moving tensors (and models) onto a device — both operands of an operation must be on the same device. When: pick the device once at the top of a script and send everything there. Where: code written this way runs on CPU or GPU unchanged.
import torch
device = "cuda" if torch.cuda.is_available() else "cpu"
print("using", device)
x = torch.randn(1000, 1000, device=device) # created directly on the device
y = torch.randn(1000, 1000).to(device) # or moved with .to(device)
z = x @ y # runs on the GPU if available
print(z.device)Why: you constantly move between NumPy (data prep) and tensors (training), so knowing the conversions — and that a detached CPU tensor shares memory with its NumPy array — avoids surprises. When: convert data to tensors going in, and predictions back to NumPy coming out. Where: call .detach().cpu().numpy() to safely take a tensor off the graph and the GPU.
import torch, numpy as np
arr = np.array([1.0, 2.0, 3.0])
t = torch.from_numpy(arr) # NumPy -> tensor (shares memory)
pred = torch.tensor([0.1, 0.9, 0.4])
out = pred.detach().cpu().numpy() # tensor -> NumPy (off graph, off GPU)
print(out)