expansion¶
expansion
¶
Generable-set expansion, growing what a flow model can reach.
Distribution matching teaches a flow to reproduce its training density. Set expansion asks a different question, which valid designs can the model generate at all? This package implements Active Flow Expansion (ActFlow, De Santi et al., 2026, arXiv:2606.08802), which fine-tunes a pre-trained flow on self-generated, verifier-labelled samples chosen by active exploration in the flow's own noised representation.
Modules:
representation, the feature mapphi_s(hidden activations of the velocity field at noise levels).uncertainty, verifier-uncertainty estimators overphi_s, including the closed-form kernel posterior standard deviation (Eq. 10).guidance, the uncertainty tiltp_theta * exp(sigma / beta)(Eq. 9) exposed as aLikelihoodsoPosteriorSolvercan sample it.actflow, the round loop of Algorithm 1.metrics, histogram generable-set coverage for 2D problems.
The signed replay objective lives with the other losses as
deltaflow.losses.SignedReplayFlowMatchingLoss.
ActFlow
¶
ActFlow(model, verifier, optimizer, sample_shape, representation=None, uncertainty=None, loss=None, base_solver=None, tweedie=None, beta=0.1, guidance_scale=1.0, grad_normalize=False, n_steps=50, finetune_steps=100, batch_size=64, min_accepted=0, feature_batch_size=1024, cond=None)
Active Flow Expansion, verifier-guided growth of a flow's generable set.
Runs Algorithm 1 of De Santi et al. (2026). At round \(t\),
The representation \(\phi^{\theta_t}_s\) is re-read from the current model
every round, so the uncertainty refit uses the model's evolving features,
as in the paper. beta=math.inf turns off the tilt and gives the
self-training baseline (REC-F in the paper when alpha=0).
References
De Santi et al., "Active Flow Expansion for Out-of-Distribution Discovery: from Theory to Molecules" (2026), https://arxiv.org/abs/2606.08802.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
pre-trained velocity field (an |
required |
verifier
|
Callable[[Tensor], Any]
|
black-box validity oracle |
required |
optimizer
|
Optimizer
|
optimizer over |
required |
sample_shape
|
Sequence[int]
|
shape of one design without the batch axis, e.g.
|
required |
representation
|
Optional[FlowRepresentation]
|
feature map \(\phi_s\). Defaults to
|
None
|
uncertainty
|
Optional[BaseUncertainty]
|
verifier-uncertainty estimator. Defaults to the paper's
linear-kernel |
None
|
loss
|
Optional[SignedReplayFlowMatchingLoss]
|
continued pre-training objective. Defaults to
|
None
|
base_solver
|
Optional[BaseSolver]
|
integrator wrapped by |
None
|
tweedie
|
Optional[BaseTweedie]
|
Tweedie decomposition matching the training path. Defaults
to |
None
|
beta
|
float
|
KL weight \(\beta\). Smaller explores harder. |
0.1
|
guidance_scale
|
float
|
step size on the tilt gradient ( |
1.0
|
grad_normalize
|
bool
|
forwarded to |
False
|
n_steps
|
int
|
ODE steps per self-generated batch. |
50
|
finetune_steps
|
int
|
gradient steps of the flow update per round. |
100
|
batch_size
|
int
|
minibatch size drawn from each replay buffer. |
64
|
min_accepted
|
int
|
warm-up, the flow is not updated until the accepted buffer holds at least this many samples. |
0
|
feature_batch_size
|
int
|
chunk size for reading features of the history. |
1024
|
cond
|
Optional[Dict[str, Any]]
|
conditioning forwarded unchanged to the model in sampling, features, and the loss. It must not depend on the batch size. |
None
|
Source code in deltaflow/expansion/actflow.py
features
¶
Read \(\phi_s(x)\) from the current model in chunks.
Source code in deltaflow/expansion/actflow.py
fit_uncertainty
¶
Refit \(\sigma_t\) on the full history with the current features.
Source code in deltaflow/expansion/actflow.py
guidance
¶
self_generate
¶
Draw n designs from the uncertainty-tilted model (Eq. 9).
Source code in deltaflow/expansion/actflow.py
query
¶
Label x with the verifier and return a bool tensor [B].
Source code in deltaflow/expansion/actflow.py
update_flow
¶
Signed replay continued pre-training. Returns the last loss or None.
Source code in deltaflow/expansion/actflow.py
step
¶
Run one round of Algorithm 1 with a batch of n_samples queries.
Source code in deltaflow/expansion/actflow.py
run
¶
Run n_rounds rounds, calling callback(round) after each.
Source code in deltaflow/expansion/actflow.py
ActFlowRound
dataclass
¶
ActFlowRound(round, samples, labels, acceptance_rate, mean_uncertainty, n_accepted, n_rejected, finetuned, loss, alpha)
Summary of one ActFlow round.
Attributes:
| Name | Type | Description |
|---|---|---|
round |
int
|
zero-based round index. |
samples |
Tensor
|
the self-generated queries of this round. |
labels |
Tensor
|
their verifier labels (bool, |
acceptance_rate |
float
|
fraction of this round's queries accepted. |
mean_uncertainty |
float
|
mean \(\sigma_t(\phi_s(x))\) of the queries under the uncertainty fitted at the start of the round. |
n_accepted |
int
|
size of the accepted buffer after the round. |
n_rejected |
int
|
size of the rejected buffer after the round. |
finetuned |
bool
|
whether the flow was updated ( |
loss |
Optional[float]
|
last signed-replay loss of the update, if any. |
alpha |
float
|
last calibrated repulsion weight \(\alpha_t\). |
UncertaintyGuidance
¶
Likelihood adapter for the uncertainty tilt \(p_\theta \exp(\sigma_t / \beta)\).
Implements ActFlow's active-exploration objective
by returning \(-\sigma_t(\phi_s(\hat{x}_1)) / \beta\) as a negative
log-likelihood. Pass it to PosteriorSolver as likelihood. The
solver differentiates it through FlowRepresentation and the Tweedie
estimate \(\hat{x}_1\) back to the current state \(x_t\).
References
De Santi et al., "Active Flow Expansion for Out-of-Distribution Discovery: from Theory to Molecules" (2026), https://arxiv.org/abs/2606.08802.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
representation
|
FlowRepresentation
|
the feature map \(\phi_s\). |
required |
uncertainty
|
BaseUncertainty
|
a fitted |
required |
beta
|
float
|
KL weight \(\beta > 0\). |
0.1
|
cond
|
Optional[Dict[str, Any]]
|
conditioning forwarded unchanged to the velocity field inside
\(\phi_s\) (the |
None
|
Source code in deltaflow/expansion/guidance.py
score
¶
FlowRepresentation
¶
FlowRepresentation(model, s=0.9, layer=None, interpolant=None, pool='mean', normalize=True, time_scale=1.0)
Feature map \(\phi_s : \mathcal{X} \to \mathcal{Z}_s\) read from a velocity field.
A design \(x\) is first noised to level \(s\) on the training path,
(for the default linear path), then passed through the velocity field
\(v_\theta(x_s, s)\). The activation of layer is captured by a
temporary forward hook and pooled to one vector per sample. With
layer=None the velocity output itself is used as the representation.
Pooling rule for the captured activation \(h\), shaped [B, ...].
[B, D]is returned unchanged.[B, N, D](token sequences, e.g. a DiT block) is averaged over theNtokens.[B, C, *spatial](feature maps) is averaged over the spatial axes.
Use pool="flatten" to keep every coordinate instead.
Because \(x_0\) is random, \(\phi_s\) is stochastic for \(s < 1\). Pass
noise explicitly for a deterministic evaluation.
References
De Santi et al., "Active Flow Expansion for Out-of-Distribution Discovery: from Theory to Molecules" (2026), https://arxiv.org/abs/2606.08802.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Callable
|
velocity field |
required |
s
|
float
|
noise level \(s \in (0, 1]\) at which features are read. |
0.9
|
layer
|
Optional[Union[str, Module]]
|
sub-module (or its dotted name, resolved with
|
None
|
interpolant
|
Optional[BaseInterpolant]
|
path used to noise \(x\) to level \(s\). Defaults to
|
None
|
pool
|
str
|
|
'mean'
|
normalize
|
bool
|
L2-normalise each feature vector. This keeps the linear kernel bounded, \(k(x, x) \le 1\), as the confidence-sequence theory assumes. |
True
|
time_scale
|
float
|
multiplies \(s\) before it reaches the model, matching
|
1.0
|
Source code in deltaflow/expansion/representation.py
BaseUncertainty
¶
Bases: ABC
Base class for an uncertainty score sigma(z) over features z.
Subclasses implement fit (refit from the full query history) and
__call__ (a per-sample score, differentiable in z so it can drive
sampling-time guidance).
fit
abstractmethod
¶
Refit on queried features [t, D] and their verifier labels [t].
KernelUncertainty
¶
Bases: BaseUncertainty
Posterior standard deviation of kernel regression (ActFlow Eq. 10).
Given queried features \(Z_t = (z_1, \dots, z_t)\) with kernel matrix \((K_t)_{ij} = k(z_i, z_j)\), the uncertainty of a new point is
This is the posterior variance of Bayesian linear regression under Gaussian observations in the feature space of \(k\). With no queries it reduces to the prior \(k(z, z)\).
For the linear kernel \(k(z, z') = \langle z, z' \rangle\) the push-through identity gives the equivalent primal form
which costs \(\mathcal{O}(D^3)\) instead of \(\mathcal{O}(t^3)\). It is selected automatically once \(t > D\).
The score is differentiable in \(z\) (the stored Cholesky factor is a
constant), so it can be back-propagated through FlowRepresentation
to the sampling state. Linear algebra runs in float64 for stability and
the result is cast back to the input dtype.
References
De Santi et al., "Active Flow Expansion for Out-of-Distribution Discovery: from Theory to Molecules" (2026), https://arxiv.org/abs/2606.08802. Rasmussen and Williams, "Gaussian Processes for Machine Learning" (2006), Ch. 2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel
|
str
|
|
'linear'
|
reg
|
float
|
regulariser \(\lambda > 0\) (observation-noise variance). |
1.0
|
lengthscale
|
float
|
RBF lengthscale \(\ell\). Ignored for the linear kernel. |
1.0
|
max_points
|
Optional[int]
|
if set, only the most recent |
None
|
Source code in deltaflow/expansion/uncertainty.py
variance
¶
Return \(\sigma_t^2(z)\) with shape [B].
Source code in deltaflow/expansion/uncertainty.py
mean
¶
Posterior mean of the label regression, a cheap validity surrogate.
Returns zeros if no labels were provided to fit.
Source code in deltaflow/expansion/uncertainty.py
generable_coverage
¶
Histogram estimate of generable-set coverage for 2D samples.
Samples are binned on a bins x bins grid over bounds. A bin
\(b\) is generable when its density estimate
\(\hat{p}(b) = n_b / (N\,|b|)\) is at least \(\tau\), giving the
plug-in generable set \(\hat{\Omega}^\tau\). A bin is valid when
valid_fn accepts its centre, giving \(\hat{\Omega}^*\). Then
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
Tensor
|
|
required |
valid_fn
|
Callable[[Tensor], Tensor]
|
verifier on |
required |
bounds
|
Bounds
|
|
(-3.5, 3.5)
|
bins
|
int
|
bins per axis. |
100
|
tau
|
float
|
density threshold \(\tau\). |
0.01
|
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
A dict with |
Dict[str, float]
|
|