# Brick selectors¶

class blocks.select.Path(nodes)[source]

Bases: object

Encapsulates a path in a hierarchy of bricks.

Currently the only allowed elements of paths are names of the bricks and names of parameters. The latter can only be put in the end of the path. It is planned to support regular expressions in some way later.

Parameters: nodes (list or tuple of path nodes) – The nodes of the path.
nodes

tuple – The tuple containing path nodes.

class BrickName[source]

Bases: str

part()[source]
class ParameterName[source]

Bases: str

part()[source]
parameter_separator = '.'
static parse(string)[source]

Constructs a path from its string representation.

Todo

More error checking.

Parameters: string (str) – String representation of the path.
separator = '/'
separator_re = <_sre.SRE_Pattern object>
class blocks.select.Selector(bricks)[source]

Bases: object

Selection of elements of a hierarchy of bricks.

Parameters: bricks (list of Brick) – The bricks of the selection.
get_parameters(parameter_name=None)[source]

Returns parameters from selected bricks and their descendants.

Parameters: parameter_name (Path.ParameterName, optional) – If given, only parameters with a name attribute equal to parameter_name are returned. parameters – A dictionary of (path, parameter) pairs, where path is a string representation of the path in the brick hierarchy to the parameter (i.e. the slash-delimited path to the brick that owns the parameter, followed by a dot, followed by the parameter’s name), and parameter is the Theano variable representing the parameter. OrderedDict

Examples

>>> from blocks.bricks import MLP, Tanh
>>> mlp = MLP([Tanh(), Tanh(), Tanh()], [5, 7, 11, 2])
>>> mlp.allocate()
>>> selector = Selector([mlp])
>>> selector.get_parameters()
OrderedDict([('/mlp/linear_0.W', W), ('/mlp/linear_0.b', b),
('/mlp/linear_1.W', W), ('/mlp/linear_1.b', b),
('/mlp/linear_2.W', W), ('/mlp/linear_2.b', b)])


Or, select just the weights of the MLP by passing the parameter name W:

>>> w_select = Selector([mlp])
>>> w_select.get_parameters('W')
OrderedDict([('/mlp/linear_0.W', W), ('/mlp/linear_1.W', W),
('/mlp/linear_2.W', W)])

select(path)[source]

Select a subset of current selection matching the path given.

Warning

Current implementation is very inefficient (theoretical complexity is $$O(n^3)$$, where $$n$$ is the number of bricks in the hierarchy). It can be sped up easily.

Parameters: path (Path or str) – The path for the desired selection. If a string is given it is parsed into a path. Depending on the path given, one of the following * Selector with desired bricks. * list of SharedTensorVariable.