DAI developerLab· about 50 min· server sandbox
Lab: tensors, autograd and your first training loop
Compute gradients with autograd, compare with hand calculation, and train a linear model with a real loop.
Theory
requires_grad=True tracks the tensor, .backward() fills .grad, gradients accumulate — zero them every step. torch.no_grad() turns off tracking when you update weights by hand.
Sub-tasks
- gradient with autograd —
grad_of(f, x0)returns df/dx at x0 using autograd, where f is a Python function on a tensor. - gd_steps —
gd_steps(f, x0, lr, steps)does gradient descent on f with autograd and zeroes the gradient every step. - train_linear —
train_linear(X, y, lr, steps)trains w, b (MSE) and returns (w, b).
Passes when: mse <= 0.05
The starter code
runs in an isolated sandbox on the serverimport torch
def grad_of(f, x0):
# TODO: x = torch.tensor(float(x0), requires_grad=True); y = f(x); y.backward(); return x.grad.item()
...
def gd_steps(f, x0, lr=0.1, steps=100):
x = torch.tensor(float(x0), requires_grad=True)
for _ in range(steps):
# TODO: forward, backward, uppdatera under no_grad, nollställ grad
...
return x.item()
def train_linear(X, y, lr=0.05, steps=500):
w = torch.zeros(X.shape[1], requires_grad=True)
b = torch.zeros(1, requires_grad=True)
for _ in range(steps):
# TODO: pred = X @ w + b; mse; backward; steg; nollställ
...
return w.detach(), b.detach()
You write the code; tests you cannot see decide whether it holds up. Create a free account to run the lab.
Try the diagnosticCreate a free accountExpected results
grad_of(lambda x: x**2 + 2*x, 3.0) = 8; gd_steps on (x−3)² reaches ≈3; a linear model on y = 2x + 1 + noise gives MSE < 0.05.
Common mistakes
- Forgets
zero_()→ the gradient accumulates and the steps are wrong. - Updates weights without
torch.no_grad()→ autograd error. .item()missing when a number should be returned.