Model

A model in Blocks is simply an annotated computation graph. The class Model extends blocks.graph.ComputationGraph :class:, which is able to handle annotations and roles in general, but is deliberately made unaware of specific annotations that a Theano graph created by Blocks typically has, such as bricks and application calls. The Model adds this functionality. Using Model you can do things like query all the bricks used to build the computation graph, request “hierarchical names” of the parameters (a hierarchical name is a path-like string which in addition to the parameter’s name contains names of the bricks on the path from a root brick to the brick that owns the parameters, e.g. /mlp/linear/W).

For more information, see Model docstring.

class blocks.model.Model(*args, **kwargs)[source]

Bases: blocks.graph.ComputationGraph

Handles annotations in Blocks-built computation graphs.

Use this class to handle your Blocks-created computation graph.

Examples

>>> from theano import tensor
>>> from blocks.bricks import MLP, Tanh
>>> x = tensor.matrix('x')
>>> mlp = MLP([Tanh(), Tanh()], [10, 10, 10])
>>> y = mlp.apply(x)
>>> model = Model(y)

With Model you can get access to the brick hierarchy. The brick hierarchy is defined by children attributes that every brick has. The bricks that are not children of other bricks are called top bricks. It is often useful to have access to top bricks of a brick hierarchy used to build a computation graph, and here is how you can do it:

>>> model.get_top_bricks() 
[<blocks.bricks.sequences.MLP object at ...]

You can also get “hierarchical” names for the parameters, which encode the position of the owning brick in the brick hierarchy.

>>> model.get_parameter_dict() 
OrderedDict([('/mlp/linear_1.b', b), ('/mlp/linear_0.b', b),
('/mlp/linear_0.W', W), ('/mlp/linear_1.W', W)])
check_sanity(algorithm)[source]
get_parameter_dict()[source]

Returns parameters with their hierarchical names.

The parameter names are formed from positions of their owner bricks in the bricks hierarchy. The variable names are used for the parameters that do not belong to any brick.

Returns:parameter_dict – A dictionary of (hierarchical name, shared variable) pairs.
Return type:dict
get_parameter_values()[source]

Return the values of model parameters.

The same hierarhical names as in get_parameter_dict() are used to uniquely identify parameters.

Returns:parameter_values – Dictionary of (hierarchical name, ndarray) pairs.
Return type:OrderedDict
get_top_bricks()[source]

Get the bricks that do not have parents.

Returns:bricks
Return type:list of Brick
set_parameter_values(parameter_values)[source]

Set the values of model parameters.

The same hierarhical names as in get_parameter_dict() are used to uniquely identify parameters.

Parameters:parameter_values (OrderedDict) – Dictionary of (hierarchical name, ndarray) pairs.