# Why is the logistic regression cost function scaled by the number of examples?

I sometimes see that the cost function, along with the regularizer is divided by 1/2m where m is the number of examples. When we are trying to find the minimum of the cost, why does scaling by this amount matter? It doesn't affect where the minimum is. Example: • For scaling by 1/m, it could be for readability and interpretation i.e. a score that the user can easily understandable. However, for scaling by 1/2, it is to get rid of the 2 in the exponent when computing the derivative. For example, the derivative of x^2 is 2*x, and for (1/2)*x^2, the derivative would be x (cleaner). Apr 30 '13 at 8:59
• @soufanom why don't you make an answer :) ? Apr 30 '13 at 11:23

In general, there are two reasons to have a constant in loss function.

• The first reason is to have a simpler notation later. For example, you can have a loss function $f(y,\hat y)=\frac 1 2(y-\hat y)^2$, take the derivative respect to $y$, you will not have the annoying term $2$.

• The second reason is trying to "normalize" the loss value on number of data points. For example, (let us think about the regression now), suppose you have $10$ data points, and for each data point, you made $1.0$ error, e.g., the ground truth is $3.5$, and your prediction is $2.5$, etc.. Then, your loss for the entire data set is $10.0$. On the other hand, if you have $100$ data points. you still have $1.0$ error for each estimation. Then your loss for entire data is $100$. This does not make too much sense because two models are equal, but applied on different data. If you normalize the loss by number of data (divide the total loss by number of data), then the loss values in above examples will be identical.

Building further on the answer by @hxd1011:

Suppose we had a set model -- parameters and all fixed. We would look at the Mean Squared Error as our measure of error.

$MSE = \frac{1}{m}\sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)})^2$

When we regularize using some function of our parameters we add a parameter of the form $\lambda \sum_{j=1}^n f(\theta_j)$ in your case $f(x) = x^2$, but this is a more general concept.

If you don't scale that regularization by your sample size -- then for a given $\lambda$, as $m \rightarrow \infty$, the regularization becomes meaningless. By adding that scaling, we gain two things.

1. The scale of $\lambda$ becomes stable for different sample sizes. (i.e. you can get a sense of 'how much' you are regularizing) In some contexts (live model updating) this is crucial.

2. This is closely related to the mean loss on our predictions. While that may seem initially less useful than a concept like MSE, if we are regularizing, then it is a desirable feature.