Loss Functions

This module contains the Loss base class, which establishes the interface for custom objective functions, along with a set of pre-implemented loss functions such as MSE and Cross-Entropy.

class HeteroSymNN.Core.losses.BinaryCrossEntropy(computational_method: Literal['GPU_CUDA', 'CPU_JIT', 'CPU_PYTHON'] | None = None, gpu_id: int = 0)[source]

Bases: FlexibleLoss

High level class for calling for a Binary Cross Entropy loss function.

Parameters:
  • computational_method (Literal["GPU_CUDA","CPU_JIT","CPU_PYTHON"], optional) – The computational method that is going to be used, by default None.

  • gpu_id (int, optional) – The GPU ID that is going to be used if method is “GPU_CUDA”, by default 0.

class HeteroSymNN.Core.losses.FlexibleLoss(loss_expression: str = '(y_pred - y_true)**2', constants: dict[str, float] | None = None, computational_method: Literal['GPU_CUDA', 'CPU_JIT', 'CPU_PYTHON'] | None = None, gpu_id: int = 0)[source]

Bases: Loss

Base class for loss functions that use the JIT compiler.

Parameters:
  • loss_expression (str) –

    The definition of the loss function. This can be:

    1. A valid mathematical Python expression using y_pred and y_true (e.g., "(y_pred - y_true)**2").

    2. A case-insensitive key from the predefined COMMON_FORMULAS (e.g., "mse", "bce", "huber").

  • constants (dict[str, float], optional) – Dictionary of the non-standard constants that are in the loss function if any, by default None.

  • computational_method (Literal["GPU_CUDA","CPU_JIT","CPU_PYTHON"], optional) – The computational method that is going to be used, by default None.

  • gpu_id (int, optional) – The GPU ID that is going to be used if method is “GPU_CUDA”, by default 0.

Examples

>>> from HeteroSymNN.Core.losses import FlexibleLoss
>>> from HeteroSymNN.Core.Nets.neural_nets import SimpleNN
>>> net_structure = [4, 6, 3]
>>> loss_expression = "abs(y_pred - y_true)"
>>> example_net = SimpleNN(net_structure,"sigmoid",loss_function=FlexibleLoss(loss_expression))
property COMPILER: SymbolicJITCompiler

Compiler class that is used for the symbolic loss function.

Return type:

SymbolicJITCompiler

property COMPUTATIONAL_METHOD: Literal['GPU_CUDA', 'CPU_JIT', 'CPU_PYTHON']

Current computational method used.

Return type:

Literal[“GPU_CUDA”,”CPU_JIT”,”CPU_PYTHON”]

property GPU_ID: int

Current GPU ID used.

Return type:

int

property LOSS_EXPRESSION: str

Expression or name of the loss function used.

Return type:

str

backward(y_pred: BackendArray, y_true: BackendArray)[source]

Backward pass of the loss function.

Parameters:
Returns:

Gradient of the loss with respect to the predicted values.

Return type:

BackendArray

forward(y_pred: BackendArray, y_true: BackendArray)[source]

Forward pass of the loss function.

Parameters:
Returns:

Loss value.

Return type:

BackendArray

get_config()[source]

Returns the configuration of the loss instance.

This method returns a dictionary containing the parameters necessary to reconstruct the loss function and its internal state.

Returns:

A dictionary containing the class name, loss expression, and constants.

Return type:

dict[str,any]

get_constants() dict[str, float][source]

Method to get the constants values of the loss function if it has any.

Return type:

dict[str,float]

set_constants(constants: dict[str, float]) None[source]

Method to set the constants of the loss function using a dictionary of the string of the constant and its new value.

Parameters:

constants (dict[str, float])

set_gpu_id(new_id: int) None[source]

Method to set a new GPU to do the computation.

Parameters:

new_id (int)

class HeteroSymNN.Core.losses.HuberLoss(delta: float = 1.0, computational_method: Literal['GPU_CUDA', 'CPU_JIT', 'CPU_PYTHON'] | None = None, gpu_id: int = 0)[source]

Bases: FlexibleLoss

High level class for calling for a Huber loss function.

Parameters:
  • delta (float) – Value of the constant Delta

  • computational_method (Literal["GPU_CUDA","CPU_JIT","CPU_PYTHON"], optional) – The computational method that is going to be used, by default None.

  • gpu_id (int, optional) – The GPU ID that is going to be used if method is “GPU_CUDA”, by default 0.

class HeteroSymNN.Core.losses.Loss[source]

Bases: object

Base class for all loss functions.

All methods that any loss class needs to have are defined here.

backward(y_pred: BackendArray, y_true: BackendArray) BackendArray[source]

Computes the gradient of the loss.

This method must be implemented by subclasses to define the backward pass (gradient computation) of the loss function.

Parameters:
Returns:

The gradient of the loss.

Return type:

BackendArray

forward(y_pred: BackendArray, y_true: BackendArray) BackendArray[source]

Computes the loss value.

This method must be implemented by subclasses to define the forward pass of the loss function.

Parameters:
Returns:

The loss value.

Return type:

BackendArray

get_config()[source]

Returns the configuration of the loss instance.

This method should be implemented by subclasses to return a dictionary containing the parameters necessary to reconstruct the loss function and its internal state.

set_gpu_id(new_id: int)[source]

Sets the GPU ID for computation.

This method must be implemented by subclasses to update the GPU ID used for internal calculations.

Parameters:

new_id (int) – The new GPU ID.

class HeteroSymNN.Core.losses.MAELoss(computational_method: Literal['GPU_CUDA', 'CPU_JIT', 'CPU_PYTHON'] | None = None, gpu_id: int = 0)[source]

Bases: FlexibleLoss

High level class for calling for a MAE loss function.

Parameters:
  • computational_method (Literal["GPU_CUDA","CPU_JIT","CPU_PYTHON"], optional) – The computational method that is going to be used, by default None.

  • gpu_id (int, optional) – The GPU ID that is going to be used if method is “GPU_CUDA”, by default 0.

class HeteroSymNN.Core.losses.MSELoss(computational_method: Literal['GPU_CUDA', 'CPU_JIT', 'CPU_PYTHON'] | None = None, gpu_id: int = 0)[source]

Bases: FlexibleLoss

High level class for calling for a MSE loss function.

Parameters:
  • computational_method (Literal["GPU_CUDA","CPU_JIT","CPU_PYTHON"], optional) – The computational method that is going to be used, by default None.

  • gpu_id (int, optional) – The GPU ID that is going to be used if method is “GPU_CUDA”, by default 0.