Skip to content

mod_loss_mse_torch

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} (y_i - \hat{y}_i)^2 \]

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

This implementation differs from LossMSE only by a factor of 0.5. Even though that is mostly a choice of convenience to keep the expression for the derivative cleaner in text books, this can have a profound influence on training results. The implementation here is consistent with the MSE in Pytorch and RuNNer 1. It has shown significantly improved training results for 4G charge training.

See LossMSE for further details.

Dependencies

Uses

module~~mod_loss_mse_torch~~UsesGraph module~mod_loss_mse_torch mod_loss_mse_torch module~mod_loss_abstract mod_loss_abstract module~mod_loss_mse_torch->module~mod_loss_abstract module~mod_precision mod_precision module~mod_loss_mse_torch->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_torch~~UsedByGraph module~mod_loss_mse_torch mod_loss_mse_torch module~mod_lossfunctions mod_lossfunctions module~mod_lossfunctions->module~mod_loss_mse_torch module~mod_abstract_kalman_filter mod_abstract_kalman_filter module~mod_abstract_kalman_filter->module~mod_lossfunctions 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_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_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_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_optimizers->module~mod_kalman_filter module~mod_optimizers->module~mod_kalman_filter_std 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_TORCH_IDX : integer

Derived Types Summary

Name Description
LossMSETorch Mean squared error loss function (L2 loss) without a factor of 0.5

Procedures Summary

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

Derived Types Details

type LossMSETorch

Mean squared error loss function (L2 loss) without a factor of 0.5

Components

Type-Bound Procedures

Name Attributes Binding
value public ⇒ loss_value
value_child public ⇒ loss_mse_torch
first_derivative public ⇒ first_derivative_loss_mse_torch
collect_child public ⇒ collect_mse_torch

Inherits

type~~lossmsetorch~~InheritsGraph type~lossmsetorch LossMSETorch type~abstractlossfunction AbstractLossFunction type~lossmsetorch->type~abstractlossfunction

Interface Details

interface LossMSETorch


Procedure Details

function constructor_mse_torch

function constructor_mse_torch( &
    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_torch~~CalledByGraph proc~constructor_mse_torch mod_loss_mse_torch::constructor_mse_torch interface~lossmsetorch mod_loss_mse_torch::LossMSETorch interface~lossmsetorch->proc~constructor_mse_torch

function loss_mse_torch

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

Calculate the sum of squared errors for a batch.

Computes the sum of squared differences between predictions and reference values: ∑(pred_i - ref_i)².

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(LossMSETorch) [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_torch

function collect_mse_torch( &
    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_torch

function first_derivative_loss_mse_torch( &
    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} 2 * (\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(LossMSETorch) [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