Productions¶
A deduction with a lexicon is a grammar. Its rules and its lexicon entries are productions, each
rewriting the category its conclusion names, its left-hand side. Under the normalize=lhs
deduction option, the productions that share a left-hand side form a probability distribution, as
in a probabilistic context-free grammar
(Booth and Thompson, 1973; Chi and Geman, 1998). The
deduction guide defines the
left-hand side of each rule form and the deductions the option refuses.
DeductionSystem.production_groups
returns the groups, and
DeductionSystem.production_log_probs
the log-probability of each production.
productions
¶
Productions of a weighted grammar, grouped by left-hand side.
A deduction with a lexicon is a grammar: each rule and each lexicon entry is a production that rewrites the category its conclusion names (its left-hand side) into the items its premises name, or into a word. A probabilistic context-free grammar gives the productions that share a left-hand side a probability distribution, so that
for every category \(A\) (Chi and Geman, 1998). ProductionGroup records
one left-hand side and its productions in a fixed order, and
ProductionWeights maps the grammar's free log-weights to the
per-left-hand-side log-probabilities a chart reads: each group's
log-weights pass through a
log_softmax,
so a group's probabilities sum to one whatever the free weights are.
ProductionKey
¶
A production's address: its kind and its index among the rules, or among the expanded lexicon entries.
Production
didactic-model
¶
Bases: Model
One production of a grammar: a rule or an expanded lexicon entry.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
Whether the production is a rule or a lexicon entry.
TYPE:
|
index
|
The rule's position among the deduction's rules, or the entry's position among the expanded lexicon entries (one per word and candidate category, inline entries first, then the rows of a lexicon file).
TYPE:
|
label
|
The production's name within its group: the rule's name, or the entry's word in double quotes.
TYPE:
|
learnable
|
Whether the production's log-weight is a learned parameter; a production that is not learnable has log-weight zero.
TYPE:
|
key
property
¶
key: ProductionKey
The production's address.
| RETURNS | DESCRIPTION |
|---|---|
ProductionKey
|
Its kind and index. |
ProductionGroup
didactic-model
¶
ProductionGroup(*, label: str, productions: tuple[Production, ...])
Bases: Model
The productions that rewrite one left-hand side.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The left-hand side: the category of a span item, or an item with
its position slots written
TYPE:
|
productions
|
The productions, rules in declaration order, then lexicon entries in expansion order.
TYPE:
|
ProductionWeights
¶
ProductionWeights(groups: tuple[ProductionGroup, ...], free_weight: Callable[[Production], Tensor], *, normalize: bool)
The per-left-hand-side weights a grammar's chart reads.
The object holds the grammar's production groups and a reader for
each production's free log-weight (its learned parameter, or zero).
table turns the free log-weights into the log-weights the chart
reads: under per-left-hand-side normalization, each group's vector
passes through log_softmax; otherwise each production keeps its
free log-weight. A normalized grammar may instead take its production
probabilities from the caller, one probability vector per left-hand
side with several productions (bound_groups), as a program does
when it places a prior on the grammar's weights. A run of the
deduction computes the table once and makes it active, so every
rule firing and lexical axiom of that run reads one consistent
table.
| PARAMETER | DESCRIPTION |
|---|---|
groups
|
The production groups, one per left-hand side.
TYPE:
|
free_weight
|
The free log-weight of a production, a scalar tensor.
TYPE:
|
normalize
|
Whether each group's log-weights are normalized.
TYPE:
|
Source code in src/quivers/stochastic/productions.py
bound_groups
property
¶
bound_groups: tuple[ProductionGroup, ...]
The groups whose probabilities a caller supplies.
| RETURNS | DESCRIPTION |
|---|---|
tuple[ProductionGroup, ...]
|
The groups with more than one production; a production alone in its group has probability one. |
group_vector
¶
group_vector(group: ProductionGroup) -> Tensor
The free log-weights of one group, in production order.
| PARAMETER | DESCRIPTION |
|---|---|
group
|
The group.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
A vector with one entry per production. |
Source code in src/quivers/stochastic/productions.py
check_probabilities
¶
check_probabilities(probabilities: Mapping[str, Tensor]) -> None
Refuse supplied probabilities that do not fit the groups.
| PARAMETER | DESCRIPTION |
|---|---|
probabilities
|
One probability vector per bound group, by group label.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the grammar is not normalized, a bound group is missing,
a label names no bound group, a vector's shape is not
|
Source code in src/quivers/stochastic/productions.py
table
¶
table(probabilities: Mapping[str, Tensor] | None = None) -> dict[ProductionKey, Tensor]
The log-weight of every production.
| PARAMETER | DESCRIPTION |
|---|---|
probabilities
|
Production probabilities to read instead of the free
log-weights, one vector per bound group; each is
renormalized, so its logarithm passes through the same
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[ProductionKey, Tensor]
|
One scalar per production: its group's |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the probabilities do not fit the groups
( |
Source code in src/quivers/stochastic/productions.py
active
¶
active(table: dict[ProductionKey, Tensor]) -> Iterator[None]
Serve one table to every read inside the block.
| PARAMETER | DESCRIPTION |
|---|---|
table
|
The table, from
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
None
|
Control, with the table active. |
Source code in src/quivers/stochastic/productions.py
weight
¶
weight(key: ProductionKey) -> Tensor
The log-weight one production contributes.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The production's address.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The entry of the active table, or of a fresh table when none is active. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If no group holds the production. |
Source code in src/quivers/stochastic/productions.py
log_probs
¶
The table by group and production label.
| PARAMETER | DESCRIPTION |
|---|---|
probabilities
|
Production probabilities to read instead of the free
log-weights, as for
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, dict[str, Tensor]]
|
For each left-hand side, each production's log-weight. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the probabilities do not fit the groups. |
Source code in src/quivers/stochastic/productions.py
Cited sources¶
Taylor L. Booth and Richard A. Thompson. Applying Probability Measures to Abstract Languages. IEEE Transactions on Computers, C-22(5):442–450, 1973. URL: https://doi.org/10.1109/T-C.1973.223746, doi:10.1109/T-C.1973.223746.
Zhiyi Chi and Stuart Geman. Estimation of Probabilistic Context-Free Grammars. Computational Linguistics, 24(2):299–305, 1998. URL: https://aclanthology.org/J98-2005/.