Lesson 3 of 15

Loss Functions

Measuring Error

A loss function (also called a cost function or objective) measures how wrong our network's predictions are. Training reduces this number. The choice of loss function depends on the task.

Mean Squared Error (Regression)

For regression, we want predictions close to continuous target values:

LMSE=1ni=1n(y^iyi)2\mathcal{L}_{\text{MSE}} = \frac{1}{n} \sum_{i=1}^{n} (\hat{y}_i - y_i)^2

MSE penalizes large errors quadratically — a prediction off by 2 incurs 4× the loss of one off by 1.

Binary Cross-Entropy (Classification)

For binary classification where y^i(0,1)\hat{y}_i \in (0,1) is a probability:

LBCE=1ni=1n[yilogy^i+(1yi)log(1y^i)]\mathcal{L}_{\text{BCE}} = -\frac{1}{n} \sum_{i=1}^{n} \left[ y_i \log \hat{y}_i + (1 - y_i) \log(1 - \hat{y}_i) \right]

This is derived from maximum likelihood. When yi=1y_i = 1, only logy^i\log \hat{y}_i matters — we want y^i1\hat{y}_i \to 1. When yi=0y_i = 0, only log(1y^i)\log(1 - \hat{y}_i) matters — we want y^i0\hat{y}_i \to 0.

The ε=1015\varepsilon = 10^{-15} clip prevents log(0)=\log(0) = -\infty.

Your Task

Implement:

  • mse(predictions, targets) — mean squared error
  • binary_cross_entropy(predictions, targets) — binary cross-entropy with ε=1015\varepsilon = 10^{-15}
Python runtime loading...
Loading...
Click "Run" to execute your code.