Base for all model-forward output dataclasses.
Provides a uniform protocol: outputs are simultaneously dataclass-like
(named fields, dict-style lookup) and sequence-like (iteration / tuple
indexing, with None fields silently elided). Each concrete
subclass adds a task-specific set of Tensor (or
tuple[Tensor, ...]) fields.
Notes
Subclasses must be decorated with @dataclass so
dataclasses.fields works. Iteration order follows field
declaration order, and None fields are skipped — this matches the
behaviour expected by callers that destructure outputs with
logits, loss = model(x).
Direct instantiation of ModelOutput itself is not useful (no
fields) — always subclass and add @dataclass.
Examples
>>> # ImageClassificationOutput is a typical subclass
>>> out = ImageClassificationOutput(logits=lucid.randn(1, 10))
>>> out["logits"].shape # dict-style access
(1, 10)
>>> first, = out # tuple-style unpacking
>>> first is out.logits
TrueUsed by 4
Instance methods
4(name, value) pairs of non-None fields.
Names of fields whose value is not None, in declaration order.
Return a tuple of non-None field values in declaration order.
Examples
>>> out = BaseModelOutput(last_hidden_state=h)
>>> out.to_tuple()
(h,)Values of non-None fields, in declaration order.
Dunder methods
3Look up a field by name (str) or position (int).
Parameters
idxint or strNone
iteration.Returns
TensorThe requested field value.
Raises
KeyErroridx is a string naming a missing / None field.IndexErroridx is an integer outside the non-None range.Yield each non-None field value in declaration order.
Return the number of non-None fields.