Cost and Loss Functions Explained With a Forecast Model
A loss function is what training minimizes, and it is only a stand-in: the cost you actually care about can have a different best answer. Goodfellow, Bengio, and Courville define the vocabulary in Section 4.3 of Deep Learning: "The function we want to minimize or maximize is called the objective function, or criterion. When we are minimizing it, we may also call it the cost function, loss function, or error function. In this book, we use these terms interchangeably, though some machine learning publications assign special meaning to some of these terms." So the names are not a fixed rule, and other sources draw lines between them.
Section 5.2 shows a common objective with a second term. "To perform linear regression with weight decay, we minimize a sum J(w) comprising both the mean squared error on the training and a criterion that expresses a preference for the weights to have smaller squared L2 norm," and "J(w) = MSEtrain + λ wᵀw," where "λ is a value chosen ahead of time that controls the strength of our preference for smaller weights." More generally, "we can regularize a model that learns a function f(x; θ) by adding a penalty called a regularizer to the cost function."
Try it by hand. First the objective. Take one weight w with no intercept, three invented points x = 1, 2, 3 and y = 2, 3, 7, and J(w) = (average squared error) + λw². Setting the derivative to zero gives w = Σxy / (Σx² + nλ) = 29 / (14 + 3λ). With λ = 0 that is 29/14 ≈ 2.07, with λ = 1 it is 29/17 ≈ 1.71, and J at the minimum rises from 0.64 to 4.18 because the penalty now counts. The penalty weight λ is chosen outside this weight-fitting step, often using validation data. Next the gap between the loss and the cost that matters. A shop forecasts demand and has seen 10, 20, 30, 40, and 100 units. Squared error is minimized by the average, 40. Suppose each unit of unmet demand costs 5 and each unit of unsold stock costs 1. Forecasting 40 leaves 60 units of shortfall on the last day and 10 + 20 + 30 + 0 = 60 units of excess in total, so the cost is 5 × 60 + 1 × 60 = 360. Forecasting 100 leaves no shortfall and 90 + 80 + 70 + 60 = 300 units of excess, so the cost is 300. Searching every whole-number forecast from 0 to 120 confirms that 100 has the lowest cost. The squared-error loss at 100 is 4,600 against 1,000 at 40, so the model that minimizes the training loss is not the one that minimizes the cost. I computed these in exact arithmetic, and the data and prices are invented.
The trap: assuming that minimizing the training objective automatically minimizes every cost that matters. The training loss is a mathematical choice that makes optimization workable. The cost in the world has its own shape, here lopsided, and the two can pick different answers. A caution of my own, not from the sections I read: this example fixes one tiny dataset and one cost table, so it shows that the answers can differ, not by how much in practice, and the names cost, loss, and objective mean different things in different books.
Prof Rod's rule: write down the cost you actually pay, check what your training loss prefers, and when they disagree change the loss, the decision rule, or both.