athena__loss Module

Module containing loss function implementations

This module implements loss functions that quantify the difference between model predictions and target values, guiding the optimisation process.

Implemented loss functions:

Mean Squared Error (MSE): L = (1/N) Σ (y_pred - y_true)² For regression, sensitive to outliers

Mean Absolute Error (MAE): L = (1/N) Σ |y_pred - y_true| For regression, robust to outliers

Binary Cross-Entropy: L = -(1/N) Σ [ylog(ŷ) + (1-y)log(1-ŷ)] For binary classification (outputs in [0,1])

Categorical Cross-Entropy: L = -(1/N) Σ_i Σ_c y_{i,c} * log(ŷ_{i,c}) For multi-class classification with one-hot encoded targets

Sparse Categorical Cross-Entropy: L = -(1/N) Σ log(ŷ_{i,c_i}) For multi-class with integer class labels

Huber Loss: L = (1/N) Σ { 0.5(y-ŷ)² if |y-ŷ| ≤ δ { δ(|y-ŷ| - 0.5*δ) otherwise Combines MSE and MAE, robust to outliers while smooth near zero

where N is number of samples, y is true value, ŷ is prediction


Uses


Interfaces

public interface bce_loss_type

Interface for binary cross entropy loss function

  • private module function setup_loss_bce() result(loss)

    Set up binary cross entropy loss function

    Arguments

    None

    Return Value type(bce_loss_type)

    Binary cross entropy loss function

public interface cce_loss_type

Interface for categorical cross entropy loss function

  • private module function setup_loss_cce() result(loss)

    Set up categorical cross entropy loss function

    Arguments

    None

    Return Value type(cce_loss_type)

    Categorical cross entropy loss function

public interface huber_loss_type

Interface for huber loss function

  • private module function setup_loss_huber() result(loss)

    Set up huber loss function

    Arguments

    None

    Return Value type(huber_loss_type)

    Huber loss function

public interface mae_loss_type

Interface for mean absolute error loss function

  • private module function setup_loss_mae() result(loss)

    Set up mean absolute error loss function

    Arguments

    None

    Return Value type(mae_loss_type)

    Mean absolute error loss function

public interface mse_loss_type

Interface for mean squared error loss function

  • private module function setup_loss_mse() result(loss)

    Set up mean squared error loss function

    Arguments

    None

    Return Value type(mse_loss_type)

    Mean squared error loss function

public interface nll_loss_type

Interface for negative log likelihood loss function

  • private module function setup_loss_nll() result(loss)

    Set up negative log likelihood loss function

    Arguments

    None

    Return Value type(nll_loss_type)

    Negative log likelihood loss function

interface

  • private module function compute_base(this, predicted, expected) result(output)

    Compute the loss of a model

    Arguments

    Type IntentOptional Attributes Name
    class(base_loss_type), intent(in), target :: this

    Instance of the physics-informed neural network loss function

    type(array_type), intent(inout), dimension(:,:), target :: predicted

    Predicted values

    type(array_type), intent(in), dimension(size(predicted,1),size(predicted,2)) :: expected

    Expected values

    Return Value type(array_type), pointer

    Physics-informed neural network loss


Derived Types

type, public, abstract ::  base_loss_type

Abstract type for loss functions

Components

Type Visibility Attributes Name Initial
integer, public :: batch_index = 1

Index of the batch to compute the loss for

real(kind=real32), public :: epsilon = 1.E-10_real32

Small value to prevent log(0)

character(len=:), public, allocatable :: name

Name of the loss function

integer, public :: sample_index = 1

Index of the sample to compute the loss for

Type-Bound Procedures

procedure(compute_base), public, deferred, pass(this) :: compute

Compute the loss of a model

type, public, extends(base_loss_type) ::  bce_loss_type

Binary cross entropy loss function

Components

Type Visibility Attributes Name Initial
integer, public :: batch_index = 1

Index of the batch to compute the loss for

real(kind=real32), public :: epsilon = 1.E-10_real32

Small value to prevent log(0)

character(len=:), public, allocatable :: name

Name of the loss function

integer, public :: sample_index = 1

Index of the sample to compute the loss for

Constructor

Interface for binary cross entropy loss function

private module function setup_loss_bce ()

Set up binary cross entropy loss function

Type-Bound Procedures

procedure, public :: compute => compute_bce

Compute the loss of a model

type, public, extends(base_loss_type) ::  cce_loss_type

Categorical cross entropy loss function

Components

Type Visibility Attributes Name Initial
integer, public :: batch_index = 1

Index of the batch to compute the loss for

real(kind=real32), public :: epsilon = 1.E-10_real32

Small value to prevent log(0)

character(len=:), public, allocatable :: name

Name of the loss function

integer, public :: sample_index = 1

Index of the sample to compute the loss for

Constructor

Interface for categorical cross entropy loss function

private module function setup_loss_cce ()

Set up categorical cross entropy loss function

Type-Bound Procedures

procedure, public :: compute => compute_cce

Compute the loss of a model

type, public, extends(base_loss_type) ::  huber_loss_type

Huber loss function

Components

Type Visibility Attributes Name Initial
integer, public :: batch_index = 1

Index of the batch to compute the loss for

real(kind=real32), public :: epsilon = 1.E-10_real32

Small value to prevent log(0)

real(kind=real32), public :: gamma = 1._real32

Gamma value for the huber loss function

character(len=:), public, allocatable :: name

Name of the loss function

integer, public :: sample_index = 1

Index of the sample to compute the loss for

Constructor

Interface for huber loss function

private module function setup_loss_huber ()

Set up huber loss function

Type-Bound Procedures

procedure, public :: compute => compute_huber

Compute the loss of a model

type, public, extends(base_loss_type) ::  mae_loss_type

Mean absolute error loss function

Components

Type Visibility Attributes Name Initial
integer, public :: batch_index = 1

Index of the batch to compute the loss for

real(kind=real32), public :: epsilon = 1.E-10_real32

Small value to prevent log(0)

character(len=:), public, allocatable :: name

Name of the loss function

integer, public :: sample_index = 1

Index of the sample to compute the loss for

Constructor

Interface for mean absolute error loss function

private module function setup_loss_mae ()

Set up mean absolute error loss function

Type-Bound Procedures

procedure, public :: compute => compute_mae

Compute the loss of a model

type, public, extends(base_loss_type) ::  mse_loss_type

Mean squared error loss function

Components

Type Visibility Attributes Name Initial
integer, public :: batch_index = 1

Index of the batch to compute the loss for

real(kind=real32), public :: epsilon = 1.E-10_real32

Small value to prevent log(0)

character(len=:), public, allocatable :: name

Name of the loss function

integer, public :: sample_index = 1

Index of the sample to compute the loss for

Constructor

Interface for mean squared error loss function

private module function setup_loss_mse ()

Set up mean squared error loss function

Type-Bound Procedures

procedure, public :: compute => compute_mse

Compute the loss of a model

type, public, extends(base_loss_type) ::  nll_loss_type

Negative log likelihood loss function

Components

Type Visibility Attributes Name Initial
integer, public :: batch_index = 1

Index of the batch to compute the loss for

real(kind=real32), public :: epsilon = 1.E-10_real32

Small value to prevent log(0)

character(len=:), public, allocatable :: name

Name of the loss function

integer, public :: sample_index = 1

Index of the sample to compute the loss for

Constructor

Interface for negative log likelihood loss function

private module function setup_loss_nll ()

Set up negative log likelihood loss function

Type-Bound Procedures

procedure, public :: compute => compute_nll

Compute the loss of a model


Functions

private module function compute_base(this, predicted, expected) result(output)

Placeholder for compute function in base_loss_type

Arguments

Type IntentOptional Attributes Name
class(base_loss_type), intent(in), target :: this

Instance of the base loss function

type(array_type), intent(inout), dimension(:,:), target :: predicted

Predicted values

type(array_type), intent(in), dimension(size(predicted,1),size(predicted,2)) :: expected

Expected values

Return Value type(array_type), pointer

Loss value

private function compute_bce(this, predicted, expected) result(output)

Compute the binary cross entropy loss of a model

Arguments

Type IntentOptional Attributes Name
class(bce_loss_type), intent(in), target :: this

Instance of the physics-informed neural network loss function

type(array_type), intent(inout), dimension(:,:), target :: predicted

Predicted values

type(array_type), intent(in), dimension(size(predicted,1),size(predicted,2)) :: expected

Expected values

Return Value type(array_type), pointer

Binary cross entropy loss

private function compute_cce(this, predicted, expected) result(output)

Compute the categorical cross entropy loss of a model

Arguments

Type IntentOptional Attributes Name
class(cce_loss_type), intent(in), target :: this

Instance of the physics-informed neural network loss function

type(array_type), intent(inout), dimension(:,:), target :: predicted

Predicted values

type(array_type), intent(in), dimension(size(predicted,1),size(predicted,2)) :: expected

Expected values

Return Value type(array_type), pointer

Categorical cross entropy loss

private function compute_huber(this, predicted, expected) result(output)

Compute the huber loss of a model

Arguments

Type IntentOptional Attributes Name
class(huber_loss_type), intent(in), target :: this

Instance of the huber loss function

type(array_type), intent(inout), dimension(:,:), target :: predicted

Predicted values

type(array_type), intent(in), dimension(size(predicted,1),size(predicted,2)) :: expected

Expected values

Return Value type(array_type), pointer

Huber loss

private function compute_mae(this, predicted, expected) result(output)

Compute the mean absolute error of a model

Arguments

Type IntentOptional Attributes Name
class(mae_loss_type), intent(in), target :: this
type(array_type), intent(inout), dimension(:,:), target :: predicted

Predicted values

type(array_type), intent(in), dimension(size(predicted,1),size(predicted,2)) :: expected

Expected values

Return Value type(array_type), pointer

Mean absolute error

private function compute_mse(this, predicted, expected) result(output)

Compute the mean squared error of a model

Arguments

Type IntentOptional Attributes Name
class(mse_loss_type), intent(in), target :: this

Instance of the mean squared error loss function

type(array_type), intent(inout), dimension(:,:), target :: predicted

Predicted values

type(array_type), intent(in), dimension(size(predicted,1),size(predicted,2)) :: expected

Expected values

Return Value type(array_type), pointer

Mean squared error loss

private function compute_nll(this, predicted, expected) result(output)

Compute the negative log likelihood of a model

Arguments

Type IntentOptional Attributes Name
class(nll_loss_type), intent(in), target :: this

Instance of the physics-informed neural network loss function

type(array_type), intent(inout), dimension(:,:), target :: predicted

Predicted values

type(array_type), intent(in), dimension(size(predicted,1),size(predicted,2)) :: expected

Expected values

Return Value type(array_type), pointer

Negative log likelihood loss

private module function setup_loss_bce() result(loss)

Set up binary cross entropy loss function

Arguments

None

Return Value type(bce_loss_type)

Binary cross entropy loss function

private module function setup_loss_cce() result(loss)

Set up categorical cross entropy loss function

Arguments

None

Return Value type(cce_loss_type)

Categorical cross entropy loss function

private module function setup_loss_huber() result(loss)

Set up huber loss function

Arguments

None

Return Value type(huber_loss_type)

Huber loss function

private module function setup_loss_mae() result(loss)

Set up mean absolute error loss function

Arguments

None

Return Value type(mae_loss_type)

Mean absolute error loss function

private module function setup_loss_mse() result(loss)

Set up mean squared error loss function

Arguments

None

Return Value type(mse_loss_type)

Mean squared error loss function

private module function setup_loss_nll() result(loss)

Set up negative log likelihood loss function

Arguments

None

Return Value type(nll_loss_type)

Negative log likelihood loss function