Neural Network Cost Function in MATLAB
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The cost function is the number your training algorithm tries to minimize. In MATLAB, whether you use the Deep Learning Toolbox or write the math yourself, understanding the cost function is what lets you diagnose slow learning, unstable gradients, and mismatched output layers.
What a Cost Function Does
A neural network produces predictions from input data and current weights. The cost function measures how far those predictions are from the expected targets. Training then adjusts the weights to reduce that error.
For regression, a common choice is mean squared error, often abbreviated as MSE. For classification, cross-entropy is more common because it matches probability-based outputs better.
In practical terms:
- low cost means predictions are close to targets
- high cost means the model is still making large mistakes
The optimizer never updates weights blindly. It follows the gradient of the cost.
Mean Squared Error in MATLAB
For a regression model with predictions yPred and targets yTrue, MSE is:
mean((yPred - yTrue).^2)
Here is a small MATLAB example:
This computes the average squared difference across all samples. Squaring makes large errors more expensive, which is useful in many regression problems.
If you are implementing training manually, you would compute predictions from the current network, calculate the cost, then use backpropagation to obtain gradients for each weight matrix.
Cross-Entropy for Classification
If the network predicts class probabilities, MSE is often not the best fit. Cross-entropy penalizes confident wrong predictions more appropriately.
For binary classification:
J = -mean(y .* log(p) + (1 - y) .* log(1 - p))
In MATLAB:
The epsilon clamp prevents log(0), which would produce -Inf and break training.
A Small Manual Neural Network Example
To make the role of the cost function concrete, consider a one-hidden-layer network for regression. This example does not implement full training, but it shows how the forward pass leads to a scalar cost.
The important detail is that the network may produce many outputs, but the cost is reduced to a single scalar. That scalar is what gradient-based training uses to update every weight.
MATLAB Tooling vs Manual Math
If you use the Deep Learning Toolbox, MATLAB handles much of this for you. You typically define layers, choose training options, and let the framework use the correct loss for the network type. For example, a regression network often pairs naturally with MSE, while a classification network uses cross-entropy through the classification layer.
Manual implementations are still worth understanding because they explain why training behaves the way it does. If cost never decreases, the issue may be:
- the wrong output activation
- a target shape mismatch
- a learning rate that is too large
- a cost function that does not match the task
Knowing the formula lets you debug instead of guessing.
Cost Function and Regularization
In many real models, the total objective includes more than prediction error. You may add regularization so very large weights are penalized.
For L2 regularization:
Regularization can reduce overfitting, but it changes the optimization target. If you compare two experiments, make sure you know whether the reported cost includes the regularization term.
Choosing the Right Cost
A cost function is not just a mathematical formality. It encodes what counts as a good prediction.
- For regression, MSE is a reasonable default.
- For binary or multi-class classification, cross-entropy is usually the better choice.
- For imbalanced problems or special error tolerances, you may need weighted or custom losses.
The right answer depends on the task, output representation, and how you want mistakes to be penalized.
Common Pitfalls
- Using MSE for a probability-based classification problem when cross-entropy is the better match. This can slow learning and weaken gradients.
- Forgetting numerical protection around
log, which leads toInforNaNvalues during cross-entropy calculations. - Comparing cost values across experiments without checking whether regularization is included. That can make results look inconsistent.
- Returning a vector of per-sample errors when the training code expects one scalar objective. Backpropagation code usually assumes a scalar cost.
- Blaming the optimizer when the real issue is a target-shape or output-layer mismatch. A correct optimizer cannot rescue a broken loss definition.
Summary
- A cost function converts prediction quality into a scalar objective for training.
- MSE is common for regression, while cross-entropy is common for classification.
- In MATLAB, you can compute the loss directly with matrix operations or rely on toolbox layers.
- Regularization adds another term to the objective and changes what the model minimizes.
- Understanding the cost formula makes debugging training behavior much easier.

