Inside Algorithm¶
The inside algorithm computes, by dynamic programming over CKY spans, the semiring sum over all derivations of a span of the product of their weights (Goodman, 1999). This module computes these inside weights in log space. The weight of the whole sentence is its log probability only when the grammar is a normalized PCFG.
A rule of probability zero has the log weight \(-\infty\) exactly, so a sentence without a derivation scores \(-\infty\). The split points of each premise pair are summed before the rules apply, as in the span plans, so no product over categories and split points together is kept. The derivative of \(\log P\) in a probability \(p\) is \((\partial P / \partial p) / P\), and the backward pass computes \(\partial P / \partial p\) by the outside pass in the log semiring, as a sum of products of outside and inside weights with no factor \(p\). It is thus exact where \(p\) is zero, where differentiating \(\log p\) would multiply zero by an infinite slope, and it keeps only the chart. A sentence without a derivation has derivative zero.
inside
¶
CKY inside algorithm for probabilistic context-free grammars.
Implements the inside algorithm as a differentiable PyTorch module, enabling end-to-end gradient-based learning of PCFG parameters.
A PCFG is specified by two stochastic morphisms:
binary : N -> N * N: binary production probabilities. For each nonterminal A,binary[A, B, C]is the probability of the rule A -> B C.lexical : N -> T: terminal production probabilities. For each nonterminal A,lexical[A, t]is the probability of the rule A -> t.
The inside algorithm computes, for each nonterminal A and span (i, j) of the input sentence:
beta(A, i, j) = P(w_i ... w_{j-1} | A)
The sentence log-probability is log beta(start, 0, L) where
start is the start symbol index (default 0).
All computation is done in log-space for numerical stability, using logsumexp for marginalization. This preserves gradient flow for learning rule probabilities end-to-end.
Unary rules
An optional unary morphism adds rules A -> B. A chain of them
derives a category over a span from another over the same span, so
each chart cell is closed once under the closure matrix
U* = (I - U)^{-1} of Stolcke (1995), section 4.5.2, which sums the
probability of every chain exactly once.
What it computes
The algorithm computes a function from nonempty terminal strings to their inside weights under the start symbol: the sum, over the parse trees of the string, of the product of the weights of the rules in the tree. When the rule weights of each nonterminal form a probability distribution over its productions, this is the string's probability.
Examples:
>>> from quivers.core.objects import FinSet, ProductSet
>>> from quivers.stochastic.morphisms import StochasticMorphism
>>> N = FinSet(name="N", cardinality=5)
>>> T = FinSet(name="T", cardinality=10)
>>> binary = StochasticMorphism(N, ProductSet(N, N))
>>> lexical = StochasticMorphism(N, T)
>>> cky = InsideAlgorithm(binary, lexical, start=0)
>>> tokens = torch.randint(0, 10, (4, 6)) # batch=4, length=6
>>> log_probs = cky(tokens) # (4,)
InsideAlgorithm
¶
Bases: Module
CKY inside algorithm for differentiable PCFG parsing.
Computes sentence log-probabilities under a PCFG defined by binary and lexical production rules, both expressed as stochastic morphisms.
| PARAMETER | DESCRIPTION |
|---|---|
binary
|
Binary production rules. Must be a morphism
TYPE:
|
lexical
|
Lexical (terminal) production rules. Must be a morphism
TYPE:
|
start
|
Index of the start symbol in N (default 0).
TYPE:
|
unary
|
Optional unary production rules, a morphism
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the morphisms have incompatible types. |
Notes
The closure exists only when the spectral radius of U is below
one; a row-stochastic U (every category rewrites by some unary
rule with probability one) has spectral radius one, and parsing
with it raises DivergentClosureError.
Source code in src/quivers/stochastic/inside.py
n_nonterminals
property
¶
Number of nonterminal symbols.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The count. |
n_terminals
property
¶
Number of terminal symbols.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The count. |
forward
¶
Compute sentence log-probabilities via the inside algorithm.
| PARAMETER | DESCRIPTION |
|---|---|
tokens
|
Integer tensor of terminal indices. Shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Log-probability of each sentence under the grammar.
Shape |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the sentences are empty. |
DivergentClosureError
|
If the unary rules' closure diverges. |
Source code in src/quivers/stochastic/inside.py
inside_chart
¶
Compute the full inside chart (for analysis/debugging).
| PARAMETER | DESCRIPTION |
|---|---|
tokens
|
Integer tensor of terminal indices. Shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The full inside chart in log-space. Shape
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the sentences are empty. |
Source code in src/quivers/stochastic/inside.py
__repr__
¶
The sizes of the grammar.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The representation. |
Cited sources¶
Joshua Goodman. Semiring Parsing. Computational Linguistics, 25(4):573–606, 1999. URL: https://aclanthology.org/J99-4004/.