Parametric Regression#
regression
#
| CLASS | DESCRIPTION |
|---|---|
RegressionProbabilistic |
Probabilistic regression model with uncertainty estimation. |
Classes#
RegressionProbabilistic(*, model: ModelType | dict[str, ModelType], loss_kwargs: dict | None = None, optimizer_fn: type[torch.optim.Optimizer] | str | None = None, optimizer_kwargs: dict | None = None, lr_scheduler_fn: Any | str | None = None, lr_scheduler_kwargs: dict | None = None, device: Literal['cuda', 'cpu'] = 'cuda')
#
Bases: Parametric
Probabilistic regression model with uncertainty estimation.
This class implements a probabilistic regression approach that predicts both the mean and variance (scale) of target values. The model outputs parameters of a specified probability distribution, enabling uncertainty quantification in predictions.
The architecture consists of:
- A feature extraction network (the main model).
- A mean prediction layer (linear layer -> mean).
- A scale prediction layer (linear layer -> softplus -> positive scale).
The model optimizes the negative log-likelihood of the specified distribution, learning to predict both the expected value and uncertainty for each prediction.
The loss function used is the negative log-likelihood
\(L_{NLL} = -\frac{1}{N} \sum_{i=1}^{N} \log p(y_i | \mu_i, \sigma_i)\),
where \(p(y | \mu, \sigma)\) is the probability density function of the chosen distribution (e.g., Gaussian), \(\mu\) is the predicted mean, and \(\sigma\) is the predicted scale (standard deviation).
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Neural network model for feature extraction. The model output features are used as input to the mean and scale prediction layers.
TYPE:
|
loss_kwargs
|
Additional keyword arguments to pass to the LogLikelihoodLoss constructor.
TYPE:
|
optimizer_fn
|
Optimizer specification for training.
TYPE:
|
optimizer_kwargs
|
Keyword arguments for optimizer instantiation
(e.g.,
TYPE:
|
lr_scheduler_fn
|
Learning rate scheduler specification.
TYPE:
|
lr_scheduler_kwargs
|
Keyword arguments for scheduler instantiation.
TYPE:
|
device
|
Computation device. Automatically falls back to "cpu" if CUDA unavailable.
TYPE:
|
Notes
- The model automatically adds a
torch.nn.Sigmoidoutput activation if none exists - Mean and scale layers are added on top of the feature extraction model
- Scale predictions are constrained to be positive using
torch.nn.Softplusactivation - Sign for the loss is set to -1.0 to convert log-likelihood maximization.
Even if provided otherwise in
loss_kwargs, it will be overridden to -1.0
| METHOD | DESCRIPTION |
|---|---|
forward_step |
Forward pass through the feature extraction model. |
loss |
Compute probabilistic regression loss. |
score_step |
Compute negative log-likelihood as the scoring metric. |
training_step |
Training step for PyTorch Lightning with probabilistic loss. |
predict_step |
Prediction step returning distribution parameters. |
Source code in spectre/parametric/regression.py
Functions#
forward_step(X: torch.Tensor) -> torch.Tensor
#
Forward pass through the feature extraction model.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input features.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
torch.Tensor of shape (n_samples, out_features)
|
Extracted features for mean and scale prediction. |
Source code in spectre/parametric/regression.py
loss(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Compute probabilistic regression loss.
Computes the negative log-likelihood of the target values under the predicted distribution parameters (mean and scale).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input features.
TYPE:
|
weights
|
Sample weights for weighted loss computation.
TYPE:
|
target
|
Target values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar loss value (negative log-likelihood). |
Source code in spectre/parametric/regression.py
score_step(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Compute negative log-likelihood as the scoring metric.
For probabilistic regression, the natural scoring metric is the negative log-likelihood, which measures how well the predicted distribution fits the observed data.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input features.
TYPE:
|
weights
|
Sample weights for weighted scoring.
TYPE:
|
target
|
Target values for scoring.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Negative log-likelihood score (lower is better). |
Source code in spectre/parametric/regression.py
training_step(batch: DataBatch, batch_idx: int) -> torch.Tensor
#
Training step for PyTorch Lightning with probabilistic loss.
Computes the negative log-likelihood loss and logs it for monitoring during training and validation.
| PARAMETER | DESCRIPTION |
|---|---|
batch
|
Input batch containing (X, weights, target) where:
TYPE:
|
batch_idx
|
Batch index.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar training loss (negative log-likelihood). |
Source code in spectre/parametric/regression.py
predict_step(batch: DataBatch, batch_idx: int, dataloader_idx: int | None = None) -> torch.Tensor
#
Prediction step returning distribution parameters.
Predicts both the mean and scale (standard deviation) of the target distribution for uncertainty quantification.
| PARAMETER | DESCRIPTION |
|---|---|
batch
|
Input batch containing input features.
TYPE:
|
batch_idx
|
Batch index (required by PyTorch Lightning but unused).
TYPE:
|
dataloader_idx
|
Dataloader index (required by PyTorch Lightning but unused).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
torch.Tensor of shape (n_samples, 2)
|
Concatenated predictions where:
|