Skip to main content

Command Palette

Search for a command to run...

ML from Scratch — Episode 3: Logistic Regression

Updated
1 min readView as Markdown
A
A learner building ML algorithms from scratch to actually understand what's happening under the hood. Writing about the math, the mistakes, and the process and not just the results.

What is Logistic Regression?

Logistic regression is a ML model that predicts an output in the range of 0 to 1 — interpreted as the probability of belonging to a class.

Hypothesis Function

We use the sigmoid function to keep predictions between 0 and 1:

$$\hat{y} = \sigma(z) = \frac{1}{1+e^{-z}}$$

where z = wx + b.

Why Binary Cross Entropy over MSE?

BCE gives a convex loss surface, whereas MSE with sigmoid gives a non-convex one — making it difficult for gradient descent to reach the global minimum.

$$L = -\frac{1}{m} \sum \left[ y \log(\hat{y}) + (1-y) \log(1-\hat{y}) \right]$$

Gradients

Applying the chain rule to the loss function in three steps:

$$\frac{\partial L}{\partial w} = \frac{\partial L}{\partial \hat{y}} \cdot \frac{\partial \hat{y}}{\partial z} \cdot \frac{\partial z}{\partial w}$$

The sigmoid and cross entropy cancel cleanly, giving:

$$\frac{\partial L}{\partial w} = \frac{1}{m}\sum(\hat{y} - y) \cdot x$$

$$\frac{\partial L}{\partial b} = \frac{1}{m}\sum(\hat{y} - y)$$

Weight Update

$$w = w - \alpha \cdot \frac{\partial L}{\partial w}$$

$$b = b - \alpha \cdot \frac{\partial L}{\partial b}$$

31 views