Concepts 🚀 Project 🧮 Labs
🧮 Labs / ML Advanced / Gradient Descent — the engine behind neural networks

Gradient Descent — the engine behind neural networks

Most ML models (neural nets, logistic regression, SVMs) use gradient descent, not closed-form solutions. Understand the algorithm.

script.py v3.1
Python output click Run to execute
Click "Run" to execute the code on the left.
The output will appear here.
🧠 How it works

Gradient Descent = the engine of modern ML. ALGORITHM: (1) init β random, (2) compute gradient ∇f (uphill direction), (3) step β -= lr * ∇f, (4) repeat. LEARNING RATE: too small = slow. Too large = diverge. Just right = fast. Start with 0.001, tune by 3x (0.001 → 0.003 → 0.01). VARIANTS: Batch GD — all data per step. Accurate, slow. SGD — 1 sample per step. Noisy, fast. Mini-batch (32-256) — standard. Best of both. Adam — adaptive lr per param + momentum. Default for deep learning. WHY IT MATTERS: neural nets, logistic regression, SVMs, gradient boosting — ALL use GD. LinearRegression + RandomForest are exceptions (closed-form / greedy). Backpropagation (neural nets) = chain rule of calculus + GD. If you understand GD, you understand how most models LEARN.

✏️ Try these changes
  • Change learning_rate from 0.5 to 0.01
    → much slower convergence — needs 200+ steps
  • Change learning_rate from 1.1 to 2.0
    → diverges faster — bounces to infinity
  • Change the function from x² to x⁴ (gradient = 4x³)
    → steeper — needs smaller lr
  • Add momentum: v = 0.9*v + 0.1*gradient; x -= lr*v
    → smoother convergence — Adam-like