Key terms for this tutorial
Here are the words that matter most in this tutorial. Come back here if a term feels fuzzy.
- gradient
- A vector of partial derivatives showing how much each parameter affects the loss.
- gradient descent
- An optimization algorithm that updates weights by moving them in the direction that lowers the loss.
- learning rate
- How far the optimizer moves the weights each step. Too big and training explodes; too small and it crawls.
- loss
- A number that measures how wrong the model is. Training tries to minimize loss.
- model
- A function with learnable parameters that maps inputs to outputs.
- weight
- A learnable number inside a model that is adjusted during training.
The loss surface
Imagine a landscape where the height at any point is the model's error. Your goal is to find the lowest valley. The problem: you cannot see the whole landscape. You only know the slope under your feet.
That slope is the gradient: a vector of partial derivatives telling you which way is up. To go down, walk in the opposite direction.
Core idea: the gradient points uphill. Subtract a small multiple of it from the weights and the loss goes down.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Gradient Descent'. Explain the section 'The loss surface' in plain English. Include simple analogies, vocabulary definitions, and a short example.
Fitting a line from scratch
We will fit y = m*x + b to noisy data by hand. There are two weights: slope m and bias b. The loss is the average squared error.
import numpy as np
# True line: y = 2*x + 1, with noise
np.random.seed(0)
X = np.random.rand(100, 1) * 5
y = 2 * X + 1 + np.random.randn(100, 1) * 0.5
m, b = 0.0, 0.0
lr = 0.05
epochs = 200
for _ in range(epochs):
pred = m * X + b
error = pred - y
# Gradients of mean squared error
dm = (2 / len(X)) * np.sum(error * X)
db = (2 / len(X)) * np.sum(error)
m -= lr * dm
b -= lr * db
print(f"m = {m:.3f}, b = {b:.3f}")
m = 1.992, b = 1.046
Line-by-line: gradient_descent.py
Read this slowly. Each line does one small, concrete thing.
import numpy as np— Import thenumpylibrary so we can use its built-in tools.for _ in range(epochs):— Loop over the data one batch at a time.pred = m * X + b— Generate predictions by running inputs through the model.print(f"m = {m:.3f}, b = {b:.3f}")— Print a result so we can see what the model learned.
Pop-in: Read the code one block at a time
Industry code imports many libraries. Focus on the sequence: prepare data, define the model, run the training loop, and evaluate. The rest is setup.
The algorithm recovers the true slope and bias from noisy data. Notice there is no PyTorch yet — just derivatives and NumPy.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Gradient Descent'. Explain the section 'Fitting a line from scratch' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.
Learning rate and convergence
The learning rate controls step size. Too small and training crawls. Too large and you leap over valleys and never settle. Modern optimizers like Adam adjust per-weight step sizes automatically.
You can also get stuck in a local minimum, a valley that is lower than its surroundings but not the lowest point on the whole surface. For big models, local minima are less scary than saddle points, and smart optimizers handle both.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Gradient Descent'. Explain the section 'Learning rate and convergence' in plain English. Include simple analogies, vocabulary definitions, and a short example.
Check yourself
What happens if the learning rate is huge?
Imagine taking giant steps down a hill when you can only feel the slope at your feet.
The algorithm may overshoot the bottom and oscillate, or even diverge and blow up the loss. A huge learning rate is like running downhill blindly.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Gradient Descent'. The practice question 'What happens if the learning rate is huge?' asks: Imagine taking giant steps down a hill when you can only feel the slope at your feet. The answer is: The algorithm may overshoot the bottom and oscillate, or even diverge and blow up the loss. A huge learning rate is like running downhill blindly.. Explain the concept behind this in simple terms.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Gradient Descent'. Explain the section 'Check yourself' in plain English. Include simple analogies, vocabulary definitions, and a short example.
Three questions
1. What does the gradient point in?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Gradient Descent'. The quiz asks: What does the gradient point in? The correct answer is: The direction of steepest ascent. Explain why that answer is right and why the other choices are wrong, using a simple example.
2. In the line-fitting example, what are the 'weights'?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Gradient Descent'. The quiz asks: In the line-fitting example, what are the 'weights'? The correct answer is: The slope m and bias b. Explain why that answer is right and why the other choices are wrong, using a simple example.
3. Why do we use the average squared error instead of the total squared error?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Gradient Descent'. The quiz asks: Why do we use the average squared error instead of the total squared error? The correct answer is: It makes the numbers smaller and independent of dataset size. Explain why that answer is right and why the other choices are wrong, using a simple example.
Keep learning
Dive deeper into momentum, Adam, learning-rate schedules, and non-convex landscapes.
Part of the CodeHS tutorial library.