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
Interface for binary cross entropy loss function
Set up binary cross entropy loss function
Binary cross entropy loss function
Interface for categorical cross entropy loss function
Set up categorical cross entropy loss function
Categorical cross entropy loss function
Interface for huber loss function
Set up huber loss function
Huber loss function
Interface for mean absolute error loss function
Set up mean absolute error loss function
Mean absolute error loss function
Interface for mean squared error loss function
Set up mean squared error loss function
Mean squared error loss function
Interface for negative log likelihood loss function
Set up negative log likelihood loss function
Negative log likelihood loss function
Compute the loss of a model
| Type | Intent | Optional | 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 |
Physics-informed neural network loss
Abstract type for loss functions
| 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 |
| procedure(compute_base), public, deferred, pass(this) :: compute | Compute the loss of a model |
Binary cross entropy loss function
| 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 |
Interface for binary cross entropy loss function
| private module function setup_loss_bce () | Set up binary cross entropy loss function |
| procedure, public :: compute => compute_bce | Compute the loss of a model |
Categorical cross entropy loss function
| 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 |
Interface for categorical cross entropy loss function
| private module function setup_loss_cce () | Set up categorical cross entropy loss function |
| procedure, public :: compute => compute_cce | Compute the loss of a model |
Huber loss function
| 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 |
Interface for huber loss function
| private module function setup_loss_huber () | Set up huber loss function |
| procedure, public :: compute => compute_huber | Compute the loss of a model |
Mean absolute error loss function
| 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 |
Interface for mean absolute error loss function
| private module function setup_loss_mae () | Set up mean absolute error loss function |
| procedure, public :: compute => compute_mae | Compute the loss of a model |
Mean squared error loss function
| 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 |
Interface for mean squared error loss function
| private module function setup_loss_mse () | Set up mean squared error loss function |
| procedure, public :: compute => compute_mse | Compute the loss of a model |
Negative log likelihood loss function
| 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 |
Interface for negative log likelihood loss function
| private module function setup_loss_nll () | Set up negative log likelihood loss function |
| procedure, public :: compute => compute_nll | Compute the loss of a model |
Placeholder for compute function in base_loss_type
| Type | Intent | Optional | 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 |
Loss value
Compute the binary cross entropy loss of a model
| Type | Intent | Optional | 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 |
Binary cross entropy loss
Compute the categorical cross entropy loss of a model
| Type | Intent | Optional | 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 |
Categorical cross entropy loss
Compute the huber loss of a model
| Type | Intent | Optional | 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 |
Huber loss
Compute the mean absolute error of a model
| Type | Intent | Optional | 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 |
Mean absolute error
Compute the mean squared error of a model
| Type | Intent | Optional | 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 |
Mean squared error loss
Compute the negative log likelihood of a model
| Type | Intent | Optional | 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 |
Negative log likelihood loss
Set up binary cross entropy loss function
Binary cross entropy loss function
Set up categorical cross entropy loss function
Categorical cross entropy loss function
Set up mean absolute error loss function
Mean absolute error loss function
Set up mean squared error loss function
Mean squared error loss function
Set up negative log likelihood loss function
Negative log likelihood loss function