pymc.model.transform.extract_deterministics#

pymc.model.transform.extract_deterministics(model, var_names=None)[source]#

Remove Deterministics from a Model, returning them as detached subgraphs.

The Deterministic computations are inlined into the variables that depend on them, so the returned Model is equivalent to the original one but without the Deterministic labels. The removed Deterministics are returned as standalone graphs that can later be spliced back into a (possibly different) Model with insert_deterministics().

Parameters:
modelModel

The model to extract Deterministics from.

var_namesstr or sequence of str, optional

The names of the Deterministics to extract. Defaults to all the Deterministics in the model.

Returns:
new_modelModel

A copy of the model without the extracted Deterministics.

deterministicslist of FrozenFunctionGraph

The extracted Deterministics, as standalone graphs. The order is topological, so that Deterministics that depend on other extracted Deterministics come later.

See also

insert_deterministics

Splice Deterministics back into a Model.

Examples

import numpy as np
import pymc as pm
from pymc.model.transform import (
    extract_deterministics,
    insert_deterministics,
)

with pm.Model() as model:
    x = pm.Data("x", np.ones((10, 3)))
    beta = pm.Normal("beta", shape=(3,))
    mu = pm.Deterministic("mu", x @ beta)
    pm.Normal("y", mu=mu, sigma=1, observed=np.ones(10))

# Drop the ``mu`` Deterministic (it gets inlined into ``y``)
no_det_model, deterministics = extract_deterministics(model)

# Put it back
model_again = insert_deterministics(no_det_model, deterministics)