divergence
divergence
¶
Divergence and entropy losses for latent distributions.
GaussianEntropyLoss
¶
Bases: BaseLoss
Entropy regularization for Gaussian distributions.
Maximizes entropy H(N(μ, σ²)) = 0.5 * sum(1 + log(2π) + logvar) to prevent distribution collapse.
Since we maximize entropy, this loss contributes negatively to the total.
Initialize Gaussian entropy loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Prediction key for logvar tensor to compute entropy over. |
value
|
weight
|
float
|
Loss weight. Positive values encourage higher entropy. |
0.01
|
logvar_min
|
float
|
Minimum logvar value. |
-4.0
|
logvar_max
|
float
|
Maximum logvar value. |
2.0
|
bound_weight
|
float
|
Weight for the bound entropy loss. |
1.0
|
Source code in src/versatil/metrics/losses/divergence.py
set_weights
¶
Setter that updates the weight scalar coefficients.
Source code in src/versatil/metrics/losses/divergence.py
get_required_keys
¶
compute_entropy
staticmethod
¶
Compute entropy of a diagonal Gaussian.
H(N(μ, σ²)) = 0.5 * sum(1 + log(2π) + logvar)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logvar
|
Tensor
|
Log variance tensor (..., latent_dim). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Entropy summed over latent dimensions, shape (...). |
Source code in src/versatil/metrics/losses/divergence.py
forward
¶
Compute negative entropy loss (to maximize entropy via minimization).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
dict[str, Tensor]
|
Must contain the logvar key. |
required |
targets
|
dict[str, Tensor]
|
Unused. |
required |
is_pad
|
Tensor | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
LossOutput
|
LossOutput with negative weighted entropy. |
Source code in src/versatil/metrics/losses/divergence.py
KLDivergenceLoss
¶
Bases: BaseLoss
KL divergence loss for VAE latent distributions.
Initialize KL divergence loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight
|
float
|
Weight for KL divergence loss KL(posterior || prior) |
10.0
|
prior_regularization_weight
|
float
|
Weight for KL(prior || N(0,I)) regularization. Only meaningful for learned priors. Pushes the learned prior towards a standard Gaussian. |
0.0
|
Source code in src/versatil/metrics/losses/divergence.py
set_weights
¶
Setter that updates the weight scalar coefficients.
Source code in src/versatil/metrics/losses/divergence.py
get_required_keys
¶
Get required keys for KL divergence loss.
Source code in src/versatil/metrics/losses/divergence.py
forward
¶
Compute KL divergence loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
dict[str, Tensor]
|
Dictionary with 'mu' and 'logvar' keys |
required |
targets
|
dict[str, Tensor]
|
Not used for KL divergence |
required |
is_pad
|
Tensor | None
|
Not used for KL divergence |
None
|
Returns:
| Type | Description |
|---|---|
LossOutput
|
LossOutput with KL divergence loss |
Source code in src/versatil/metrics/losses/divergence.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
BinaryKLDivergenceLoss
¶
Bases: BaseLoss
KL divergence loss for Free Transformer binary latent distributions.
Computes KL divergence between learned binary distributions and uniform prior. Used with Free Transformer's binary mapper output.
Based on "The Free Transformer" (Fleuret, 2025) - arXiv:2510.17558
Initialize binary KL divergence loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight
|
float
|
Weight for KL divergence loss |
5.0
|
entropy_weight
|
float
|
Weight for the entropy regularization term |
0.01
|
latent_bits
|
float
|
Number of bits of the latent codes. |
64
|
free_bits
|
float
|
Free bits threshold (only penalize KL above this value) |
2 * log(2)
|
Source code in src/versatil/metrics/losses/divergence.py
set_weights
¶
Setter that updates the weight scalar coefficients.
Source code in src/versatil/metrics/losses/divergence.py
get_required_keys
¶
Get required keys for binary KL divergence loss.
Returns:
| Type | Description |
|---|---|
set[str]
|
Set containing binary_logits key from Free Transformer |
forward
¶
Compute binary KL divergence loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
dict[str, Tensor]
|
Dictionary with 'binary_logits' key (B, T, H) or (B, H) |
required |
targets
|
dict[str, Tensor]
|
Not used for KL divergence |
required |
is_pad
|
Tensor | None
|
Optional padding mask (B, T) or (B,) |
None
|
Returns:
| Type | Description |
|---|---|
LossOutput
|
LossOutput with KL divergence loss |
Source code in src/versatil/metrics/losses/divergence.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |