GraphBuilder

Contents

GraphBuilder#

class liesel.model.model.GraphBuilder[source]#

Bases: object

A graph builder, used to set up a Model.

Constructs a model containing all nodes and variables that were added to the graph builder and their recursive inputs.

Important

  • In build_model() , the graph builder will automatically find all inputs to its nodes - and the inputs to these inputs (i.e. it finds inputs recursively).

  • The outputs of the nodes, however, are not added to the model automatically, so all root nodes need to be added explicitly.

  • Root nodes are nodes that are not inputs to any other node in the graph. The response in a regression model is an example of a root node.

The standard workflow is to create the nodes and variables, add the root var to a graph builder, and construct a model from the graph builder. After the model has been constructed, some methods of the graph builder are not available anymore, because the graph is considered static.

See also

Model

The liesel model class, representing a static graph.

GraphBuilder.add()

Method for adding variables and nodes to the GraphBuilder.

GraphBuilder.build_model()

Method for building a model from the GraphBuilder.

GraphBuilder.transform()

Transforms a variable by adding a new transformed variable as an input. This is useful for variables that are constrained to a certain domain, e.g. positive values.

Examples

We start by creating some 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 initialize a GraphBuilder and add the root node c to it:

>>> gb = lsl.GraphBuilder()
>>> gb.add(c)
GraphBuilder(0 nodes, 1 vars)

We are now ready to build the model:

>>> model = gb.build_model()
>>> model
Model(9 nodes, 3 vars)

Note that when build_model() is called, all weak variables in the graph will be updated. So the value of c is now available:

>>> c.value
3.0

The graph builder is now empty:

>>> gb.vars
[]

Methods

add(*args[, to_float32])

Adds nodes, variables or other graph builders to the graph.

add_groups(*groups[, to_float32])

Adds groups to the graph.

build_model([copy])

Builds a model from the graph.

convert_dtype(from_dtype, to_dtype)

Tries to convert the node values in the graph to the specified data type.

copy()

Returns a shallow copy of the graph builder.

count_node_names()

Counts the number of times each node name occurs in the graph.

count_var_names()

Counts the number of times each variable name occurs in the graph.

groups()

Collects the groups from all nodes and variables.

plot_nodes()

Plots all nodes in the graph.

plot_vars()

Plots all variables in the graph.

rename(pattern, replacement)

Renames all nodes and variables in the graph.

rename_nodes(pattern, replacement)

Renames all nodes in the graph.

rename_vars(pattern, replacement)

Renames all variables in the graph.

replace_node(old, new)

Replaces the old with the new node.

replace_var(old, new)

Replaces the old with the new variable.

transform(var[, bijector])

Transforms a variable by adding a new transformed variable as an input.

update()

Updates all nodes in the graph.

Attributes

log_lik_node

User-defined log-likelihood node, if there is one.

log_prior_node

User-defined log-prior node, if there is one.

log_prob_node

User-defined log-probability node, if there is one.

nodes

The nodes that were explicitly added to the graph.

vars

The variables that were explicitly added to the graph.