Inference¶
photosvi.inference.points.train_points_svi ¶
train_points_svi(model, y, inj_t, *, lr=0.001, steps=1000, device='cpu', verbose=True, log_every=100, seed=None, keep_best=True)
Fit a SingleTraceModel by relaxed-discrete SVI. Returns the per-element ELBO loss history.
y is [B, T] observed traces sharing the injection bin inj_t. Optimizes
Trace_ELBO(max_plate_nesting=2) (sequences x time plates) with ClippedAdam.
The relaxed ELBO loss is noisy and backslides off its minimum, so by default
(keep_best=True) the param store is snapshotted at every new best and
restored at the end -- the returned model carries the best-checkpoint
params, not the last step's. Set keep_best=False to leave the store at the
last step (the raw trajectory, for debugging). This is the best-checkpoint
analogue of standard early stopping: no run is halted early (there is no
held-out split to stop on), but the fit is read off the minimum, not the tail.
Returns {"elbo": [...], "best_loss": float, "best_step": int} -- the
per-element negative ELBO history (lower is better), the minimum loss, and
its 1-indexed step.
Source code in src/photosvi/inference/points/train.py
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 | |
Convergence¶
photosvi.inference.points.EstimandPlateau ¶
EstimandPlateau(tol=0.02, window=6, min_steps=5000)
Stop when the reported estimand has stopped moving.
Records a value at each probe and reports convergence once the estimand has
stopped drifting, which is not the same as it having stopped moving. At a
usable learning rate the iterate oscillates around the optimum rather than
creeping toward it -- measured at lr=0.01, beta_inj runs 0.714, 0.748, 0.698,
0.741, 0.739 while sitting on its answer. A test on the spread of those values
reads that as unconverged and never fires, while a slow creep at lr=0.001 passes
it easily and stops the fit 0.09 short. So the criterion compares the mean of
the first half of the window against the second: no systematic movement between
them, whatever the oscillation amplitude.
Report plateau_mean rather than the last probe. Once the iterate is orbiting,
the final value is one draw from the orbit and the average over it is a better
estimate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tol
|
movement of the window's centre, on the log-rate axis, that still counts
|
as settled. The default of 0.02 is well inside the estimator's own sampling spread across seeds (0.13-0.16), so it is a statement about the optimiser having stopped, not a claim about precision. |
0.02
|
window
|
probes held. Must be even enough to halve; 6 gives three per half.
|
|
6
|
min_steps
|
a floor below which convergence is never declared. Early in a fit
|
|
5000
|
Source code in src/photosvi/inference/points/convergence.py
52 53 54 55 56 57 58 59 60 61 62 | |
drift_per_coordinate
property
¶
drift_per_coordinate
Per-coordinate movement of the window's centre between its halves.
plateau_mean
property
¶
plateau_mean
Mean over the window -- the value to report once converged.
Scalar for a scalar estimand; for a vector use plateau_means.
update ¶
update(step, value)
Record a probe; return True once the plateau condition holds.
value may be a scalar or a vector. The cohort estimand is mu_drug, one
coordinate per drug, and the criterion is then the worst coordinate's
drift: a fit is not finished because the drug that settled first has
settled. A scalar is the one-coordinate case of the same rule.
Source code in src/photosvi/inference/points/convergence.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
Device selection¶
photosvi.inference.points.resolve_device ¶
resolve_device(spec='auto', *, batch=1)
Pick a torch device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
"auto" (default), "cpu", "cuda", "cuda:1", or a `torch.device`. None is
|
treated as "auto". An explicit request for CUDA when none is available is an error, not a silent fallback: a run that was launched for the GPU and quietly took 60x longer on CPU is worse than one that refused to start. |
'auto'
|
batch
|
the number of sequences the fit will carry. Only consulted under "auto",
|
where it selects by the rule in the module docstring. |
1
|
Source code in src/photosvi/inference/points/device.py
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 | |
photosvi.inference.points.prepare ¶
prepare(model, y, device)
Move a model and its traces onto device together, and set CPU threading.
On CPU the fits are run one-per-process under an external fan-out, so a single torch thread per process is right: the math backends otherwise build an OpenMP pool per worker and oversubscribe the machine several times over. On GPU the thread count is irrelevant and left alone.
Source code in src/photosvi/inference/points/device.py
81 82 83 84 85 86 87 88 89 90 91 92 | |