Program¶
Top-level probabilistic program definitions and execution.
program
¶
Program: compile a morphism expression into a trainable nn.Module.
Program
¶
Program(morphism: Morphism | ContinuousMorphism | Module | None = None)
Bases: Module
Wraps a morphism expression as a trainable nn.Module.
Traverses the morphism DAG, collects all learnable parameters and observed buffers, and provides a forward() that materializes the composed tensor.
Supports both discrete Morphism instances (which produce a
membership tensor via forward()) and ContinuousMorphism
instances (which expose rsample / log_prob).
| PARAMETER | DESCRIPTION |
|---|---|
morphism
|
The root morphism expression (possibly a composition tree).
TYPE:
|
Examples:
>>> from quivers import FinSet, morphism, Program
>>> X = FinSet(name="X", cardinality=3)
>>> Y = FinSet(name="Y", cardinality=4)
>>> f = morphism(X, Y)
>>> prog = Program(f)
>>> out = prog() # shape (3, 4)
Source code in src/quivers/program.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
morphism
property
¶
morphism: Morphism | ContinuousMorphism | Module | None
The underlying morphism expression, or None for a
morphism-less module (one declaring only signatures /
encoders / decoders / losses, or exporting a parametric
program template that has not been instantiated).
domain
property
¶
domain
Domain of the underlying morphism.
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the Program wraps no morphism (an uninstantiated parametric template or a structural-artifact container). |
codomain
property
¶
codomain
Codomain of the underlying morphism.
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the Program wraps no morphism (an uninstantiated parametric template or a structural-artifact container). |
rsample
¶
rsample(x: Tensor, sample_shape: Size = Size()) -> Tensor
Reparameterized sample (continuous programs only).
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Input tensor.
TYPE:
|
sample_shape
|
Extra leading sample dimensions.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Sampled output. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the underlying morphism is not continuous. |
Source code in src/quivers/program.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
log_prob
¶
log_prob(x: Tensor, y: Tensor) -> Tensor
Log-probability (continuous programs only).
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Input tensor.
TYPE:
|
y
|
Output tensor.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Log-probability of y given x. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the underlying morphism is not continuous. |
Source code in src/quivers/program.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
forward
¶
forward(n_steps: int | None = None) -> Tensor
Materialize the composed tensor (discrete programs only).
| PARAMETER | DESCRIPTION |
|---|---|
n_steps
|
If provided, sets the step count on all
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Tensor of shape |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the underlying morphism is continuous. |
Source code in src/quivers/program.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
log_membership
¶
log_membership() -> Tensor
Log of the membership tensor.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Log-membership values. Entries near 0 map to large negative values. |
Source code in src/quivers/program.py
261 262 263 264 265 266 267 268 269 270 271 | |
nll_loss
¶
nll_loss(domain_indices: Tensor, codomain_indices: Tensor) -> Tensor
Negative log-likelihood loss for observed (domain, codomain) pairs.
For each pair (x, y), computes -log(tensor[x, y]). Suitable when the morphism represents fuzzy membership and observed pairs should have high membership.
| PARAMETER | DESCRIPTION |
|---|---|
domain_indices
|
Integer tensor of shape (batch,) or (batch, n_domain_dims) indexing into the domain.
TYPE:
|
codomain_indices
|
Integer tensor of shape (batch,) or (batch, n_codomain_dims) indexing into the codomain.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar mean negative log-likelihood. |
Source code in src/quivers/program.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
bce_loss
¶
bce_loss(target: Tensor) -> Tensor
Binary cross-entropy between the morphism tensor and a target.
| PARAMETER | DESCRIPTION |
|---|---|
target
|
Target tensor of the same shape as the morphism tensor, with values in [0, 1].
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar BCE loss. |
Source code in src/quivers/program.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |