tbnpy.compile¶
Compiles a ModelSpec into runnable tbnpy objects.
compile_file() loads and compiles a spec file in one call:
from tbnpy.compile import compile_file
spec, varis, probs = compile_file("examples/ABCDE/abcde.tbn.json")
Every compiled node is a Probability, so it prints as the
distribution it defines, with the form taken from the spec:
>>> probs["C"]
DistributionSpecNode(P(C | A, B): normal(loc=A + B, scale=0.6))
>>> probs["E"]
EquationSpecNode(P(E | C, D): E = If(D == 0, C, 0))
Each node also keeps the NodeSpec it came from as .spec, and
that node’s free-form metadata as .spec_metadata, for inference algorithms
that need model-level hints.
API¶
Compile a tbnpy.spec.ModelSpec into runnable tbnpy objects.
compile_model returns exactly the two things the rest of the library works
with – a {name: Variable} table and a {name: node} table – and
nothing else. It has no opinion about how those are then used, which is what
keeps bespoke inference algorithms unconstrained by the model format.
Every generated node follows the tbnpy protocol: childs, parents,
sample(n_sample=...) for roots or sample(Cs_pars=...) for the rest,
and log_prob(Cs) with columns ordered childs-then-parents. Each is a
tbnpy.probability.Probability, whose form() comes from the spec,
so it prints as the distribution it defines. Nodes also
carry spec (the originating NodeSpec) and
spec_metadata, so an algorithm that needs model-level hints can read them
without the compiler having to anticipate it.
- tbnpy.compile.compile_model(spec: ModelSpec, device: str = 'cpu', search_path: Sequence | None = None, validate: bool = True)[source]¶
Turn a spec into
(varis, probs).- Parameters:
spec – The model description.
device –
"cpu"or"cuda", passed to every node.search_path – Directories to make importable while constructing
customnodes, typically the folder holding the model file.validate – Re-check the spec before compiling. Leave on unless it was just validated.
- Returns:
{name: Variable}and{node_name: node}, ready fortbnpy.inferenceor for a bespoke algorithm.- Return type:
(dict, dict)
- tbnpy.compile.compile_file(path, device: str = 'cpu', search_path=None)[source]¶
Load a spec file and compile it, searching next to the file by default.
- exception tbnpy.compile.CompileError[source]¶
Raised when a valid spec cannot be turned into runnable objects.
- tbnpy.compile.FAMILIES = {'bernoulli': (('probs',), <function <lambda>>), 'beta': (('concentration1', 'concentration0'), <function <lambda>>), 'categorical': (('probs',), <function <lambda>>), 'exponential': (('rate',), <function <lambda>>), 'gamma': (('concentration', 'rate'), <function <lambda>>), 'lognormal': (('loc', 'scale'), <function <lambda>>), 'loguniform': (('low', 'high'), <function <lambda>>), 'normal': (('loc', 'scale'), <function <lambda>>), 'student_t': (('df', 'loc', 'scale'), <function <lambda>>), 'truncated_poisson': (('rate',), <function <lambda>>), 'uniform': (('low', 'high'), <function <lambda>>), 'weibull': (('scale', 'concentration'), <function <lambda>>)}¶
family -> (required parameter names, builder).
builderreceives a dict of broadcast tensors plusn_statesfor discrete children.