Recipe

PyTorch primer

A fast on-ramp to PyTorch for engineers shipping production ML on Meridian. We cover tensors, autograd, and a minimal training loop you can lift into your own pipeline today.

Tensors are the foundation

Every PyTorch program revolves around the torch.Tensor. Tensors are n-dimensional arrays with GPU acceleration, device placement, and a strided memory layout. Move data between CPU and CUDA with a single .to(device) call.

Autograd does the calculus

Set requires_grad=True and PyTorch tracks every operation in a dynamic graph. Call .backward() on a scalar loss and gradients flow back to every leaf tensor automatically.

A minimal training loop

Five lines is all you need. Forward pass, loss, zero grads, backward, step.

import torch
model = torch.nn.Linear(10, 1).cuda()
opt = torch.optim.Adam(model.parameters(), lr=1e-3)
for x, y in loader:
    pred = model(x.cuda())
    loss = ((pred - y.cuda()) ** 2).mean()
    opt.zero_grad(); loss.backward(); opt.step()