Loss registry¶
A loss attachment
attaches a weighted scalar training objective to a declared component, such as the
reconstruction loss,
which penalizes a low probability of the original tree. Each compiled module has a LossRegistry
that maps attachment sites to weighted loss functions. The sites are program, deduction, encoder,
decoder, and rule names, and the chart and global sites.
The training driver calls LossRegistry.evaluate(env) to sum the
registered losses. evaluate_on(kind, target, env, rule_deduction)
returns the weighted partial sum for one attachment site.
The QVR compiler compiles loss bodies as let-expression closures with
signature (env) -> Tensor. For a global loss, env contains the
compiled module's program, deduction, encoder, and decoder bindings as
top-level names. For a loss attached to a rule, it also contains "rule",
"deduction", "antecedents", "conclusion", and "weight"
keys populated by the callback the agenda calls when a rule fires.
losses
¶
Loss attachment registry.
Holds the table of weighted scalar losses declared in a compiled module, keyed by attachment site, so the training driver can evaluate the right ones at the right point in the training step.
LossEntry
didactic-model
¶
LossEntry(*, name: str, body: LossBody = field(opaque=True, extras=OPAQUE_BY_IDENTITY), weight: LossWeight | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY), attachment_kind: AttachmentKind = 'global', target: str | None = None, rule_deduction: str | None = None)
Bases: ValueModel
One registered loss.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
Diagnostic identifier (the DSL name).
TYPE:
|
body |
Computes the scalar loss given a training-step environment.
TYPE:
|
weight |
Computes a scalar multiplier given the same environment, or
TYPE:
|
attachment_kind |
Where this loss fires (see
TYPE:
|
target |
Name of the attachment target (program / deduction / encoder / decoder / chart / rule).
TYPE:
|
rule_deduction |
For
TYPE:
|
LossRegistry
didactic-model
¶
LossRegistry(*, entries: list[LossEntry] = field(default_factory=list, opaque=True, extras=OPAQUE_BY_IDENTITY))
Bases: ValueModel
All losses declared in a compiled module.
| ATTRIBUTE | DESCRIPTION |
|---|---|
entries |
The registered losses in registration order.
TYPE:
|
add
¶
add(entry: LossEntry) -> None
Register a loss.
| PARAMETER | DESCRIPTION |
|---|---|
entry
|
The loss to append to the registry.
TYPE:
|
by_attachment
¶
by_attachment(kind: AttachmentKind, target: str | None = None) -> list[LossEntry]
Return the losses attached to a kind of construct.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
The attachment kind to select.
TYPE:
|
target
|
When given, only losses attached to the target of this name are returned.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[LossEntry]
|
The matching losses in registration order. |
Source code in src/quivers/structural/losses.py
evaluate
¶
Sum all registered losses, weighted, under env.
| PARAMETER | DESCRIPTION |
|---|---|
env
|
The training-step environment passed to every loss body and weight; empty when omitted.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The scalar weighted sum of the losses. |
Source code in src/quivers/structural/losses.py
evaluate_on
¶
evaluate_on(kind: AttachmentKind, target: str | None = None, env: Mapping[str, TrainEnv] | None = None, rule_deduction: str | None = None) -> Tensor
Sum only the losses whose attachment matches the filter.
kind selects the attachment kind; target filters by
attachment target (the program / deduction / encoder /
decoder / rule name); rule_deduction further narrows the
"rule" kind to a specific enclosing deduction.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
The attachment kind to select.
TYPE:
|
target
|
When given, the attachment target the losses must name.
TYPE:
|
env
|
The training-step environment passed to every loss body and weight; empty when omitted.
TYPE:
|
rule_deduction
|
When given, the enclosing deduction a
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The scalar weighted sum of the matching losses. |
Source code in src/quivers/structural/losses.py
Type aliases¶
Structural compression: signatures, encoders, decoders, losses.
Runtime substrate for QVR signatures, encoders, decoders, and attached losses: second-order binding signatures with their generated term operations, and a uniform algebraic interface for compressing structured objects to fixed-length vectors and decoding them under a learned distribution.
LossBody
¶
LossBody = Callable[[Mapping[str, 'TrainEnv']], Tensor]
A loss term: maps the training environment to a scalar loss.
LossWeight
¶
LossWeight = Callable[[Mapping[str, 'TrainEnv']], Tensor]
A loss weight: maps the training environment to a scalar multiplier.
TrainEnv
¶
A value of the training environment a loss body reads.
The environment holds compiled encoders, decoders, and deductions
(each an nn.Module), input tensors, structured-term
observations, and raw scalar or tensor targets.
AttachmentKind
¶
The kind of construct a registered loss is attached to.