maximum_mean_discrepancy
maximum_mean_discrepancy
¶
Maximum Mean Discrepancy losses for latent distribution matching.
MaximumMeanDiscrepancyLoss
¶
MaximumMeanDiscrepancyLoss(weight=1.0, prior_regularization_weight=0.0, kernel_type=value, bandwidth_multipliers=None, use_median_heuristic=True, use_fixed_gaussian_as_prior=False, prior_target_key=value)
Bases: BaseLoss
MMD loss for regularizing latent distributions toward a prior.
Ref: Info-VAE / MMD-VAE
Initialize MMD loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight
|
float
|
Loss weight for MMD(posterior, prior). |
1.0
|
prior_regularization_weight
|
float
|
Weight for MMD(prior, N(0,I)) regularization. Only meaningful for learned priors. |
0.0
|
kernel_type
|
str
|
Kernel type for MMD computation (see KernelType enum). |
value
|
bandwidth_multipliers
|
list[float] | None
|
Scale factors for bandwidth. When use_median_heuristic=True these scale the adaptive median. When False these are absolute bandwidth values. WAE recommends [2 * latent_dim] with use_median_heuristic=False. |
None
|
use_median_heuristic
|
bool
|
Adaptive bandwidth via median heuristic (True) or fixed absolute bandwidths (False). |
True
|
use_fixed_gaussian_as_prior
|
bool
|
If True, always use standard Gaussian as prior. |
False
|
prior_target_key
|
str
|
Posterior output key used as aggregate prior-matching samples.
Use |
value
|
Source code in src/versatil/metrics/losses/maximum_mean_discrepancy.py
set_weights
¶
Setter that updates the weight scalar coefficients.
Source code in src/versatil/metrics/losses/maximum_mean_discrepancy.py
get_required_keys
¶
Get required keys for MMD loss.
forward
¶
Compute MMD between posterior samples and the configured prior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
dict[str, Tensor]
|
Must contain |
required |
targets
|
dict[str, Tensor]
|
Unused; prior samples come from |
required |
is_pad
|
Tensor | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
LossOutput
|
LossOutput with MMD loss. |
Source code in src/versatil/metrics/losses/maximum_mean_discrepancy.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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 | |
ConditionalMaximumMeanDiscrepancyLoss
¶
ConditionalMaximumMeanDiscrepancyLoss(weight=1.0, state_weight=1.0, kernel_type=value, bandwidth_multipliers=None, use_median_heuristic=False, condition_kernel_type=value, condition_bandwidth_multipliers=None, condition_use_median_heuristic=True, prior_target_key=value, condition_key=value, normalize_condition=True)
Bases: BaseLoss
Product-kernel joint MMD for conditional aggregate matching.
This regularizes q(z|s) toward p(z|s) by matching the empirical
joint samples (s, z_posterior) and (s, z_prior). The state vector
is emitted by the prior and should be action-free. Separate kernels are
used for state and latent samples so their bandwidths can be controlled
independently.
Initialize conditional MMD loss.
Source code in src/versatil/metrics/losses/maximum_mean_discrepancy.py
set_weights
¶
Setter that updates the weight scalar coefficients.
get_required_keys
¶
Get required keys for conditional MMD loss.
forward
¶
Compute joint MMD between posterior and prior conditioned samples.
Source code in src/versatil/metrics/losses/maximum_mean_discrepancy.py
307 308 309 310 311 312 313 314 315 316 317 318 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 | |
BinaryMaximumMeanDiscrepancyLoss
¶
Bases: ScalarWeightedLoss
MMD loss for regularizing binary latent distributions toward a uniform prior.
Encourages q(b|x) ≈ p(b) where p(b) = Bernoulli(0.5) independent for each bit.
Initialize binary MMD loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight
|
float
|
Loss weight. |
1.0
|
kernel_type
|
str
|
Kernel type for MMD computation (see KernelType enum). |
value
|
bandwidth_multipliers
|
list[float] | None
|
Scale factors for the median heuristic bandwidth. |
None
|
Source code in src/versatil/metrics/losses/maximum_mean_discrepancy.py
get_required_keys
¶
Returns required prediction keys: {DecoderOutputKey.BINARY_LOGITS.value}.
forward
¶
Compute MMD between binary latent samples and uniform Bernoulli prior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
dict[str, Tensor]
|
Must contain DecoderOutputKey.BINARY_LOGITS.value with shape (B, H). |
required |
targets
|
dict[str, Tensor]
|
Unused (prior is implicit). |
required |
is_pad
|
Tensor | None
|
Unused. |
None
|
Returns:
| Type | Description |
|---|---|
LossOutput
|
LossOutput with MMD loss. |