Repository navigation
Dynamic Bayesian networks in the mid level - #94
richard303d wants to merge 17 commits into
Conversation
DynamicBayesianNetwork(variables, initial, transition) is a directed graphical model over discrete time steps, given by the CPDs of the first steps and of one transition (Murphy, 2002); Variable.previous() is the arc from the step before, previous(lag) from an older one, for a model of any order. It builds the BayesianNetwork of each kind of step, where the interface variables of the steps before are observed roots, and the unrolled network of any horizon. DynamicInference runs any engine of a static model on a DBN, on the network unrolled over the horizon, built once per horizon. It returns an InferenceOutput with the steps as the last leading axis. WearDataset is a synthetic fleet of machines with concepts over time. The mid-level examples 08 and 09 build a concept bottleneck model over time, and concepts moved forward by a fitted mechanism and a learned residual; 08 ends by running the two networks of the DBN one step at a time by hand. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…work # Conflicts: # doc/modules/mid_level_api.rst # torch_concepts/nn/__init__.py
…WearDataset split by machine
There was a problem hiding this comment.
together with the dataset, this PR also need to implement the corresponding DataModule
| def __init__( | ||
| self, | ||
| variables: List[Variable], | ||
| initial: Union[List[ParametricCPD], List[List[ParametricCPD]]], |
There was a problem hiding this comment.
is it really necessary to permit List[List[ParametricCPD]] ? a list of CPDs should suffice for a prior of a first-order DBN.
Except we want to include second-order, but I would keep it simple for now
| class DynamicConceptDataset(ConceptDataset): | ||
| """A ConceptDataset whose samples are windows of time series. | ||
|
|
||
| The inputs are ``N`` series of ``T`` steps, ``(T, N, F)``, and the |
| """A ConceptDataset whose samples are windows of time series. | ||
|
|
||
| The inputs are ``N`` series of ``T`` steps, ``(T, N, F)``, and the | ||
| optional concepts are series over the same steps, ``(T, N, C)``. Each |
There was a problem hiding this comment.
explain the flattening operation also here
| >>> from torch_concepts.data.base import DynamicConceptDataset | ||
| >>> x, c = torch.randn(100, 3, 4), torch.randn(100, 3, 2) # 3 series of 100 steps | ||
| >>> annotations = Annotations(labels=["a", "b"], cardinalities=[1, 1], types=["continuous"] * 2) | ||
| >>> dataset = DynamicConceptDataset(x, c, annotations, window=10, stride=5) |
There was a problem hiding this comment.
do the example with stride = 1 so that is easier to read
| class WearDataset(DynamicConceptDataset): | ||
| """Synthetic dataset of machines whose two components wear out, with concepts over time. | ||
|
|
||
| Each of the ``n_gen`` machines runs for ``n_steps`` steps, and at every |
There was a problem hiding this comment.
the docstring is criptic. Be clearer about the data shape it produces
| show the load, but no concept records it. The alarm fires when the mean | ||
| health of the two components drops below ``ALARM_THRESHOLD = -0.45``. | ||
|
|
||
| The dataset is modelled on N-CMAPSS. Like the flight conditions of an |
There was a problem hiding this comment.
reference the N-CMAPSS citation
| and load. Reading the health from the sensors means undoing the | ||
| conditions, a nonlinear task. | ||
|
|
||
| By default a sample is the whole series of one machine. Its inputs are |
There was a problem hiding this comment.
also here the docstring could be made much more readable. Probably even more compact
| # Data | ||
| # ---- | ||
| # Each machine runs for 30 steps, enough for almost all of them to reach the | ||
| # alarm. A sample is a window of ``W + H`` steps of one machine: ``D`` |
There was a problem hiding this comment.
I would put D as an hyperparameter
| # Each machine runs for 30 steps, enough for almost all of them to reach the | ||
| # alarm. A sample is a window of ``W + H`` steps of one machine: ``D`` | ||
| # channels, the health of its two components and the alarm. A window starts | ||
| # every 2 steps, so the samples cover every stage of wear. The model reads the |
There was a problem hiding this comment.
I would describe the model later, here data only
| x = dataset.input_data[:, :W] | ||
| c = dataset.concepts[:, W - 1:][["health_A", "health_B"]] | ||
| y = dataset.concepts[:, W - 1:][["alarm"]] | ||
| train, test = dataset.series < 1000, dataset.series >= 1000 |
There was a problem hiding this comment.
specify "inductive split" here
| n_gen=1500, n_steps=30, window=W + H, stride=2, root=str(DATA_ROOT / "wear") | ||
| ) | ||
| x = dataset.input_data[:, :W] | ||
| c = dataset.concepts[:, W - 1:][["health_A", "health_B"]] |
There was a problem hiding this comment.
comment in line with the expected shapes
| D = dataset.input_data.shape[-1] | ||
|
|
||
| # %% | ||
| # Variables |
There was a problem hiding this comment.
variables are part of the "model"
There was a problem hiding this comment.
can we add a demo of the intervention on this kind of models?
| variables = dict.fromkeys(templates.values()) # each variable once, in the order passed | ||
| # A copy of each variable at each step where it has a CPD. | ||
| copies = { | ||
| (v.name, t): _copy(v, t) |
There was a problem hiding this comment.
think on whether you want to keep this notation (v.name, t) for referring to the variable. It has its advantages w.r.t. using strings -> it is harder to get things wrong, but at the same time also disadvantages -> the api is different and the user would need to learn two different APIs, and needs to be documented separately, which is usually better to avoid
There was a problem hiding this comment.
they will still be references as name_1, name_2 in the pgm, as that is forced to be a string. So maybe better keep it that way to avoid duplicating the same thing?
| print(f"\nmean over the {len(x_open)} test windows with 0.2 < P(alarm_H = 1) < 0.8:") | ||
| for label, given in evidence.items(): | ||
| p = sampler.query({("alarm", 4): ones}, evidence=given, horizon=H).probabilities | ||
| print(f" {label:32s} {p.mean():.3f}") |
There was a problem hiding this comment.
also from the output alone, it is not super clear what the example is about and what exactly is being claimed
There was a problem hiding this comment.
same comments as the other example :)
| return {_at(n, t): v for t, step in enumerate(per_step) for n, v in step.items()} | ||
|
|
||
|
|
||
| class DynamicInference(BaseInference): |
There was a problem hiding this comment.
I am not sure this is the best name? is it established in the literature? I would consider RolloutInference
Description
This PR adds dynamic Bayesian networks (DBNs) to the mid level. A DBN is a model over time given by the CPDs of the first step and the CPDs of one transition, the same at every step (Murphy, 2002). The existing engines run on the network unrolled over the horizon.
Variable.previous(lag=1): a handle to the variablelagsteps earlier, for the parents of a transition CPD.DynamicBayesianNetwork(variables, initial, transition): validates the CPDs and builds from them the unrolled network of steps0..H(unroll(H), one variable per step). For a model of orderk,initialholds one list of CPDs for each of the firstksteps. It is aBayesianNetwork: that of its first steps,unroll(k), with one variable per step (c_0,c_1), on which any static engine andplot_pgmrun.DynamicInference(pgm, engine=None): runs any static engine (DeterministicInferenceby default) onpgm.unroll(H). In a query, a name stands for every step at which its variable exists and(name, t)for steptonly. The output is anInferenceOutputwith the steps as the last leading axis,(n, T, k), the layout of the concepts over time that Concepts on the last axis in the data layer, with leading axes such as time steps #92 added to the data layer. It is aBaseInferencethat holds the DBN aspgm.WearDataset: a small synthetic fleet of machines modelled on N-CMAPSS (Arias Chao et al., 2021), with concepts over time.08_dynamic_concept_bottleneck_model.py, a CBM over time, and09_dynamic_cbm_with_residual.py, the model of the example we discussed, with concepts moved forward by a fitted mechanism plus a learned residual.The engines are unchanged, apart from two lines of
unpack_plates, which now rebuild aBayesianNetworkfor a subclass of it;Variablegainsprevious()and two private attributes.Changes Made
Checklist
Tests
test_dynamic_bayesian_network.py, 40 tests:previous(),unroll(), a model of order 2, the DBN as aBayesianNetworkand the errors.test_dynamic_inference.py, 52 tests with seven engines, most of them against the recursion of the model written in plain torch with the same modules, gradients included, andDynamicInferenceas an engine.test_wear.py, 4 tests.pytest tests --ignore=tests/dataon Python 3.10, 3.12 and 3.13: 2737 passed, 6 skipped, against 2645 ondevat a16e60d, whose tests all keep their outcome.tests/datawith the[data]extras: 466 passed (462 ondev).dev.