Skip to content

mod_loss_mae

Lines: 62 | Visibility: private

Implementation of the mean absolute error (MAE) loss/cost function.

The Mean Absolute Error (MAE), also known as L1 loss, computes the average of the absolute differences between predicted and reference values:

\[ \text{MAE} = \frac{1}{n} \sum_{i=1}^{n} |y_i - \hat{y}_i| \]

where \(y_i\) are the reference values and \(\hat{y}_i\) are the predictions.

Properties: - Robust to outliers compared to MSE - Linear penalty for errors - Non-differentiable at zero (handled using sign function)

Use cases: - When outliers should not dominate the loss - When all errors should be weighted equally - Training with robust optimization algorithms

author: Alea Tokita, Alexander Knoll

Dependencies

Uses

module~~mod_loss_mae~~UsesGraph module~mod_loss_mae mod_loss_mae module~mod_loss_abstract mod_loss_abstract module~mod_loss_mae->module~mod_loss_abstract module~mod_precision mod_precision module~mod_loss_mae->module~mod_precision module~mod_loss_abstract->module~mod_precision module~mod_mpi mod_mpi module~mod_loss_abstract->module~mod_mpi iso_c_binding iso_c_binding module~mod_precision->iso_c_binding iso_fortran_env iso_fortran_env module~mod_precision->iso_fortran_env module~mod_mpi->module~mod_precision mpi_f08 mpi_f08 module~mod_mpi->mpi_f08

Used By

module~~mod_loss_mae~~UsedByGraph module~mod_loss_mae mod_loss_mae module~mod_abstract_kalman_filter mod_abstract_kalman_filter module~mod_abstract_kalman_filter->module~mod_loss_mae module~mod_lossfunctions mod_lossfunctions module~mod_abstract_kalman_filter->module~mod_lossfunctions module~mod_lossfunctions->module~mod_loss_mae module~mod_adam mod_adam module~mod_adam->module~mod_lossfunctions module~mod_gradient_descent mod_gradient_descent module~mod_gradient_descent->module~mod_lossfunctions module~mod_initialize mod_initialize module~mod_initialize->module~mod_lossfunctions module~mod_optimizers mod_optimizers module~mod_initialize->module~mod_optimizers module~mod_kalman_filter mod_kalman_filter module~mod_kalman_filter->module~mod_abstract_kalman_filter module~mod_kalman_filter_std mod_kalman_filter_std module~mod_kalman_filter_std->module~mod_abstract_kalman_filter module~mod_nada_opt mod_nada_opt module~mod_nada_opt->module~mod_lossfunctions module~mod_stochastic_gradient_descent mod_stochastic_gradient_descent module~mod_stochastic_gradient_descent->module~mod_lossfunctions module~mod_train_mode mod_train_mode module~mod_train_mode->module~mod_lossfunctions module~mod_train_mode->module~mod_initialize module~mod_train_loop_4g_charges mod_train_loop_4g_charges module~mod_train_mode->module~mod_train_loop_4g_charges module~mod_train_loop_atomic_property mod_train_loop_atomic_property module~mod_train_mode->module~mod_train_loop_atomic_property module~mod_train_loop_short_range mod_train_loop_short_range module~mod_train_mode->module~mod_train_loop_short_range module~mod_write_outfiles_training mod_write_outfiles_training module~mod_train_mode->module~mod_write_outfiles_training program~runner RuNNer program~runner->module~mod_lossfunctions program~runner->module~mod_initialize program~runner->module~mod_train_mode module~mod_predict_mode mod_predict_mode program~runner->module~mod_predict_mode module~mod_optimizers->module~mod_adam module~mod_optimizers->module~mod_gradient_descent module~mod_optimizers->module~mod_kalman_filter module~mod_optimizers->module~mod_kalman_filter_std module~mod_optimizers->module~mod_nada_opt module~mod_optimizers->module~mod_stochastic_gradient_descent module~mod_predict_mode->module~mod_initialize module~mod_write_outfiles mod_write_outfiles module~mod_predict_mode->module~mod_write_outfiles module~mod_runner_interface_bindings mod_runner_interface_bindings module~mod_runner_interface_bindings->module~mod_initialize module~mod_train_energy_multiple_structures mod_train_energy_multiple_structures module~mod_train_energy_multiple_structures->module~mod_initialize module~mod_train_forces_multiple_structures mod_train_forces_multiple_structures module~mod_train_forces_multiple_structures->module~mod_initialize module~mod_train_forces_multiple_structures->module~mod_train_energy_multiple_structures module~mod_train_loop_4g_charges->module~mod_initialize module~mod_cost_calculator mod_cost_calculator module~mod_train_loop_4g_charges->module~mod_cost_calculator module~mod_output_files_and_cost_4g_charges mod_output_files_and_cost_4g_charges module~mod_train_loop_4g_charges->module~mod_output_files_and_cost_4g_charges module~mod_train_loop_atomic_property->module~mod_initialize module~mod_train_loop_atomic_property->module~mod_cost_calculator module~mod_output_files_and_cost mod_output_files_and_cost module~mod_train_loop_atomic_property->module~mod_output_files_and_cost module~mod_train_loop_short_range->module~mod_initialize module~mod_train_loop_short_range->module~mod_train_energy_multiple_structures module~mod_train_loop_short_range->module~mod_train_forces_multiple_structures module~mod_train_loop_short_range->module~mod_cost_calculator module~mod_train_loop_short_range->module~mod_output_files_and_cost module~mod_output_files_and_cost_4g_short_range mod_output_files_and_cost_4g_short_range module~mod_train_loop_short_range->module~mod_output_files_and_cost_4g_short_range module~mod_write_outfiles->module~mod_initialize module~mod_lammps_interface mod_lammps_interface module~mod_lammps_interface->module~mod_runner_interface_bindings module~mod_write_outfiles_training->module~mod_write_outfiles module~mod_cost_calculator->module~mod_write_outfiles_training module~mod_output_files_and_cost->module~mod_cost_calculator module~mod_output_files_and_cost_4g_charges->module~mod_cost_calculator module~mod_output_files_and_cost_4g_short_range->module~mod_cost_calculator

