the-algorithm-ml/projects/home/recap/model/numeric_calibration.py

67 lines
2.5 KiB
Python
Raw Normal View History

import torch
class NumericCalibration(torch.nn.Module):
2023-09-21 19:23:34 +02:00
"""
Numeric calibration module for adjusting probability scores.
2023-09-14 08:00:10 +02:00
2023-09-21 19:23:34 +02:00
This module scales probability scores to correct for imbalanced datasets, where positive and negative samples
may be underrepresented or have different ratios. It is designed to be used as a component in a neural network
for tasks such as binary classification.
2023-09-14 08:00:10 +02:00
2023-09-21 19:23:34 +02:00
Args:
pos_downsampling_rate (float): The downsampling rate for positive samples.
neg_downsampling_rate (float): The downsampling rate for negative samples.
2023-09-14 08:00:10 +02:00
2023-09-21 19:23:34 +02:00
Example:
To use `NumericCalibration` in a PyTorch model, you can create an instance of it and apply it to probability
scores like this:
2023-09-14 08:00:10 +02:00
2023-09-21 19:23:34 +02:00
```python
# Create a NumericCalibration instance with downsampling rates
calibration = NumericCalibration(pos_downsampling_rate=0.1, neg_downsampling_rate=0.2)
2023-09-14 08:00:10 +02:00
2023-09-21 19:23:34 +02:00
# Generate probability scores (e.g., from a neural network)
raw_probs = torch.tensor([0.8, 0.6, 0.2, 0.9])
2023-09-14 08:00:10 +02:00
2023-09-21 19:23:34 +02:00
# Apply numeric calibration to adjust the probabilities
calibrated_probs = calibration(raw_probs)
2023-09-14 08:00:10 +02:00
2023-09-21 19:23:34 +02:00
# The `calibrated_probs` now contains the adjusted probability scores
```
2023-09-14 08:00:10 +02:00
2023-09-21 19:23:34 +02:00
Note:
The `NumericCalibration` module is used to adjust probability scores to account for differences in
the number of positive and negative samples in a dataset. It can help improve the calibration of
probability estimates in imbalanced classification problems.
2023-09-14 08:00:10 +02:00
2023-09-21 19:23:34 +02:00
Warning:
This class is intended for internal use within neural network architectures and should not be
directly accessed or modified by external code.
"""
2023-09-14 08:00:10 +02:00
2023-09-21 19:23:34 +02:00
def __init__(
self,
pos_downsampling_rate: float,
neg_downsampling_rate: float,
):
2023-09-14 08:00:10 +02:00
"""
2023-09-21 19:23:34 +02:00
Apply numeric calibration to probability scores.
Args:
probs (torch.Tensor): Probability scores to be calibrated.
Returns:
torch.Tensor: Calibrated probability scores.
"""
super().__init__()
2023-09-21 19:23:34 +02:00
# Using buffer to make sure they are on correct device (and not moved every time).
# Will also be part of state_dict.
self.register_buffer(
"ratio", torch.as_tensor(neg_downsampling_rate / pos_downsampling_rate), persistent=True
)
2023-09-21 19:23:34 +02:00
def forward(self, probs: torch.Tensor):
return probs * self.ratio / (1.0 - probs + (self.ratio * probs))