4. Losses
4.1
elbo
This module contains the code for the Losses.
4.1.1
ELBOLoss(loss_function, num_samples=1, kl_weight=0.001)
Computes the Evidence Lower Bound (ELBO) loss, combining a reconstruction loss and KL divergence.
Initializes the ELBO loss with specified reconstruction loss function, sample count, and KL weight.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loss_function
|
Module
|
Loss function for computing reconstruction loss. |
required |
num_samples
|
int
|
Number of samples for Monte Carlo approximation. |
1
|
kl_weight
|
float
|
Scaling factor for the KL divergence component. |
0.001
|
Source code in illia/losses/torch/elbo.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
4.1.1.1
forward(outputs, targets, model)
Computes the ELBO loss, averaging over multiple samples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
outputs
|
Tensor
|
Predicted values from the model. |
required |
targets
|
Tensor
|
True target values. |
required |
model
|
Module
|
PyTorch model containing Bayesian modules. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Average ELBO loss across samples. |
Source code in illia/losses/torch/elbo.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
4.1.2
KLDivergenceLoss(reduction='mean', weight=1.0)
Computes the KL divergence loss for Bayesian modules within a model.
Initializes the KL Divergence Loss with specified reduction method and weight.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reduction
|
Literal['mean']
|
Method to reduce the loss, currently only "mean" is supported. |
'mean'
|
weight
|
float
|
Scaling factor for the KL divergence loss. |
1.0
|
Source code in illia/losses/torch/elbo.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
4.1.2.1
forward(model)
Computes the KL divergence loss across all Bayesian modules in the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
PyTorch model containing Bayesian modules. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
KL divergence cost scaled by the specified weight. |
Source code in illia/losses/torch/elbo.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|