Skip to content

    Loss Functions

    AI · Intermediate

    Definition

    A loss function measures how wrong predictions are; training minimizes it.

    The choice of loss defines what the model optimizes.

    Diagram

    MSE (regression)Cross-entropy (clas…

    Flowchart

    PredictCompare to targetCompute lossBackpropagate

    Example: MSE & cross-entropy

    import math
    y, p = [1, 0], [0.9, 0.2]
    mse = sum((a - b) ** 2 for a, b in zip(y, p)) / 2
    ce = -math.log(0.9)
    print(round(mse, 3), round(ce, 3))

    Output: 0.025 0.105

    Key points

    • Cross-entropy pairs with softmax/sigmoid
    • MAE is robust to outliers

    Interview questions

    Why not MSE for classification?

    Cross-entropy gives stronger gradients for confident wrong predictions.

    More AI topics