Skip to main content
Regression loss functions measure the difference between predicted and actual continuous values.

Mean squared error

Computes the mean squared error between true and predicted values.
template<Arithmetic T>
T mse(const std::vector<T>& y_true, const std::vector<T>& y_pred)
y_true
const std::vector<T>&
Vector of true target values
y_pred
const std::vector<T>&
Vector of predicted values
return
T
The mean squared error 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, 2.0, 3.0, 4.0};
    std::vector<double> y_pred = {1.1, 2.2, 2.9, 4.3};
    
    double loss = mlpp::losses::mse(y_true, y_pred);
    std::cout << "MSE: " << loss << std::endl;
    
    return 0;
}

Mean absolute error

Computes the mean absolute error between true and predicted values.
template<Arithmetic T>
T mae(const std::vector<T>& y_true, const std::vector<T>& y_pred)
y_true
const std::vector<T>&
Vector of true target values
y_pred
const std::vector<T>&
Vector of predicted values
return
T
The mean absolute error 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<float> y_true = {1.0f, 2.0f, 3.0f, 4.0f};
    std::vector<float> y_pred = {1.1f, 2.2f, 2.9f, 4.3f};
    
    float loss = mlpp::losses::mae(y_true, y_pred);
    std::cout << "MAE: " << loss << std::endl;
    
    return 0;
}

Huber loss

Computes the Huber loss, which is less sensitive to outliers than MSE. It is quadratic for small errors and linear for large errors.
template<Arithmetic T>
T huber(const std::vector<T>& y_true, const std::vector<T>& y_pred, T delta = static_cast<T>(1))
y_true
const std::vector<T>&
Vector of true target values
y_pred
const std::vector<T>&
Vector of predicted values
delta
T
default:"1"
Threshold at which to change from quadratic to linear loss. Controls the boundary between small and large errors.
return
T
The Huber 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, 2.0, 3.0, 10.0};
    std::vector<double> y_pred = {1.1, 2.2, 2.9, 4.0};
    
    // Using default delta = 1.0
    double loss = mlpp::losses::huber(y_true, y_pred);
    std::cout << "Huber loss (delta=1.0): " << loss << std::endl;
    
    // Using custom delta
    double loss_custom = mlpp::losses::huber(y_true, y_pred, 2.0);
    std::cout << "Huber loss (delta=2.0): " << loss_custom << std::endl;
    
    return 0;
}

Build docs developers (and LLMs) love