PyTorch tensors, devices, autograd, modules and dataloaders
standardintermediateA PyTorch tensor is like a NumPy array, but it can live on a GPU and track the operations performed on it for automatic differentiation. A device specifies where a tensor's data actually lives (CPU or GPU). Autograd is the system that builds the computational graph (§9.1) and computes gradients automatically. A `Module` defines a model's layers; a `Dataset` and `DataLoader` handle loading and batching training data.
Think of it as
These five pieces are the vocabulary every PyTorch program is written in. A tensor is the core data structure — everything (inputs, weights, gradients) is a tensor. A device is an explicit choice: a tensor and the model operating on it must be on the same device, or PyTorch raises an error rather than silently moving data, because a silent cross-device copy would be a hidden performance cost. Autograd is what makes `loss.backward()` (§9.2) work — it only tracks gradients for tensors with `requires_grad=True`, which is why inference code wraps itself in `torch.no_grad()` to skip that (unneeded, memory-costly) bookkeeping. A `Module` is the base class every model architecture is built from — it groups layers and defines the forward pass; PyTorch tracks all its parameters automatically once they're assigned as attributes. A `Dataset` defines how to fetch one example; a `DataLoader` wraps it to handle batching, shuffling, and parallel loading, which is what actually produces the batches a training loop iterates over.
- A grid diagram: a real (3,1) tensor [[10],[20],[30]] and a real (1,4) tensor [[1,2,3,4]], each shown stretched to a real (3,4) grid by repeating its values.
- The two real stretched grids are added cell by cell, producing the real (3,4) result [[11,12,13,14],[21,22,23,24],[31,32,33,34]] — numpy actually computed this.
Remember: Tensors are the core data structure, device-aware and gradient-trackable via autograd. A tensor and its model must share a device explicitly. `nn.Module` defines architecture and tracks parameters; `Dataset` + `DataLoader` handle fetching and batching training data.
See also: numpy arrays and vectorization · training and validation loops





