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:
FlexibleLossHigh 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:
LossBase class for loss functions that use the JIT compiler.
- Parameters:
loss_expression (str) –
The definition of the loss function. This can be:
A valid mathematical Python expression using
y_predandy_true(e.g.,"(y_pred - y_true)**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:
y_pred (
BackendArray) – Predicted values.y_true (
BackendArray) – True values.
- Returns:
Gradient of the loss with respect to the predicted values.
- Return type:
- forward(y_pred: BackendArray, y_true: BackendArray)[source]
Forward pass of the loss function.
- Parameters:
y_pred (
BackendArray) – Predicted values.y_true (
BackendArray) – True values.
- Returns:
Loss value.
- Return type:
- 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]
- 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:
FlexibleLossHigh 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:
objectBase 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:
y_pred (
BackendArray) – The predicted values.y_true (
BackendArray) – The ground truth values.
- Returns:
The gradient of the loss.
- Return type:
- 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:
y_pred (
BackendArray) – The predicted values.y_true (
BackendArray) – The ground truth values.
- Returns:
The loss value.
- Return type:
- class HeteroSymNN.Core.losses.MAELoss(computational_method: Literal['GPU_CUDA', 'CPU_JIT', 'CPU_PYTHON'] | None = None, gpu_id: int = 0)[source]
Bases:
FlexibleLossHigh 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:
FlexibleLossHigh 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.