Skip to content
class

ModelOutput

ModelOutput()

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

>>> import lucid
>>> from lucid.models import ImageClassificationOutput
>>> out = ImageClassificationOutput(logits=lucid.randn(1, 10))
>>> out["logits"].shape           # dict-style access
(1, 10)
>>> first, = out                  # tuple-style unpacking
>>> first is out.logits
True

Used by 25

… 13 more

Instance methods

5
fn

get

→Tensor or object
get(key: str, default: D | None = None)

The value of field key, or default when key is not a key.

Parameters

keystr
Field name.
defaultobject= None
Returned when key names no field or a field that is None (such a field is not a key, as keys and in agree). Default: None.

Returns

Tensor or object

The field's value, else default.

Examples

>>> import lucid
>>> from lucid.models import ImageClassificationOutput
>>> out = ImageClassificationOutput(logits=lucid.zeros(1, 10))
>>> out.get("logits").shape
(1, 10)
>>> out.get("loss") is None, out.get("loss", 0.0)
(True, 0.0)
fn

items

→list[tuple[str, Tensor]]
items()

(name, value) pairs of non-None fields.

fn

keys

→list[str]
keys()

Names of fields whose value is not None, in declaration order.

fn

to_tuple

→tuple[Tensor, ...]
to_tuple()

Return a tuple of non-None field values in declaration order.

Examples

>>> out = BaseModelOutput(last_hidden_state=h)
>>> out.to_tuple()
(h,)
fn

values

→list[Tensor]
values()

Values of non-None fields, in declaration order.

Dunder methods

3
dunder

__getitem__

→Tensor
__getitem__(idx: int | str)

Look up a field by name (str) or position (int).

Parameters

idxint or str
Field name or positional index into the non-None iteration.

Returns

Tensor

The requested field value.

Raises

KeyError
If idx is a string naming a missing / None field.
IndexError
If idx is an integer outside the non-None range.
dunder

__iter__

→Iterator[Tensor]
__iter__()

Yield each non-None field value in declaration order.

dunder

__len__

→int
__len__()

Return the number of non-None fields.