torchmodal.inference¶
torchmodal.inference ¶
torchmodal.inference ~~~~~~~~~~~~~~~~~~~~
Upward-Downward inference algorithm for MLNN (Appendix B.1).
The core inference procedure propagates truth bounds through the logical formula graph in two passes:
- Upward pass (leaves → root): Computes bounds bottom-up using differentiable operators following topological order.
- Downward pass (root → leaves): Refines bounds top-down using inverse operator constraints.
Each iteration can only tighten bounds (increase L or decrease U), creating a monotonic bounded sequence that converges to a unique fixed point for acyclic formula graphs.
Why the two passes are iterated rather than run once. A single
upward sweep is exact for the upward system alone, and a single downward
sweep is exact for the downward system given fixed parent bounds, but the
joint fixed point generally needs more than one round: the downward pass
tightens a leaf that the upward pass has already consumed, so any sibling
formula sharing that leaf is stale until the next sweep. Since sharing
subformulae across asserted axioms is precisely what the downward pass is
for, :func:upward_downward iterates to convergence_threshold and warns
if max_iterations is exhausted first.
Downward coverage. NEGATION, CONJUNCTION, DISJUNCTION and
IMPLICATION invert on both endpoints. NECESSITY and POSSIBILITY
invert on one endpoint each — a universally quantified lower bound
distributes over the neighbourhood (□) and an existential upper bound caps
every disjunct (♢), while the opposite directions constrain an aggregate
without saying which neighbour realises it and so have no canonical
per-world form. UNTIL has no downward rule at all: its backward DP
couples every time step.
FormulaType ¶
Bases: Enum
Types of nodes in a formula graph.
Each type corresponds to a logical operator in the MLNN language:
ATOMIC: Leaf proposition (no children).NEGATION: ¬ϕ (one child).CONJUNCTION: ϕ ∧ ψ (two children), Łukasiewicz t-norm.DISJUNCTION: ϕ ∨ ψ (two children), Łukasiewicz t-conorm.IMPLICATION: ϕ → ψ (two children), Łukasiewicz implication.NECESSITY: □ϕ (one child), aggregates over accessible worlds.POSSIBILITY: ♢ϕ (one child), aggregates over accessible worlds.UNTIL: ϕ U ψ (two children), backward DP over time steps.
Source code in torchmodal/inference.py
FormulaNode ¶
A node in the formula dependency graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique name for this formula node. |
required |
ftype
|
FormulaType
|
Type of formula (atomic, connective, or modal). |
required |
children
|
Optional[List[str]]
|
Names of child formula nodes. |
None
|
Source code in torchmodal/inference.py
FormulaGraph ¶
Directed acyclic graph of logical formulae.
Manages the dependency structure for upward-downward inference. Nodes are added in any order; topological sort is computed automatically.
Example::
>>> graph = FormulaGraph()
>>> graph.add_atomic("p")
>>> graph.add_atomic("q")
>>> graph.add_conjunction("p_and_q", "p", "q")
>>> graph.add_necessity("box_p_and_q", "p_and_q")
>>> order = graph.topological_order()
Source code in torchmodal/inference.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
add_atomic ¶
add_negation ¶
Add a negation node: ¬child.
add_conjunction ¶
Add a conjunction node: left ∧ right.
Source code in torchmodal/inference.py
add_disjunction ¶
Add a disjunction node: left ∨ right.
Source code in torchmodal/inference.py
add_implication ¶
Add an implication node: antecedent → consequent.
Source code in torchmodal/inference.py
add_necessity ¶
Add a necessity node: □child.
add_possibility ¶
Add a possibility node: ♢child.
add_until ¶
Add an Until node: hold U goal.
hold must remain true until goal becomes true.
Source code in torchmodal/inference.py
is_acyclic ¶
Check whether the formula graph is a DAG.
The upward-downward inference algorithm requires an acyclic dependency graph (Theorem 2 in the paper). Call this method to verify the invariant before running inference.
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in torchmodal/inference.py
topological_order ¶
Compute topological order (leaves first).
Source code in torchmodal/inference.py
upward_downward ¶
upward_downward(graph: FormulaGraph, bounds: Dict[str, Tensor], accessibility: Tensor, tau: float = 0.1, max_iterations: int = 10, convergence_threshold: float = 1e-06, top_k: Optional[int] = None) -> Dict[str, Tensor]
Run upward-downward inference on a formula graph.
Iteratively tightens truth bounds until convergence or maximum iterations. Each iteration consists of:
- Upward pass: propagate from leaves to root in topological order.
- Downward pass: propagate constraints from root to leaves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
FormulaGraph
|
The formula dependency graph. |
required |
bounds
|
Dict[str, Tensor]
|
Dict mapping formula names to initial bounds |
required |
accessibility
|
Tensor
|
Accessibility matrix |
required |
tau
|
float
|
Temperature for modal operators. Default 0.1. |
0.1
|
max_iterations
|
int
|
Maximum inference iterations. Default 10. |
10
|
convergence_threshold
|
float
|
Stop if max bound change < threshold. |
1e-06
|
top_k
|
Optional[int]
|
Passed to :func: |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Tensor]
|
Dict mapping formula names to tightened bounds |
Source code in torchmodal/inference.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | |