AI Computer Institute
Expert-curated CS & AI curriculum aligned to CBSE standards. A bharath.ai initiative. About Us

Loss Functions: Teaching Neural Networks What to Learn

📚 Neural Network Fundamentals⏱️ 25 min read🎓 Grade 10
✍️ AI Computer Institute Editorial Team Updated: August 2026 CBSE-aligned · Peer-reviewed · 25 min read
Content curated by subject matter experts with IIT/NIT backgrounds. All chapters are fact-checked against official CBSE/NCERT syllabi.

Suppose you have built a small neural network that predicts how late the Mumbai–Pune Duronto Express will run, in minutes, using that day's congestion data. The network outputs a number: 12 minutes. The train actually arrives 19 minutes late. The network was wrong. But "wrong" is not yet useful information — a weight inside the network cannot act on the word "wrong." It needs a number: a precise, computable measure of exactly how wrong, one that gets smaller as the network's guesses improve and that can be traced backward, weight by weight, to say "you, specifically, should change by this much." That number is the loss, and the function that produces it is a loss function. Everything a neural network appears to "learn" is, mechanically, nothing more than repeatedly asking one question — "which direction shrinks the loss?" — and moving a small step that way. If you understand loss functions precisely, you understand what training actually is. Everything else (backpropagation, optimizers, learning rates) is machinery built to answer that one question efficiently.

This chapter builds the loss function from scratch: why a naive definition of "error" fails, how Mean Squared Error is constructed and differentiated by hand, how that derivative becomes the gradient descent update rule, and why classification problems need an entirely different loss — cross-entropy — built from a different piece of mathematics.

Why "Just Measure the Error" Doesn't Work

The most obvious way to measure how wrong a prediction is: subtract. error = actual − predicted. Do this for every example in your training set, and average the errors to get one number summarizing how the whole network is doing.

Try it on two trains. Train A: predicted delay 15 minutes, actual delay 25 minutes, so error = 25 − 15 = +10. Train B: predicted delay 30 minutes, actual delay 20 minutes, so error = 20 − 30 = −10. Average error = (10 + (−10)) / 2 = 0. By this measure, the network looks flawless — zero average error — despite being off by a full 10 minutes on every single train. Positive and negative errors cancelled each other out. A loss function that can read as "0" while being wrong every time is worse than useless; it actively hides mistakes.

Two fixes remove the sign, and they lead to the two workhorse loss functions of regression:

Mean Absolute Error (MAE) takes the absolute value of each error before averaging: MAE = (1/n) Σ |actual − predicted|. For the two trains: MAE = (|10| + |−10|) / 2 = 10 minutes — an honest number.

Mean Squared Error (MSE) squares each error before averaging instead: MSE = (1/n) Σ (actual − predicted)². For the two trains: MSE = (10² + (−10)²) / 2 = (100 + 100)/2 = 100. Squaring also destroys the sign (a squared number is never negative), but it does something MAE doesn't: it changes units. The original errors were in minutes; MSE is in minutes². To bring the number back to an interpretable scale, you take its square root — the Root Mean Squared Error, RMSE = √MSE = √100 = 10 minutes. Here RMSE happens to equal MAE exactly, because both errors had identical magnitude (10). That equality is not a coincidence you can rely on in general: because squaring inflates larger errors disproportionately, RMSE is mathematically guaranteed to be greater than or equal to MAE whenever the individual error magnitudes differ, and strictly greater when they do. You'll verify this yourself in the practice problems below.

