21/31
Part 5 of 6 · A Linear layer, proved by gradcheckLearn and do

The practice trains it to the exact answer

The practice trains Linear(3, 1) on 64 random points of y=x⋅[1,−2,0.5]+0.3y = x \cdot [1, -2, 0.5] + 0.3 with plain SGD:

torch.manual_seed(0)
layer = Linear(3, 1)
x = torch.randn(64, 3)
y = x @ torch.tensor([[1.0], [-2.0], [0.5]]) + 0.3
opt = torch.optim.SGD(layer.parameters(), lr=0.1)
for step in range(1, 101):
    loss = ((layer(x) - y) ** 2).mean()
    if step in (1, 100):
        print(step, f"{loss.item():.3g}")
    opt.zero_grad()
    loss.backward()
    opt.step()
print(layer.weight.detach().flatten(), layer.bias.detach())
Predict

Train Linear(3, 1) with SGD for 100 steps on data made from weights [1, -2, 0.5] and bias 0.3, with no noise. Where do the weights end?

Pick one to see what it prints.
Pick an answer above to see the rest