Loss Functions
The loss module provides loss functions for training neural networks.
Regression Losses
Regression loss functions.
- class fit.loss.regression.MSELoss(reduction='mean')[source]
Bases:
objectMean Squared Error loss for regression.
L(y, ŷ) = (1/n) * Σ(y - ŷ)²
- __init__(reduction='mean')[source]
Initialize MSELoss.
- Parameters:
reduction – Reduction method (‘mean’, ‘sum’, or ‘none’)
- class fit.loss.regression.MAELoss(reduction='mean')[source]
Bases:
objectMean Absolute Error loss for regression.
L(y, ŷ) = (1/n) * Σ|y - ŷ|
- __init__(reduction='mean')[source]
Initialize MAELoss.
- Parameters:
reduction – Reduction method (‘mean’, ‘sum’, or ‘none’)
- class fit.loss.regression.SmoothL1Loss(beta=1.0, reduction='mean')[source]
Bases:
objectSmooth L1 Loss (Huber Loss) for regression.
Less sensitive to outliers than MSE. L(x) = 0.5*x² if |x| < β
β*|x| - 0.5*β² otherwise
- __init__(beta=1.0, reduction='mean')[source]
Initialize SmoothL1Loss.
- Parameters:
beta – Threshold for switching between L2 and L1 loss
reduction – Reduction method (‘mean’, ‘sum’, or ‘none’)
- class fit.loss.regression.HuberLoss(delta=1.0, reduction='mean')[source]
Bases:
objectHuber Loss for regression.
Combines MSE and MAE - quadratic for small errors, linear for large errors.
- __init__(delta=1.0, reduction='mean')[source]
Initialize Huber Loss.
- Parameters:
delta – Threshold for switching between quadratic and linear
reduction – Reduction method (‘mean’, ‘sum’, or ‘none’)
- class fit.loss.regression.LogCoshLoss(reduction='mean')[source]
Bases:
objectLogarithm of the hyperbolic cosine loss.
Smooth approximation to MAE that is less sensitive to outliers than MSE.
- __init__(reduction='mean')[source]
Initialize LogCosh Loss.
- Parameters:
reduction – Reduction method (‘mean’, ‘sum’, or ‘none’)
- class fit.loss.regression.QuantileLoss(quantile=0.5, reduction='mean')[source]
Bases:
objectQuantile Loss for quantile regression.
Allows predicting specific quantiles of the target distribution.
- __init__(quantile=0.5, reduction='mean')[source]
Initialize Quantile Loss.
- Parameters:
quantile – Quantile to predict (0 < quantile < 1)
reduction – Reduction method (‘mean’, ‘sum’, or ‘none’)
- class fit.loss.regression.CosineSimilarityLoss(reduction='mean')[source]
Bases:
objectCosine Similarity Loss for regression.
Measures the cosine of the angle between prediction and target vectors.
- __init__(reduction='mean')[source]
Initialize Cosine Similarity Loss.
- Parameters:
reduction – Reduction method (‘mean’, ‘sum’, or ‘none’)
Classification Losses
Classification loss functions.
- class fit.loss.classification.CrossEntropyLoss(reduction='mean')[source]
Bases:
objectCross-entropy loss for multi-class classification.
Combines log-softmax and negative log-likelihood in a numerically stable way.
- __init__(reduction='mean')[source]
Initialize CrossEntropyLoss.
- Parameters:
reduction – Reduction method (‘mean’, ‘sum’, or ‘none’)
- class fit.loss.classification.BinaryCrossEntropyLoss(reduction='mean')[source]
Bases:
objectBinary cross-entropy loss for binary classification.
- __init__(reduction='mean')[source]
Initialize BinaryCrossEntropyLoss.
- Parameters:
reduction – Reduction method (‘mean’, ‘sum’, or ‘none’)
- class fit.loss.classification.NLLLoss(reduction='mean')[source]
Bases:
objectNegative Log-Likelihood loss.
Expects log-probabilities as input (e.g., output of log-softmax).
- __init__(reduction='mean')[source]
Initialize NLLLoss.
- Parameters:
reduction – Reduction method (‘mean’, ‘sum’, or ‘none’)
- class fit.loss.classification.FocalLoss(alpha=1.0, gamma=2.0, reduction='mean')[source]
Bases:
objectFocal Loss for addressing class imbalance.
Focal Loss = -α(1-p)^γ * log(p)
- __init__(alpha=1.0, gamma=2.0, reduction='mean')[source]
Initialize Focal Loss.
- Parameters:
alpha – Weighting factor for rare class
gamma – Focusing parameter
reduction – Reduction method (‘mean’, ‘sum’, or ‘none’)