Squaring has a second consequence beyond removing the sign: it punishes big mistakes far more than small ones. An error of 2 contributes 4 to the sum; an error of 20 contributes 400 — one hundred times as much, not ten. This makes MSE sensitive to outliers (one badly-predicted example can dominate the whole loss), while MAE treats every unit of error as equally costly. Neither property is universally "better" — it depends on whether occasional large misses are catastrophic (favor MSE, which will fight hard to eliminate them) or merely undesirable (favor MAE, which won't over-correct for one bad case at the expense of everything else). But there is a second, more decisive reason MSE dominates in practice, and it has nothing to do with outliers — it's about what gradient descent needs.

Deriving the Gradient: How the Loss Tells a Weight What To Do

A loss function is only useful for training if you can compute, for every weight in the network, how a tiny nudge to that weight changes the loss. That sensitivity — the derivative of loss with respect to a weight — is exactly what gradient descent uses to decide which way to move.

Strip a neural network down to its simplest possible case: one input, one weight, no bias. The model predicts predicted = w·x. Suppose your training data links a route's congestion score x (on a 0–10 scale) to that day's actual delay y in minutes:

  • x₁ = 2, y₁ = 5
  • x₂ = 3, y₂ = 7
  • x₃ = 4, y₃ = 8

The Mean Squared Error as a function of the single weight w is L(w) = (1/n) Σ (yᵢ − w·xᵢ)². We want dL/dw — how L changes as w changes — derived properly, not asserted.

Write eᵢ(w) = yᵢ − w·xᵢ for the error on example i. Each term of the loss is eᵢ². If w changes by a tiny amount dw, eᵢ changes by deᵢ = −xᵢ·dw (because eᵢ is linear in w with slope −xᵢ). Now expand what happens to the squared term: (eᵢ + deᵢ)² − eᵢ² = 2eᵢ·deᵢ + deᵢ². When deᵢ is small, the deᵢ² term is negligible compared to the 2eᵢ·deᵢ term (it's the square of an already-small quantity), so the change in eᵢ² is approximately 2eᵢ·deᵢ. Substituting deᵢ = −xᵢ·dw gives d(eᵢ²) = −2xᵢ·eᵢ·dw, so d(eᵢ²)/dw = −2xᵢeᵢ. Averaging over all n examples:

dL/dw = −(2/n) Σ xᵢ(yᵢ − w·xᵢ)

This is the general reason MSE beats MAE for gradient descent: this derivative exists and is smooth everywhere. MAE's derivative is d|e|/dw, and |e| has a sharp corner at e = 0 — its slope is +1 just above zero and −1 just below, with no defined slope exactly at zero. Near a good prediction (small error), MAE's gradient still has magnitude 1, so gradient descent keeps taking full-sized steps and can oscillate around the minimum instead of settling into it. MSE's gradient, by contrast, shrinks toward zero as the error shrinks toward zero (it's proportional to eᵢ), so the steps naturally get smaller as the network approaches a good answer. That is the real reason MSE is the default regression loss, not just "it's simpler to differentiate."

Now use the formula. Start with w = 1.5. Predictions: 1.5×2 = 3, 1.5×3 = 4.5, 1.5×4 = 6. Errors (y − predicted): 5−3 = 2, 7−4.5 = 2.5, 8−6 = 2. Loss: L(1.5) = (2² + 2.5² + 2²)/3 = (4 + 6.25 + 4)/3 = 14.25/3 = 4.75.

Gradient: dL/dw = −(2/3)[2×2 + 3×2.5 + 4×2] = −(2/3)[4 + 7.5 + 8] = −(2/3)(19.5) = −13.0.

Gradient descent's update rule is w_new = w − η·(dL/dw), where η (eta) is the learning rate, a small positive number you choose. With η = 0.01: w_new = 1.5 − 0.01×(−13.0) = 1.5 + 0.13 = 1.63. The negative gradient pushed w upward — correctly, since every prediction was too low, so raising w raises every prediction toward the target.

Verify the loss actually dropped. At w = 1.63: predictions 3.26, 4.89, 6.52; errors 1.74, 2.11, 1.48; L(1.63) = (1.74² + 2.11² + 1.48²)/3 = (3.0276 + 4.4521 + 2.1904)/3 = 9.6701/3 ≈ 3.223. One step of gradient descent took the loss from 4.75 down to about 3.223 — a real, verifiable decrease, not an assertion.

Trace the same computation as code:

def predict(w, x):
    return w * x

def mse_loss(w, X, Y):
    n = len(X)
    total = 0
    for x, y in zip(X, Y):
        error = y - predict(w, x)
        total += error ** 2
    return total / n

def gradient(w, X, Y):
    n = len(X)
    total = 0
    for x, y in zip(X, Y):
        total += x * (y - predict(w, x))
    return -(2 / n) * total

X = [2, 3, 4]
Y = [5, 7, 8]
w = 1.5

print(mse_loss(w, X, Y))   # 4.75
print(gradient(w, X, Y))   # -13.0

learning_rate = 0.01
w = w - learning_rate * gradient(w, X, Y)
print(w)                   # 1.63
print(mse_loss(w, X, Y))   # 3.223366666666667

Because this toy loss has only one weight, L(w) expands into a plain quadratic: L(w) = 46 − 42w + (29/3)w² (multiply out the three squared terms and average — you can verify L(1.5) = 46 − 63 + 21.75 = 4.75 matches exactly). A quadratic's minimum is found the same way you find it in the Class 12 Applications of Derivatives chapter: set the derivative to zero and solve. dL/dw = −42 + (58/3)w = 0 gives w = 126/58 ≈ 2.1724, at which L ≈ 0.379 — the true bottom of this bowl. For one weight, you could stop here and solve algebraically instead of taking gradient steps at all. Real networks can't: they have thousands to billions of weights, the loss surface is a shape in that many dimensions (not a 2-D bowl), and for any network with more than one layer the surface is generally not even convex — it can have many valleys, not one. There is no algebraic "set every derivative to zero and solve" available. Gradient descent — repeatedly stepping downhill using exactly the derivative you just derived — is what remains possible at that scale, which is why it, not direct solving, is the standard training method even though this one-weight example could technically be solved directly.

This is also the diagram below: the exact bowl L(w) = 46 − 42w + (29/3)w², with your starting point, your one gradient step, and the true minimum all marked using the numbers you just computed by hand.

Loss Landscape L(w): Gradient Descent in Action 0 5 10 1.0 1.5 2.0 2.5 3.0 weight w loss L(w) w≈2.17 Start: w=1.5, L=4.75 After 1 step: w=1.63, L≈3.22 Minimum: w≈2.17, L≈0.38

Cross-Entropy: When the Output Is a Probability, Not a Number

MSE assumes a prediction and a target are both plain numbers on the same scale, and that being off by twice as much is roughly twice as bad. Classification breaks both assumptions. Consider a network flagging UPI transactions as fraudulent. It doesn't output "fraud" or "not fraud" directly — it outputs a probability, a number strictly between 0 and 1, produced by a sigmoid activation on its final layer. The target is either 0 (legitimate) or 1 (fraud). What should the loss be if the true label is fraud (y = 1) and the network says p = 0.01 — 99% confident it is not fraud?

Under squared error, that costs (1 − 0.01)² ≈ 0.98 — a fairly mild-looking number for what is, in reality, a disastrously overconfident wrong answer that let real fraud through. Squared error simply doesn't have room to express "this was catastrophically confident and catastrophically wrong," because it's capped: the worst possible squared error for a probability output is (1−0)² = 1. Cross-entropy loss is built from a different idea entirely — how surprising an outcome is, given what you claimed to believe. If you assign probability p to something being true and it turns out true, your "surprise" is defined as −ln(p). Assign p close to 1 (you were confident and correct) and −ln(p) is close to 0 — barely any surprise. Assign p close to 0 (you were confident it wouldn't happen, and you were wrong) and −ln(p) rockets toward infinity, because ln(p) diverges to −∞ as p → 0. This is exactly the behavior a loss for confident wrong answers should have, and MSE cannot produce it.

For a single example with true label y ∈ {0, 1} and predicted probability p of the "positive" class, binary cross-entropy is defined as:

L = −[y·ln(p) + (1 − y)·ln(1 − p)]

When y = 1, the second term vanishes (multiplied by 1 − 1 = 0) and L = −ln(p) — exactly the surprise of the true class. When y = 0, the first term vanishes and L = −ln(1 − p) — the surprise of the "not-fraud" class, using 1 − p as its probability. The formula is a single expression that automatically picks out whichever class actually happened.

Compute it for three UPI transactions:

  • Transaction 1: actually fraud (y=1), network predicts p = 0.9. L = −ln(0.9) ≈ 0.105.
  • Transaction 2: actually legitimate (y=0), network predicts p = 0.2 (i.e., 80% confident it's legitimate). L = −ln(0.8) ≈ 0.223.
  • Transaction 3: actually fraud (y=1), network predicts p = 0.01 — confidently wrong. L = −ln(0.01) ≈ 4.605.

Average loss = (0.105 + 0.223 + 4.605)/3 ≈ 1.645 nats (natural-log-based loss units are conventionally called "nats," parallel to how log-base-2 units are called "bits"). Notice how transaction 3 alone contributes 4.605 out of the total 4.933 — over 93% of the entire batch's loss came from one confidently wrong prediction. That is cross-entropy doing exactly what it's designed to do: it doesn't just count transaction 3 as "one mistake" the way accuracy would; it screams about it, producing a gradient large enough to force the weights that caused that overconfidence to correct hard. For more than two classes — say, classifying a handwritten Devanagari digit into one of ten categories — the same idea extends to categorical cross-entropy, applied to a softmax output layer that turns raw scores into a full probability distribution over all classes, with the loss again equal to −ln(probability assigned to the correct class).

Matching the Loss to the Problem

  • Regression (predicting a continuous quantity — delay in minutes, price in rupees, temperature): use MSE when large errors deserve outsized punishment and smooth, shrinking gradients near the optimum matter; use MAE when your data has occasional extreme outliers you don't want dominating training.
  • Binary classification (one of two outcomes, output as a probability — fraud/legitimate, spam/not-spam): use binary cross-entropy, never MSE — MSE paired with a sigmoid output produces very small, easily-vanishing gradients exactly when the network is most confidently wrong, which is precisely when you need the strongest correction.
  • Multi-class classification (one of several exclusive outcomes — digit recognition, language identification): use categorical cross-entropy over a softmax output.

Two Things a Loss Function Is Not

Misconception 1: "Loss and accuracy measure the same thing, so tracking either one is enough." They measure genuinely different things. Accuracy is discrete — a prediction is simply counted as right or wrong — and is not differentiable: changing a weight by a tiny amount almost never flips any prediction from wrong to right, so its gradient is zero almost everywhere and useless for training. Loss is continuous and differentiable by design, which is why it — not accuracy — is what gradient descent optimizes. But this also means two models can have identical accuracy and very different loss. Take two models classifying the same fraudulent transaction, both correctly flagging it as fraud: Model A outputs p = 0.51 (barely over the decision threshold), Model B outputs p = 0.98 (confidently correct). Both count identically toward accuracy — one correct prediction each. Their cross-entropy losses are −ln(0.51) ≈ 0.673 and −ln(0.98) ≈ 0.020 respectively — Model A's loss is more than 30 times larger. Loss captures how confidently and calibratedly correct a model is; accuracy only captures whether the final label happened to match. A model can improve substantially (moving from p=0.51 to p=0.9 on many examples, cutting loss sharply) while its accuracy number doesn't move at all.

Misconception 2: "If training loss keeps falling, the model is getting better." Training loss only measures fit to the examples the network has already seen. Given enough capacity (enough weights) relative to the size of the training set, a network can drive training loss toward zero by essentially memorizing the training examples — including their noise and quirks — rather than learning the underlying pattern. This is overfitting, and it is detected by tracking loss on a separate validation set the network never trains on. When training loss keeps dropping while validation loss starts climbing, the network has stopped learning and started memorizing. The number that tells you whether a model is actually improving is validation loss, not training loss in isolation.

Where This Fits in Your Exams

Loss functions and gradient descent sit squarely inside the CBSE Artificial Intelligence curriculum's neural networks and deep learning unit at the senior secondary level, and they are core, directly examinable material if you go on to attempt GATE's Data Science and Artificial Intelligence paper. Neither IIT-JEE nor BITSAT tests neural networks directly — they fall outside the core PCM syllabus — but the mathematical technique you used above to find the minimum of L(w), setting a derivative to zero and solving, is precisely the Application of Derivatives (maxima–minima) method examined in JEE Main and Advanced calculus. The insight worth carrying forward is that the same derivative-based reasoning underlies both: JEE asks you to solve dy/dx = 0 for a function with one variable; a neural network asks the same question for a loss function of thousands or millions of weights simultaneously, and answers it iteratively — via gradient descent — because an exact algebraic solution is out of reach at that scale.

Summary

A loss function converts a network's wrongness into a single differentiable number that gradient descent can act on. Naive signed error fails because positive and negative mistakes cancel; MAE and MSE fix this by removing the sign, with MSE preferred for gradient-based training because its derivative — dL/dw = −(2/n)Σxᵢ(yᵢ − w·xᵢ), derived above from first principles — shrinks smoothly to zero near the optimum, while MAE's derivative stays constant in magnitude even at the minimum. Gradient descent repeatedly applies w ← w − η(dL/dw), moving weights downhill along the loss surface; for a single weight this surface is a solvable quadratic bowl, but for real multi-layer networks it is a high-dimensional, often non-convex surface where only iterative stepping is tractable. Classification problems need a different loss entirely: cross-entropy, L = −[y·ln(p) + (1−y)·ln(1−p)], built from the information-theoretic idea of "surprise," which — unlike MSE — produces very large loss and very large gradients precisely when a model is confidently wrong, and negligible loss when confidently right. Loss is not accuracy (it captures confidence and calibration that accuracy discards) and a falling training loss alone does not prove a model is improving (check validation loss to rule out overfitting).

Four Problems to Test Your Understanding

  1. A network predicts prices (in lakhs of rupees) for 3 houses. Actual prices: 50, 65, 80. Predicted: 55, 60, 90. Compute MAE, MSE, and RMSE. Which is larger, RMSE or MAE, and why does that match the general rule stated above about when they can differ?
  2. Using L(w) = 46 − 42w + (29/3)w² from the worked delay example, compute L(2) directly and state whether the loss increased or decreased compared to L(1.63) ≈ 3.223. Explain why, in terms of distance from the true minimum at w ≈ 2.172.
  3. A fraud-detection model assigns p = 0.6 to a transaction that actually is fraud. Compute its binary cross-entropy loss. Now compute the loss if the model had instead predicted p = 0.95 for the same transaction. What does the difference tell you about how cross-entropy rewards increasing confidence in the correct direction?
  4. Explain, using the idea of the derivative of |e| at e = 0, why MAE is a poor choice as the sole loss for gradient-descent training even though it is more robust to outliers than MSE.
  5. A classmate says, "My training loss dropped to 0.001, so my model is basically perfect." What single question should you ask before agreeing, and what result from that question would tell you something is wrong?

Answers: (1) errors are −5, +5, −10; MAE = 20/3 ≈ 6.67 lakh; MSE = 150/3 = 50 lakh²; RMSE = √50 ≈ 7.07 lakh — RMSE > MAE here because the error magnitudes (5, 5, 10) are not all equal, matching the rule that equality only holds when every |error| is identical. (2) L(2) = 46 − 84 + 38.667 ≈ 0.667, which is lower than L(1.63) ≈ 3.223 — the loss decreased because w = 2 is closer to the true minimum at w ≈ 2.172 than w = 1.63 was. (3) −ln(0.6) ≈ 0.511 nats versus −ln(0.95) ≈ 0.051 nats — moving confidence from 0.6 to 0.95 in the correct direction cut the loss to roughly one-tenth, showing cross-entropy keeps rewarding increased confidence even after a prediction is already technically "correct." (4) d|e|/dw has magnitude 1 on both sides of e = 0 and is exactly at e = 0, so gradient descent using MAE never receives a signal to take smaller steps as it nears the optimum, causing it to overshoot or oscillate around the minimum instead of settling into it, unlike MSE whose gradient shrinks proportionally to the error itself. (5) Ask what the validation (or test) loss is — if it is much higher than 0.001, the model has memorized the training data (overfitting) rather than learned a generalizable pattern.

Think About It

Think about this: How would you explain loss functions: teaching neural networks what to learn to a friend who has never seen a computer? What real-world analogy would you use? Imagine you had to build a system using these concepts — what would be your first step? Try this: before moving on, write down three things you learned and one question you still have.

← Backpropagation: The Algorithm That Powers Deep LearningRegularization: Preventing Overfitting in Neural Networks →

Found this useful? Share it!

📱 WhatsApp 🐦 Twitter 💼 LinkedIn