Skip to content

mod_loss_mse

Lines: 61 | Visibility: private

Implementation of the mean squared error (MSE) loss/cost function.

The Mean Squared Error (MSE), also known as L2 loss, computes the average of the squared differences between predicted and reference values:

\[ \text{MSE} = \frac{1}{n} \sum_{i=1}^{n} 0.5 (y_i - \hat{y}_i)^2 \]

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

Properties: - Sensitive to outliers (quadratic penalty) - Smooth and differentiable everywhere - Commonly used for regression problems - Well-suited for gradient-based optimizers

Use cases: - Standard regression problems - When large errors should be penalized more heavily - Training with gradient-based optimization methods - When smooth gradients are desired

author: Alea Tokita, Alexander Knoll

Dependencies

Uses

module~~mod_loss_mse~~UsesGraph module~mod_loss_mse mod_loss_mse module~mod_loss_abstract mod_loss_abstract module~mod_loss_mse->module~mod_loss_abstract module~mod_precision mod_precision module~mod_loss_mse->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_mse~~UsedByGraph module~mod_loss_mse mod_loss_mse module~mod_adam mod_adam module~mod_adam->module~mod_loss_mse module~mod_lossfunctions mod_lossfunctions module~mod_adam->module~mod_lossfunctions module~mod_gradient_descent mod_gradient_descent module~mod_gradient_descent->module~mod_loss_mse module~mod_gradient_descent->module~mod_lossfunctions module~mod_lossfunctions->module~mod_loss_mse module~mod_nada_opt mod_nada_opt module~mod_nada_opt->module~mod_loss_mse module~mod_nada_opt->module~mod_lossfunctions module~mod_stochastic_gradient_descent mod_stochastic_gradient_descent module~mod_stochastic_gradient_descent->module~mod_loss_mse module~mod_stochastic_gradient_descent->module~mod_lossfunctions module~mod_abstract_kalman_filter mod_abstract_kalman_filter module~mod_abstract_kalman_filter->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_optimizers->module~mod_adam module~mod_optimizers->module~mod_gradient_descent module~mod_optimizers->module~mod_nada_opt module~mod_optimizers->module~mod_stochastic_gradient_descent module~mod_kalman_filter mod_kalman_filter module~mod_optimizers->module~mod_kalman_filter module~mod_kalman_filter_std mod_kalman_filter_std module~mod_optimizers->module~mod_kalman_filter_std 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_kalman_filter->module~mod_abstract_kalman_filter module~mod_kalman_filter_std->module~mod_abstract_kalman_filter 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

  • MSE_IDX : integer

Derived Types Summary

Name Description
LossMSE Mean squared error loss function (L2 loss).

Procedures Summary

Name Type Description
constructor_mse Function Construct a new MSE loss function object. Creates and initializes a new Mean Squared Error loss...
loss_mse Function Calculate the sum of squared errors for a batch. Computes the sum of squared differences...
collect_mse Function Normalize the sum of squared errors to get the final MSE value. Converts the sum of squared...
first_derivative_loss_mse Function Calculate the gradient of MSE with respect to predictions. Computes the derivative of the MSE...

Derived Types Details

type LossMSE

Mean squared error loss function (L2 loss).

Components

Type-Bound Procedures

Name Attributes Binding
value public ⇒ loss_value
value_child public ⇒ loss_mse
first_derivative public ⇒ first_derivative_loss_mse
collect_child public ⇒ collect_mse

Inherits

type~~lossmse~~InheritsGraph type~lossmse LossMSE type~abstractlossfunction AbstractLossFunction type~lossmse->type~abstractlossfunction

Interface Details

interface LossMSE


Procedure Details

function constructor_mse

function constructor_mse( &
    collect &
)

Construct a new MSE loss function object.

Creates and initializes a new Mean Squared 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 MSE loss function

Arguments

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

Called By

proc~~constructor_mse~~CalledByGraph proc~constructor_mse mod_loss_mse::constructor_mse interface~lossmse mod_loss_mse::LossMSE interface~lossmse->proc~constructor_mse

function loss_mse

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

Calculate the sum of squared errors for a batch.

Computes the sum of squared differences between predictions and reference values: ∑0.5*(pred_i - ref_i)². The factor of 0.5 is included to simplify the derivative calculation.

self - The MSE 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 squared errors (unnormalized)

Arguments

  • self : class(LossMSE) [in]– The MSE 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_mse

function collect_mse( &
    global_sum_loss, &
    global_batchsize &
)

Normalize the sum of squared errors to get the final MSE value.

Converts the sum of squared errors into the final MSE 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 squared errors across all processes global_batchsize - Total number of samples across all processes Returns: Final normalized MSE value

Arguments

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

function first_derivative_loss_mse

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

Calculate the gradient of MSE with respect to predictions.

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

The derivative is linear in the prediction error, normalized by the batch size. This provides smooth gradients suitable for gradient-based optimization algorithms.

self - The MSE 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(LossMSE) [in]– The MSE 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