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)¶
Filters Theano variables based on a range of criteria.
- roles (list of
VariableRoleinstances, optional) – Matches any variable which has one of the roles given.
- bricks (list of
Brickclasses 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.
- 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
- applications (list of
Application) – or
BoundApplication, optional Matches a variable that was produced by any of the applications given.
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
PARAMETERrole 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.
>>> 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]) >>> var_filter(cg.variables) [b]
- roles (list of
A helper function to retrieve an annotation of a particular type.
This function returns the first annotation of a particular type. If there are multiple–there shouldn’t be–it will ignore them.
Retrieves the application call that created this variable.