Overview
Loss functions measure the discrepancy between predicted and target values, guiding the optimization process during training.MSELoss
MSE = mean((prediction - target)^2)
Parameters
str
default:"mean"
Specifies the reduction to apply: ‘none’, ‘mean’, or ‘sum’.
Example
Use Cases
- Regression tasks
- Image reconstruction
- Autoencoders
L1Loss
L1 = mean(|prediction - target|)
Example
Use Cases
- Robust regression (less sensitive to outliers than MSE)
- Image-to-image translation
CrossEntropyLoss
Parameters
Optional[Tensor]
Manual rescaling weight for each class. Shape: (num_classes,)
int
default:"-100"
Specifies a target value that is ignored and does not contribute to the gradient.
str
default:"mean"
Specifies the reduction to apply: ‘none’, ‘mean’, or ‘sum’.
Example
Use Cases
- Multi-class classification
- Image classification
- Text classification
- Semantic segmentation
BCELoss
Parameters
Optional[Tensor]
Manual rescaling weight for the loss of each batch element.
str
default:"mean"
Specifies the reduction to apply: ‘none’, ‘mean’, or ‘sum’.
Example
Use Cases
- Binary classification
- Multi-label classification (independent binary decisions)
BCEWithLogitsLoss
Parameters
Optional[Tensor]
Manual rescaling weight for the loss of each batch element.
Optional[Tensor]
Weight for positive examples. Useful for imbalanced datasets.
str
default:"mean"
Specifies the reduction to apply: ‘none’, ‘mean’, or ‘sum’.
Example
Use Cases
- Binary classification (preferred over BCELoss for numerical stability)
- Multi-label classification
- Object detection