Losses
centimators.losses.SpearmanCorrelation
Bases: Loss
Differentiable Spearman rank correlation loss.
This loss function computes a soft approximation of Spearman's rank correlation coefficient between predictions and targets. Unlike the standard non-differentiable rank correlation, this implementation uses sigmoid-based soft rankings that allow gradient flow during backpropagation.
The loss is computed as the negative correlation (to minimize during training) between the soft ranks of predictions and targets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
regularization_strength
|
float, default=1e-3
|
Temperature parameter for the sigmoid function used in soft ranking. Smaller values create sharper (more discrete) rankings, while larger values create smoother approximations. Typically ranges from 1e-4 to 1e-1. |
0.001
|
name
|
str, default="spearman_correlation"
|
Name of the loss function. |
'spearman_correlation'
|
**kwargs
|
Additional keyword arguments passed to the base Loss class. |
{}
|
Examples:
>>> import keras
>>> loss_fn = SpearmanCorrelation(regularization_strength=0.01)
>>> model = keras.Sequential([...])
>>> model.compile(optimizer='adam', loss=loss_fn)
Source code in src/centimators/losses.py
call(y_true, y_pred)
Compute the Spearman correlation loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true
|
Ground truth values of shape (batch_size,) or (batch_size, 1). |
required | |
y_pred
|
Predicted values of shape (batch_size,) or (batch_size, 1). |
required |
Returns:
Type | Description |
---|---|
Scalar loss value (negative correlation). |
Source code in src/centimators/losses.py
centimators.losses.CombinedLoss
Bases: Loss
Weighted combination of MSE and Spearman correlation losses.
This loss function combines mean squared error (for absolute accuracy) with Spearman correlation loss (for rank preservation). This can be particularly useful when both the exact values and their relative ordering are important.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mse_weight
|
float, default=2.0
|
Weight applied to the MSE component. Higher values prioritize absolute accuracy. |
2.0
|
spearman_weight
|
float, default=1.0
|
Weight applied to the Spearman correlation component. Higher values prioritize rank preservation. |
1.0
|
spearman_regularization
|
float, default=1e-3
|
Regularization strength passed to the SpearmanCorrelation loss. |
0.001
|
name
|
str, default="combined_loss"
|
Name of the loss function. |
'combined_loss'
|
**kwargs
|
Additional keyword arguments passed to the base Loss class. |
{}
|
Examples:
>>> # Prioritize ranking accuracy over absolute values
>>> loss_fn = CombinedLoss(mse_weight=0.5, spearman_weight=2.0)
>>> model.compile(optimizer='adam', loss=loss_fn)
Source code in src/centimators/losses.py
call(y_true, y_pred)
Compute the combined loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true
|
Ground truth values of shape (batch_size,) or (batch_size, 1). |
required | |
y_pred
|
Predicted values of shape (batch_size,) or (batch_size, 1). |
required |
Returns:
Type | Description |
---|---|
Scalar loss value (weighted sum of MSE and negative Spearman correlation). |