AI Gradient Descent
Update a parameter in the direction that reduces a loss.
How it works
A gradient gives the local rate of change of a differentiable objective. Gradient descent subtracts a learning-rate-scaled gradient. The learning rate must suit the objective scale; a large step can diverge. This example fits one weight to an exact zero-intercept line with a bounded number of steps.
Run the code
Try in playground loads this complete standard-library Python example without executing it. Choose Run when ready. Local review uses Python 3.12.14; the existing browser editor uses Pyodide 0.27.7. These small fictional examples need no account, external data, model-provider key or database service.
Example
x, y = [1.,2.,3.], [2.,4.,6.]
weight = 0.0
losses = []
for _ in range(60):
residuals = [weight*a-b for a,b in zip(x,y,strict=True)]
losses.append(sum(r*r for r in residuals)/len(x))
gradient = 2*sum(r*a for r,a in zip(residuals,x,strict=True))/len(x)
weight -= 0.05*gradient
print(f"weight: {weight:.3f}")
print("loss decreased:", losses[-1] < losses[0])
Expected result
weight: 2.000 loss decreased: True
The derivative comes from mean squared error for this one-parameter model. The final loss is compared with the initial value rather than assuming training worked because a loop ran. The example omits a bias parameter deliberately and says so.