Filter

class blocks.filter.VariableFilter(roles=None, bricks=None, each_role=False, name=None, name_regex=None, theano_name=None, theano_name_regex=None, call_id=None, applications=None)[source]

Bases: object

Filters Theano variables based on a range of criteria.

Parameters:
  • roles (list of VariableRole instances, optional) – Matches any variable which has one of the roles given.
  • bricks (list of Brick classes or list of) – instances of Brick, optional Matches any variable that is instance of any of the given classes or that is owned by any of the given brick instances.
  • each_role (bool, optional) – If True, the variable needs to have all given roles. If False, a variable matching any of the roles given will be returned. False by default.
  • name (str, optional) – The variable name. The Blocks name (i.e. x.tag.name) is used.
  • name_regex (str, optional) – A regular expression for the variable name. The Blocks name (i.e. x.tag.name) is used.
  • theano_name (str, optional) – The variable name. The Theano name (i.e. x.name) is used.
  • theano_name_regex (str, optional) – A regular expression for the variable name. The Theano name (i.e. x.name) is used.
  • call_id (str, optional) – The call identifier as written in ApplicationCall metadata attribute.
  • applications (list of Application) – or BoundApplication, optional Matches a variable that was produced by any of the applications given.

Notes

Note that only auxiliary variables, parameters, inputs and outputs are tagged with the brick that created them. Other Theano variables that were created in the process of applying a brick will be filtered out.

Note that technically speaking, bricks are able to have non-shared variables as parameters. For example, we can use the transpose of another weight matrix as the parameter of a particular brick. This means that in some unusual cases, filtering by the PARAMETER role alone will not be enough to retrieve all trainable parameters in your model; you will need to filter out the shared variables from these (using e.g. is_shared_variable()).

Examples

>>> from blocks.bricks import MLP, Linear, Logistic, Identity
>>> from blocks.roles import BIAS
>>> mlp = MLP(activations=[Identity(), Logistic()], dims=[20, 10, 20])
>>> from theano import tensor
>>> x = tensor.matrix()
>>> y_hat = mlp.apply(x)
>>> from blocks.graph import ComputationGraph
>>> cg = ComputationGraph(y_hat)
>>> from blocks.filter import VariableFilter
>>> var_filter = VariableFilter(roles=[BIAS],
...                             bricks=[mlp.linear_transformations[0]])
>>> var_filter(cg.variables)
[b]
blocks.filter.get_annotation(var, cls)[source]

A helper function to retrieve an annotation of a particular type.

Notes

This function returns the first annotation of a particular type. If there are multiple–there shouldn’t be–it will ignore them.

blocks.filter.get_application_call(var)[source]

Retrieves the application call that created this variable.

See get_annotation().

blocks.filter.get_brick(var)[source]

Retrieves the brick that created this variable.

See get_annotation().