Module Variables

  • MAE_IDX : integer

Derived Types Summary

Name Description
LossMAE Mean absolute error loss function (L1 loss).

Procedures Summary

Name Type Description
constructor_mae Function Construct a new MAE loss function object. Creates and initializes a new Mean Absolute Error...
loss_mae Function Calculate the sum of absolute errors for a batch. Computes the sum of absolute differences...
collect_mae Function Normalize the sum of absolute errors to get the final MAE value. Converts the sum of absolute...
first_derivative_loss_mae Function Calculate the gradient of MAE with respect to predictions. Computes the derivative of the MAE...

Derived Types Details

type LossMAE

Mean absolute error loss function (L1 loss).

Components

Type-Bound Procedures

Name Attributes Binding
value public ⇒ loss_value
value_child public ⇒ loss_mae
first_derivative public ⇒ first_derivative_loss_mae
collect_child public ⇒ collect_mae

Inherits

type~~lossmae~~InheritsGraph type~lossmae LossMAE type~abstractlossfunction AbstractLossFunction type~lossmae->type~abstractlossfunction

Interface Details

interface LossMAE


Procedure Details

function constructor_mae

function constructor_mae( &
    collect &
)

Construct a new MAE loss function object.

Creates and initializes a new Mean Absolute Error loss function instance with the specified MPI collection behavior.

collect - Whether to collect and average loss across MPI processes Returns: Polymorphic pointer to the constructed MAE loss function

Arguments

  • collect : logical [in]– Whether to average across MPI process batches

Called By

proc~~constructor_mae~~CalledByGraph proc~constructor_mae mod_loss_mae::constructor_mae interface~lossmae mod_loss_mae::LossMAE interface~lossmae->proc~constructor_mae

function loss_mae

function loss_mae( &
    self, &
    batchsize, &
    prediction, &
    reference &
)

Calculate the sum of absolute errors for a batch.

Computes the sum of absolute differences between predictions and reference values: ∑|pred_i - ref_i|. This is the unnormalized form that will be normalized later by the collect function.

self - The MAE loss function instance batchsize - Number of samples in the batch prediction - Predicted values for the batch reference - Reference (target) values for the batch Returns: Sum of absolute errors (unnormalized)

Arguments

  • self : class(LossMAE) [in]– The MAE loss function instance
  • batchsize : integer [in]– Number of samples in batch
  • prediction : real(DP) (dim: (batchsize)) [in]– Predicted values
  • reference : real(DP) (dim: (batchsize)) [in]– Reference values

function collect_mae

function collect_mae( &
    global_sum_loss, &
    global_batchsize &
)

Normalize the sum of absolute errors to get the final MAE value.

Converts the sum of absolute errors into the final MAE by dividing by the total number of samples. This function handles the final normalization step for both local and MPI-collected computations.

global_sum_loss - Sum of absolute errors across all processes global_batchsize - Total number of samples across all processes Returns: Final normalized MAE value

Arguments

  • global_sum_loss : real(DP) [in]– Sum of absolute errors
  • global_batchsize : integer [in]– Total number of samples

function first_derivative_loss_mae

function first_derivative_loss_mae( &
    self, &
    batchsize, &
    prediction, &
    reference &
)

Calculate the gradient of MAE with respect to predictions.

Computes the derivative of the MAE loss function: [ \frac{\partial \text{MAE}}{\partial \hat{y}_i} = \frac{1}{n} \text{sign}(\hat{y}_i - y_i) ]

The derivative is the sign of the prediction error, normalized by the batch size. Note that the derivative is undefined at zero error, but the sign function handles this by returning a valid gradient.

self - The MAE loss function instance batchsize - Number of samples in the batch prediction - Predicted values for the batch reference - Reference (target) values for the batch Returns: Gradient vector with respect to each prediction

Arguments

  • self : class(LossMAE) [in]– The MAE loss function instance
  • batchsize : integer [in]– Number of samples in batch
  • prediction : real(DP) (dim: (batchsize)) [in]– Predicted values
  • reference : real(DP) (dim: (batchsize)) [in]– Reference values