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:
objectFilters Theano variables based on a range of criteria.
Parameters: - roles (list of
VariableRoleinstances, optional) – Matches any variable which has one of the roles given. - bricks (list of
Brickclasses or list of) – instances ofBrick, 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. IfFalse, a variable matching any of the roles given will be returned.Falseby 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
ApplicationCallmetadata attribute. - applications (list of
Application) – orBoundApplication, 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
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.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]
- roles (list of
-
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().