"""Synthetic scalar gradient-descent lesson. No model download or external calls."""
import json
from pathlib import Path

points = [(-2.0, -3.0), (-1.0, -1.0), (0.0, 1.0),
          (1.0, 3.0), (2.0, 5.0)]
w, b = 0.0, 0.0
learning_rate = 0.1

for _ in range(100):
    errors = [(w * x + b) - y for x, y in points]
    dw = sum(2 * error * x for error, (x, _) in zip(errors, points)) / len(points)
    db = sum(2 * error for error in errors) / len(points)
    w -= learning_rate * dw
    b -= learning_rate * db

checkpoint = {"architecture": "scalar-linear-v1", "w": w, "b": b}
path = Path("toy-weights.json")
with path.open("x", encoding="utf-8") as handle:
    json.dump(checkpoint, handle)

loaded = json.loads(path.read_text(encoding="utf-8"))
assert loaded["architecture"] == "scalar-linear-v1"
before = (loaded["w"], loaded["b"])
prediction = loaded["w"] * 3.0 + loaded["b"]
assert (loaded["w"], loaded["b"]) == before
print(f"w={w:.6f}, b={b:.6f}, prediction(3)={prediction:.6f}")
