Data Dataset Weights#
dataset_weights
#
| FUNCTION | DESCRIPTION |
|---|---|
stabilize_weights |
Stabilize highly-variable sample weights using the specified method. |
weights_log_space_clip |
Clip weights in log-space. |
weights_quantile_clip |
Hard-clip raw weights at a given quantile. |
weights_softmax_normalize |
Apply softmax in log-weight space and rescale. |
weights_ess_filter |
Zero-out weights that fall below an effective sample size threshold. |
Functions#
stabilize_weights(weights: torch.Tensor, method: Literal['log_space_clip', 'quantile_clip', 'softmax_normalize', 'ess_filter'] = 'log_space_clip', normalize: bool = True, **kwargs) -> torch.Tensor
#
Stabilize highly-variable sample weights using the specified method.
| PARAMETER | DESCRIPTION |
|---|---|
weights
|
Sample weights (1D tensor).
TYPE:
|
method
|
Stabilization algorithm. Can be:
TYPE:
|
normalize
|
If True, divide the result by its mean so the output mean equals one.
Has no effect for
TYPE:
|
**kwargs
|
Additional keyword arguments forwarded to the chosen method function. See the individual function docstrings for available parameters.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
1D tensor of stabilized weights. |
Examples:
Default log-space clipping:
>>> import torch
>>> from spectre.data import stabilize_weights
>>> weights = torch.exp(torch.randn(500))
>>> stable = stabilize_weights(weights)
>>> stable.mean()
tensor(1.)
Quantile clipping at the 95th percentile:
Notes
- All methods retain every sample in the dataset; weights are modified in place of discarding samples.
"log_space_clip"is recommended for exponential weights from metadynamics because clipping the log-weight is equivalent to capping the bias magnitude."normalize"rescales to mean equal to 1 after stabilization to prevent scale drift.
Source code in spectre/data/dataset_weights.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 73 74 75 76 77 78 79 80 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 106 107 108 109 110 111 112 113 | |
weights_log_space_clip(weights: torch.Tensor, n_sigma: float = 3.0) -> torch.Tensor
#
Clip weights in log-space.
Computes the logarithm of weights log(w) and clips any value above
median(log_w) + n_sigma * std(log_w) before exponentiating back. The
median is used instead of the mean as the log-space center so that the
threshold itself is not skewed by the extreme values being suppressed.
| PARAMETER | DESCRIPTION |
|---|---|
weights
|
Sample weights (1D tensor).
TYPE:
|
n_sigma
|
Number of standard deviations above the log-space median used as the clipping threshold. Larger values allow more extreme weights through; smaller values enforce stronger regularization. Typical range: 2–5.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
1D tensor of clipped weights (not normalized). |
Examples:
>>> import torch
>>> from spectre.data import weights_log_space_clip
>>> weights = torch.exp(torch.randn(500))
>>> clipped = weights_log_space_clip(weights, n_sigma=3.0)
>>> clipped.max() <= weights.max()
tensor(True)
Source code in spectre/data/dataset_weights.py
weights_quantile_clip(weights: torch.Tensor, quantile: float = 0.99) -> torch.Tensor
#
Hard-clip raw weights at a given quantile.
All weights above the quantile-th percentile are set to that percentile
value (Winsorization).
| PARAMETER | DESCRIPTION |
|---|---|
weights
|
1D tensor of sample weights.
TYPE:
|
quantile
|
Fraction of samples to keep unclipped. Must be in (0, 1). For example, 0.99 clips the top 1 % of weights.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
1D tensor of clipped weights (not normalized). |
Examples:
>>> import torch
>>> from spectre.data import weights_quantile_clip
>>> weights = torch.exp(torch.randn(500))
>>> clipped = weights_quantile_clip(weights, quantile=0.99)
>>> clipped.max() <= weights.max()
tensor(True)
Source code in spectre/data/dataset_weights.py
weights_softmax_normalize(weights: torch.Tensor) -> torch.Tensor
#
Apply softmax in log-weight space and rescale.
Computes softmax(log(weights)) then multiplies by the number of samples
(equivalent to a mean of 1). When the range of log-weights is very large
the distribution is effectively flattened; choose weights_log_space_clip
if preserving the relative ranking of high-weight samples is important.
| PARAMETER | DESCRIPTION |
|---|---|
weights
|
1D tensor of sample weights.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
1D tensor of normalized weights that sum to |
Examples:
>>> import torch
>>> from spectre.data import weights_softmax_normalize
>>> weights = torch.exp(torch.randn(500))
>>> normed = weights_softmax_normalize(weights)
>>> normed.sum().round()
tensor(500.)
Source code in spectre/data/dataset_weights.py
weights_ess_filter(weights: torch.Tensor, threshold: float = 0.3) -> torch.Tensor
#
Zero-out weights that fall below an effective sample size threshold.
Computes the effective sample size (ESS) after cumulatively including each
sample in time order, and sets the weight of all samples to zero until the
cumulative ESS fraction ESS / n_included exceeds threshold. This
identifies the earliest point at which the weight distribution is
sufficiently converged and masks out everything before it.
ESS is defined as (sum(w))^2 / sum(w^2), which ranges from 1 (all weight
on one sample) to N (uniform weights).
| PARAMETER | DESCRIPTION |
|---|---|
weights
|
1D tensor of sample weights ordered by time (earliest first).
TYPE:
|
threshold
|
Minimum ESS fraction Lower values are more permissive (include more early samples); higher values are stricter. If the threshold is never reached, all weights are returned as-is with a warning.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
1D tensor with the same values as |
Examples:
>>> import torch
>>> from spectre.data import weights_ess_filter
>>> weights = torch.exp(torch.randn(500))
>>> filtered = weights_ess_filter(weights, threshold=0.3)
>>> (filtered == 0).sum() >= 0 # some early samples may be zeroed
tensor(True)
Source code in spectre/data/dataset_weights.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |