Skip to main content
Classification loss functions measure the difference between predicted class probabilities and true class labels.

Binary cross-entropy

Computes the binary cross-entropy loss for binary classification tasks.
template<Arithmetic T>
T binary_cross_entropy(const std::vector<T>& y_true, const std::vector<T>& y_pred)
y_true
const std::vector<T>&
Vector of true binary labels (0 or 1)
y_pred
const std::vector<T>&
Vector of predicted probabilities (values between 0 and 1)
return
T
The binary cross-entropy loss averaged over all samples

Template constraints

  • T must satisfy the Arithmetic concept (any arithmetic type)

Exceptions

Throws std::invalid_argument if y_true and y_pred have different sizes.

Implementation details

Predicted probabilities are clamped to the range [1e-12, 1 - 1e-12] to prevent log(0) errors.

Example

#include "Losses/loss_functions.hpp"
#include <vector>
#include <iostream>

int main() {
    std::vector<double> y_true = {1.0, 0.0, 1.0, 1.0};
    std::vector<double> y_pred = {0.9, 0.1, 0.8, 0.7};
    
    double loss = mlpp::losses::binary_cross_entropy(y_true, y_pred);
    std::cout << "Binary cross-entropy: " << loss << std::endl;
    
    return 0;
}

Multiclass cross-entropy

Computes the categorical cross-entropy loss for multiclass classification tasks.
template<Arithmetic T>
T multiclass_cross_entropy(const std::vector<std::vector<T>>& y_true,
                          const std::vector<std::vector<T>>& y_pred)
y_true
const std::vector<std::vector<T>>&
2D vector of true labels in one-hot encoded format. Outer dimension represents samples, inner dimension represents classes.
y_pred
const std::vector<std::vector<T>>&
2D vector of predicted class probabilities. Outer dimension represents samples, inner dimension represents class probabilities.
return
T
The categorical cross-entropy loss averaged over all samples

Template constraints

  • T must satisfy the Arithmetic concept (any arithmetic type)

Exceptions

Throws std::invalid_argument if:
  • Outer dimensions of y_true and y_pred differ
  • Inner dimensions of any sample in y_true and y_pred differ

Implementation details

Predicted probabilities are clamped to the range [1e-12, 1 - 1e-12] to prevent log(0) errors.

Example

#include "Losses/loss_functions.hpp"
#include <vector>
#include <iostream>

int main() {
    // 3 samples, 4 classes
    std::vector<std::vector<double>> y_true = {
        {1.0, 0.0, 0.0, 0.0},  // Class 0
        {0.0, 1.0, 0.0, 0.0},  // Class 1
        {0.0, 0.0, 0.0, 1.0}   // Class 3
    };
    
    std::vector<std::vector<double>> y_pred = {
        {0.8, 0.1, 0.05, 0.05},
        {0.1, 0.7, 0.15, 0.05},
        {0.05, 0.05, 0.1, 0.8}
    };
    
    double loss = mlpp::losses::multiclass_cross_entropy(y_true, y_pred);
    std::cout << "Multiclass cross-entropy: " << loss << std::endl;
    
    return 0;
}

Hinge loss

Computes the hinge loss for binary classification, commonly used with Support Vector Machines (SVM).
template<Arithmetic T>
T hinge_loss(const std::vector<T>& y_true, const std::vector<T>& y_pred)
y_true
const std::vector<T>&
Vector of true binary labels (-1 or +1)
y_pred
const std::vector<T>&
Vector of predicted decision values (raw scores, not probabilities)
return
T
The hinge loss averaged over all samples

Template constraints

  • T must satisfy the Arithmetic concept (any arithmetic type)

Exceptions

Throws std::invalid_argument if y_true and y_pred have different sizes.

Example

#include "Losses/loss_functions.hpp"
#include <vector>
#include <iostream>

int main() {
    std::vector<double> y_true = {1.0, -1.0, 1.0, -1.0};
    std::vector<double> y_pred = {0.8, -0.9, 0.3, -0.7};
    
    double loss = mlpp::losses::hinge_loss(y_true, y_pred);
    std::cout << "Hinge loss: " << loss << std::endl;
    
    return 0;
}

Squared hinge loss

Computes the squared hinge loss, which penalizes misclassifications more heavily than standard hinge loss.
template<Arithmetic T>
T squared_hinge_loss(const std::vector<T>& y_true, const std::vector<T>& y_pred)
y_true
const std::vector<T>&
Vector of true binary labels (-1 or +1)
y_pred
const std::vector<T>&
Vector of predicted decision values (raw scores, not probabilities)
return
T
The squared hinge loss averaged over all samples

Template constraints

  • T must satisfy the Arithmetic concept (any arithmetic type)

Exceptions

Throws std::invalid_argument if y_true and y_pred have different sizes.

Example

#include "Losses/loss_functions.hpp"
#include <vector>
#include <iostream>

int main() {
    std::vector<double> y_true = {1.0, -1.0, 1.0, -1.0};
    std::vector<double> y_pred = {0.8, -0.9, 0.3, -0.7};
    
    double loss = mlpp::losses::squared_hinge_loss(y_true, y_pred);
    std::cout << "Squared hinge loss: " << loss << std::endl;
    
    return 0;
}

Build docs developers (and LLMs) love