We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Classifiers from Scratch step 4 of 5
Train Binary Classifier End-to-End
Implement train_binary_classifier to train a linear model for two classes. Use the supplied initial weights and take n_epochs full-batch gradient steps. Return the final weights, not the predicted probabilities or class labels.
Inputs and output
- x: (N, d), with one example per row and one feature per column.
- y: (N,), labels equal to 0.0 or 1.0.
- w0: (d,), the initial weights.
- lr: a finite, nonnegative learning rate.
- n_epochs: a nonnegative integer, the number of updates to perform.
N and d are positive. Tensor values are finite, and x, y and w0 share a floating-point dtype and device. Return a vector of shape (d,), preserving dtype and device. Leave all inputs unchanged.
There is no separate bias parameter. If x includes a column of ones, its weight acts as a bias. Do not add a bias or initialize new random weights.
Model and loss
For each example, the model computes a score z and a probability p of class 1:
z = x @ w
p = sigmoid(z) = 1 / (1 + exp(-z))
Use mean binary cross-entropy:
loss = -sum(y[i]*log(p[i]) + (1-y[i])*log(1-p[i]) for i in range(N)) / N
Its weight gradient simplifies to:
grad = x.T @ (p - y) / N
Each example contributes its features multiplied by its prediction error p[i]-y[i]. Average these contributions over the N examples, not the d features. This formula already includes the derivative of sigmoid; do not multiply by p*(1-p) again.
You only need the gradient to update the weights. You do not need to evaluate the logarithmic loss expression, which can encounter log(0) when probabilities round to zero or one. A framework sigmoid helper is allowed.
Training loop
Start from w0. At each step, compute probabilities using the current weights, compute the gradient above, then set w = w - lr * grad. Use all examples for every step. Carry updated weights into the next step, without shuffling, mini-batches, regularization or early stopping.
Perform exactly n_epochs updates. A fixed step count does not guarantee convergence or perfect classification. If lr=0, n_epochs=0, or every feature is zero, the returned weights have the same values as w0.
Worked example
For x=[[1.0]], y=[1.0], w0=[0.0], lr=0.1 and n_epochs=1:
z = 0
p = 0.5
grad = 1 * (0.5 - 1) = -0.5
w = 0 - 0.1 * (-0.5) = 0.05
Return [0.05]. On a second step, recompute the probability using 0.05 rather than reusing the initial probability.
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Predict the probability of one of two outcomes
A linear binary classifier first computes a score, called a logit:
$$z=w^Tx+b.$$
The weights w combine the input features x. The bias b shifts the score.
The sigmoid function converts it to the probability of class 1:
$$p=\frac{1}{1+e^{-z}}.$$
A logit of zero gives probability 0.5. Positive logits give probabilities above
0.5; negative logits give probabilities below it. Predicting class 1 when
p >= 0.5 is equivalent to checking z >= 0. Without a bias, the boundary
w^T x = 0 passes through the origin.
Train with probabilities, not thresholded predictions
For the true label y, which is either zero or one, binary cross-entropy is
$$\ell=-y\log p-(1-y)\log(1-p).$$
If y = 1, only -log(p) remains. If y = 0, only -log(1-p) remains. In both
cases, assigning a higher probability to the true class reduces the loss.
A numerically stable form works directly with the logit:
$$\ell=\max(z,0)-yz+\log(1+e^{-|z|}).$$
This avoids taking logs of probabilities that have rounded to zero or one. The
derivative with respect to z is p - y. For mean loss, average this error to
get the bias gradient, and average its product with each feature to get that
feature's weight gradient.
At zero weights and bias, all probabilities are 0.5 and mean loss is log(2).
Lower training loss means the model assigns better probabilities to the training
labels. It does not guarantee better results on new data, and a linear boundary
cannot fit every pattern.
Apply the decision threshold when making predictions. Replacing probabilities with hard class labels before computing the training loss discards information about how confident the model is.
Update parameters in a direction that reduces the loss
Gradient descent updates model parameters theta using the loss gradient:
$$\theta_{t+1}=\theta_t-\eta\nabla L(\theta_t).$$
The gradient describes how the loss changes near the current parameters. Its
negative points in the direction of steepest local decrease when distance is
measured in the usual Euclidean way. The learning rate eta sets the step
size. A step that is too large can increase the loss, even when its direction is
correct.
For L(w) = (w - 3)^2 at w = 0, the gradient is -6:
- With learning rate
0.1, the new weight is0 - 0.1 * (-6) = 0.6. The loss falls from9to5.76. - With learning rate
1, the new weight is6. The step passes the minimum at3, and the loss is still9.
How quickly the gradient changes, and how the input features are scaled, affect which learning rates work well.
Compute all gradients before changing the parameters
For an ordinary gradient-descent step, compute every gradient from the same parameter values. Updating one weight before computing another weight's gradient changes the algorithm.
When recording a loss history, state whether each value comes before or after an update. Including the initial loss gives one more entry than the number of updates.
Keep batch weighting consistent
A mini-batch gradient estimates the gradient over the full training set. Different batches can give different gradients, so a noisy loss curve is not by itself a bug.
Gradient accumulation can reproduce a larger batch's gradient when the losses and model behavior match. If smaller batches have different sizes, weight their mean gradients by their example counts. Simply averaging those means gives the smaller batches too much influence. Layers such as batch normalization can also behave differently when a batch is split.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
JAX reference solution
Sign in to track your attempts toward unlocking the reference solution.