"""Talent to Cash: original scalar-gradient example. Python 3 standard library.
Run: python gradients.py
This checks one toy parameter update; it does not train a language model.
"""


def step(x=2.0, y=5.0, w=1.0, b=0.0, rate=0.01):
    error = w * x + b - y
    dw, db = 2 * error * x, 2 * error
    next_w, next_b = w - rate * dw, b - rate * db
    return error**2, dw, db, next_w, next_b, (next_w * x + next_b - y) ** 2


if __name__ == "__main__":
    loss, dw, db, w, b, next_loss = step()
    assert loss == 9 and dw == -12 and db == -6
    assert abs(next_loss - 7.29) < 1e-10
    print(f"loss={loss}; dw={dw}; db={db}; w={w}; b={b}; next_loss={next_loss}")
    print("Try rate=0.3, compare the loss, and explain why a larger step can overshoot.")
