kernels
kernels
¶
Kernel functions for Maximum Mean Discrepancy (MMD) computation.
Provides composable kernel modules that can be plugged into MMD-based losses.
MMDKernel
¶
Bases: Module, ABC
Base class for MMD kernels.
Provides shared utilities for pairwise distance computation and the median heuristic for adaptive bandwidth selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_median_heuristic
|
bool
|
When True, bandwidth_multipliers scale the median pairwise squared distance (recomputed per batch). When False, bandwidth_multipliers are used as absolute bandwidth values. WAE (Tolstikhin et al. 2018) recommends fixed bandwidths based on the prior scale (e.g. [2 * latent_dim]) rather than the adaptive median heuristic. |
True
|
Source code in src/versatil/metrics/kernels.py
compute_pairwise_squared_distances
¶
Compute pairwise squared Euclidean distances between point sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
First point set, shape (N, D). |
required |
y
|
Tensor
|
Second point set, shape (M, D). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Distance matrix, shape (N, M). |
Source code in src/versatil/metrics/kernels.py
compute_median_squared_distance
¶
Compute median pairwise squared distance for bandwidth selection.
Ref: https://torchdrift.org/notebooks/note_on_mmd.html
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
Point set, shape (N, D). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Median squared distance (scalar). |
Source code in src/versatil/metrics/kernels.py
resolve_base_bandwidth
¶
Resolve the base bandwidth from a combined sample set.
When use_median_heuristic is True, returns 2 * median_dist^2 computed from the provided samples. When False, returns 1.0 so that bandwidth_multipliers are used as absolute values.
Callers that want a single bandwidth shared across multiple kernel evaluations (e.g. the three MMD terms K(x,x), K(y,y), K(x,y)) must pass the combined sample set here and then feed the returned value to each forward() call via the bandwidth argument. Relying on forward()'s default per-call resolution produces inconsistent kernels across the three MMD terms and breaks the statistic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
Tensor
|
Combined point set, shape (N, D). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Base bandwidth scalar. |
Source code in src/versatil/metrics/kernels.py
forward
abstractmethod
¶
Compute kernel matrix K(x, y).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
First point set, shape (N, D). |
required |
y
|
Tensor
|
Second point set, shape (M, D). |
required |
bandwidth
|
float | None
|
Pre-resolved base bandwidth. When None, computed from the combined set (x, y). Pass a shared bandwidth explicitly when computing multiple kernel terms that must use the same kernel function (e.g. MMD's three terms). |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Kernel matrix, shape (N, M). |
Source code in src/versatil/metrics/kernels.py
RBFKernel
¶
Bases: MMDKernel
Multi-scale RBF (Gaussian) kernel.
K(x, y) = (1/S) * sum_i exp(-||x-y||^2 / (s_i * base_bandwidth))
With median heuristic (default): base_bandwidth = 2 * median_dist^2. With fixed bandwidth: base_bandwidth = 1.0, so s_i are absolute values.
Ref: Gretton et al., "A Kernel Two-Sample Test", https://jmlr.org/papers/volume13/gretton12a/gretton12a.pdf
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bandwidth_multipliers
|
list[float] | None
|
Scale factors applied to the base bandwidth. With median heuristic, these are relative to the median. Without, these are absolute bandwidth values. |
None
|
use_median_heuristic
|
bool
|
Adaptive bandwidth via median heuristic (True) or fixed absolute bandwidths (False). |
True
|
Source code in src/versatil/metrics/kernels.py
forward
¶
Compute multi-scale RBF kernel matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
First point set, shape (N, D). |
required |
y
|
Tensor
|
Second point set, shape (M, D). |
required |
bandwidth
|
float | None
|
Pre-resolved base bandwidth. When None, computed from cat(x, y). See MMDKernel.resolve_base_bandwidth. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Kernel matrix, shape (N, M). |
Source code in src/versatil/metrics/kernels.py
IMQKernel
¶
Bases: MMDKernel
Multi-scale Inverse Multiquadratic kernel.
K(x, y) = (1/S) * sum_i C_i / (C_i + ||x-y||^2)
With median heuristic (default): C_i = s_i * 2 * median_dist^2. With fixed bandwidth: C_i = s_i directly. WAE (Tolstikhin et al. 2018) recommends C = 2 * latent_dim for a standard Gaussian prior, so pass bandwidth_multipliers=[2 * latent_dim] with use_median_heuristic=False.
Ref: Tolstikhin et al., "Wasserstein Auto-Encoders" (ICLR 2018)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bandwidth_multipliers
|
list[float] | None
|
Scale factors applied to the base bandwidth. With median heuristic, these are relative to the median. Without, these are absolute C values. |
None
|
use_median_heuristic
|
bool
|
Adaptive bandwidth via median heuristic (True) or fixed absolute bandwidths (False). |
True
|
Source code in src/versatil/metrics/kernels.py
forward
¶
Compute multi-scale IMQ kernel matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
First point set, shape (N, D). |
required |
y
|
Tensor
|
Second point set, shape (M, D). |
required |
bandwidth
|
float | None
|
Pre-resolved base bandwidth. When None, computed from cat(x, y). See MMDKernel.resolve_base_bandwidth. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Kernel matrix, shape (N, M). |
Source code in src/versatil/metrics/kernels.py
KernelType
¶
Bases: StrEnum
Kernel type for MMD computation.
to_kernel
¶
Instantiate the corresponding kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bandwidth_multipliers
|
list[float] | None
|
Scale factors for bandwidth. Relative to median when use_median_heuristic=True, absolute otherwise. |
None
|
use_median_heuristic
|
bool
|
Adaptive bandwidth via median heuristic (True) or fixed absolute bandwidths (False). |
True
|
Returns:
| Type | Description |
|---|---|
MMDKernel
|
Instantiated MMDKernel. |