Model

Contents

Model#

class liesel.model.model.Model(nodes_and_vars, grow=True, copy=False)[source]#

Bases: object

A model with a static graph.

Tip

While you can create a model directly, it is usually more convenient to use a GraphBuilder to construct the model.

Parameters:
  • nodes_and_vars (Iterable[Node | Var]) – The nodes and variables to include in the model.

  • grow (bool) – Whether a GraphBuilder should be used to grow the model (finding the recursive inputs of the nodes and variables), and to add the model nodes. (default: True)

  • copy (bool) – Whether the nodes and variables should be copied upon initialization. (default: False)

See also

GraphBuilder

A graph builder, used to set up a model.

Examples

For basic examples on how to set up a model, please refer to the GraphBuilder documentation.

Modifying an existing model

If you have an existing model and want to make changes to it, you can use the Model.copy_nodes_and_vars() or the Model.copy_nodes_and_vars() method to obtain the nodes and variables of the model, make changes to them, and then create a new model from the modified nodes and variables.

>>> a = lsl.Var(1.0, name="a")
>>> b = lsl.Var(2.0, name="b")
>>> c = lsl.Var(lsl.Calc(lambda x, y: x + y, a, b), name="c")

We now build a model:

>>> model = lsl.GraphBuilder().add(c).build_model()
>>> model
Model(9 nodes, 3 vars)
>>> nodes, vars_ = model.pop_nodes_and_vars()
>>> vars_
{'c': Var(name="c"), 'b': Var(name="b"), 'a': Var(name="a")}
>>> from pprint import pprint # for nicer formatting of the output dicts
>>> pprint(nodes)
{'a_value': Data(name="a_value"),
 'a_var_value': VarValue(name="a_var_value"),
 'b_value': Data(name="b_value"),
 'b_var_value': VarValue(name="b_var_value"),
 'c_value': Calc(name="c_value"),
 'c_var_value': VarValue(name="c_var_value")}

We can now make changes to the nodes and variables. Just for show, let’s add a distribution to the node a:

>>> import tensorflow_probability.substrates.jax.distributions as tfd
>>> vars_["a"].dist_node = lsl.Dist(tfd.Normal, loc=0.0, scale=1.0)

Now we create a new GraphBuilder and build a new model:

>>> gb = lsl.GraphBuilder()
>>> gb = gb.add(*nodes.values(), *vars_.values())
>>> model = gb.build_model()
>>> model
Model(12 nodes, 3 vars)

Methods

copy_nodes_and_vars()

Returns an unfrozen deep copy of the model nodes and variables.

groups()

Collects the groups from all nodes and variables.

pop_nodes_and_vars()

Pops the nodes and variables out of this model.

set_seed(seed)

Splits and sets the seed / PRNG key.

simulate(seed[, skip])

Updates the model state simulating from the probability distributions in the model using a provided random seed, optionally skipping specified nodes.

update(*names)

Updates the target nodes and their recursive inputs if they are outdated.

Attributes

auto_update

Whether to update the model automatically if the value of a node is modified.

log_lik

The log-likelihood of the model.

log_prior

The log-prior of the model.

log_prob

The (unnormalized) log-probability / log-posterior of the model.

node_graph

The directed graph of the model nodes.

nodes

A mapping of the model nodes with their names as keys.

state

The state of the model as a dict of node names and states.

var_graph

The directed graph of the model variables.

vars

A mapping of the model variables with their names as keys.