Scan Morphism¶
ScanMorphism(cell) realizes the iterated Kleisli composition of a
per-step cell over a sequence. ScanMorphism.rsample(x) applies the
kernel at each step while threading the hidden state.
ScanMorphism.log_joint(x, hidden_states) sums the per-step log
density. It accepts the hidden-state trajectory either as a positional
tensor or as a {state_key: tensor} dictionary. The default
state_key is "h".
scan
¶
Scan combinator: temporal recurrence over sequences.
A ScanMorphism wraps a recurrent cell and applies it across a sequence, threading hidden state from one time step to the next. This implements the standard RNN pattern:
h_t = cell(x_t, h_{t-1})
where cell : A * H -> H is a morphism (either a plain ContinuousMorphism or a MonadicProgram) whose domain is a product of the per-timestep input space A and the hidden state space H, and whose codomain is H.
Given a cell : A * H -> H, scan(cell) produces a morphism
A -> H that, at runtime:
- Expects a 3D input tensor of shape (batch, seq_len, dim_A).
- Initializes hidden state h_0 (zeros or a learned parameter).
- At each step t, concatenates x[:, t, :] with h to form the cell input, then calls cell.rsample to produce the new h.
- Returns the final hidden state h_T of shape (batch, dim_H).
The scan's type in the categorical framework is:
scan(f : A x H -> H) : A -> H
where the sequence structure is implicit in the tensor's time dimension, following standard neural network conventions.
Initialization strategies
"zeros": h_0 = 0 (default)."learned": h_0 is a learnable nn.Parameter.
Examples:
>>> from quivers.continuous.spaces import Euclidean, ProductSpace
>>> from quivers.continuous.families import ConditionalNormal
>>> A = Euclidean(name="input", dim=32)
>>> H = Euclidean(name="hidden", dim=64)
>>> cell = ConditionalNormal(ProductSpace(A, H), H, scale=0.1)
>>> scanned = ScanMorphism(cell, init="zeros")
>>> scanned.domain # Euclidean(name="input", dim=32)
>>> scanned.codomain # Euclidean(name="hidden", dim=64)
>>> x = torch.randn(8, 10, 32) # batch=8, seq_len=10, input_dim=32
>>> h = scanned.rsample(x) # (8, 64)
ScanMorphism
¶
ScanMorphism(cell: ContinuousMorphism, init: str = 'zeros')
Bases: ContinuousMorphism
Temporal scan: apply a recurrent cell across a sequence.
Wraps a cell morphism f : A * H -> H and produces a morphism
A -> H that iterates over the time dimension of a 3D input
tensor, threading hidden state forward.
This implements standard RNN-style recurrence::
h_0 = init
h_t = cell(concat(x_t, h_{t-1})) for t = 1..T
The scan returns the final hidden state h_T.
| PARAMETER | DESCRIPTION |
|---|---|
cell
|
The recurrent cell. Must have a product domain
TYPE:
|
init
|
Initialization strategy for h_0. One of
TYPE:
|
Source code in src/quivers/continuous/scan.py
rsample
¶
Run the cell across the time dimension of x.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Input sequence, in either layout
TYPE:
|
sample_shape
|
Additional leading sample dimensions (applied to the cell's rsample at the first time step only). |
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Final hidden state. Shape |
Source code in src/quivers/continuous/scan.py
base_dimension
¶
base_dimension(x: Tensor) -> int | None
One cell's worth of coordinates per time step of the input.
The recurrence draws once per position, so its coordinate
budget is the sequence length times the cell's own. The length
is read off the input rather than declared, because
scan(cell) : A -> H says nothing about how many positions a
given input carries, and it is read through
_as_sequence
so a folded (batch, seq_len * input_dim) input reports the
whole sequence's budget rather than one step's. Under-reporting
it would hand
push_base
a block too short to run the recurrence on, and a chain that
sliced its coordinates by that count would give every later
factor the wrong ones.
Source code in src/quivers/continuous/scan.py
push_base
¶
Run the recurrence on supplied coordinates instead of draws.
Time step t reads the t-th block of base, so the
trajectory is a deterministic function of the coordinates and
the input, and the same coordinates always produce the same
final state.
Source code in src/quivers/continuous/scan.py
reference_trajectory
¶
The states :math:h_1, \ldots, h_T this kernel scores at y.
The recurrence's own deterministic skeleton, re-anchored at the
observed final state: for :math:t < T the state is the cell's
image of the base measure's origin, and :math:h_T is y.
Prefix states use push_base at zero coordinates; the final
state is y. The resulting trajectory is deterministic.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Input sequence, in either layout
TYPE:
|
y
|
Observed final state. Shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Shape |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the cell declares no reparameterization, so the prefix has no canonical states to take. |
Source code in src/quivers/continuous/scan.py
log_prob
¶
Log-density of the scan's trajectory ending at y.
scan(cell) denotes a Kleisli morphism
:math:\mathbf{x}_{1:T} \to \mathcal{G}(h_T) whose density at
:math:h_T marginalizes every intermediate state:
.. math::
p(h_T \mid x_{1:T}) = \int
p(h_T \mid x_T, h_{T-1})
\prod_{t<T} p(h_t \mid x_t, h_{t-1})
\, dh_{1:T-1}.
The implementation scores the fixed trajectory returned by
reference_trajectory with log_joint:
.. math::
\sum_{t=1}^{T} \log p(h_t \mid x_t, h_{t-1}),
\qquad h_T = y .
Floating-point reassociation in the recurrent prefix may be amplified by later transitions. Numerical comparisons should use the same cell implementation.
This is the joint density of the fixed trajectory, not the marginal density of its endpoint.
If the cell has no conditional density, this method returns a zero contribution.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Input sequence, in either layout
TYPE:
|
y
|
Final hidden state. Shape
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Shape |
Source code in src/quivers/continuous/scan.py
log_joint
¶
log_joint(x: Tensor, hidden_states: 'torch.Tensor | dict[str, torch.Tensor]', *, state_key: str = 'h') -> Tensor
Joint log-density given all intermediate hidden states.
Computes: log p(h_1, ..., h_T | x_{1:T}) = sum_t log p(h_t | x_t, h_{t-1})
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Input sequence, in either layout
TYPE:
|
hidden_states
|
All hidden states including final, shape
|
state_key
|
Dict key under which the hidden-state tensor is
looked up when
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Joint log-density. Shape |