Evaluation¶
Import from xflow.evaluation. The runner uses PyTorch inference mode and
restores the model’s previous training/evaluation mode after the run.
- xflow.evaluation.runner.run_evaluation(model, dataset, device, hooks=None, unpack_batch=default_unpack_batch, forward_fn=default_forward, max_batches=None, strict_hook_errors=True)¶
Run inference over an iterable of batches. Default batch forms are
(x,),(x, y), or a dictionary containinginputsand optionaltargetsandmetadata. Supplyunpack_batchfor a different format. Returns anEvalContextcontaining sample/batch counts and hook state.A
BaseEvalHooksubclass can implementon_start(ctx),on_batch(ctx, batch), andon_end(ctx).EvalBatchcontains inputs, predictions, targets, and metadata; tensor outputs are detached to CPU. Setctx.stop = Trueto stop processing further batches.
For small datasets, InMemoryCollector collects per-sample results:
from xflow.evaluation import InMemoryCollector, run_evaluation
collector = InMemoryCollector()
context = run_evaluation(model, val_loader, "cpu", hooks=[collector])
predictions = [sample["predictions"] for sample in collector.samples]
This example continues from the model and loader in Overview.