| Conditions | 4 |
| Total Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | from collections import OrderedDict |
||
| 59 | def add_auxiliary_variable(self, variable, roles=None, name=None): |
||
| 60 | """Attach an auxiliary variable to the graph. |
||
| 61 | |||
| 62 | Auxiliary variables are Theano variables that are not part of a |
||
| 63 | brick's output, but can be useful nonetheless e.g. as a regularizer |
||
| 64 | or to monitor during training progress. |
||
| 65 | |||
| 66 | Parameters |
||
| 67 | ---------- |
||
| 68 | variable : :class:`~tensor.TensorVariable` |
||
| 69 | The variable you want to add. |
||
| 70 | roles : list of :class:`.VariableRole` instances, optional |
||
| 71 | The roles of this variable. The :const:`.AUXILIARY` |
||
| 72 | role will automatically be added. Other options are |
||
| 73 | :const:`.COST`, :const:`.WEIGHT`, etc. |
||
| 74 | name : str, optional |
||
| 75 | Name to give to the variable. If the variable already has a |
||
| 76 | name it will be overwritten. |
||
| 77 | |||
| 78 | Examples |
||
| 79 | -------- |
||
| 80 | >>> from blocks.bricks.base import application, Brick |
||
| 81 | >>> from blocks.roles import COST |
||
| 82 | >>> from blocks.utils import shared_floatx_nans |
||
| 83 | >>> class Foo(Brick): |
||
| 84 | ... def _allocate(self): |
||
| 85 | ... W = shared_floatx_nans((10, 10)) |
||
| 86 | ... self.add_auxiliary_variable(W.mean(), name='mean_W') |
||
| 87 | ... @application |
||
| 88 | ... def apply(self, x, application_call): |
||
| 89 | ... application_call.add_auxiliary_variable( |
||
| 90 | ... x - 1, name='x_minus_1') |
||
| 91 | ... application_call.add_auxiliary_variable( |
||
| 92 | ... x.mean(), roles=[COST], name='mean_x') |
||
| 93 | ... return x + 1 |
||
| 94 | >>> from theano import tensor |
||
| 95 | >>> x = tensor.vector() |
||
| 96 | >>> y = Foo().apply(x) |
||
| 97 | >>> from blocks.graph import ComputationGraph |
||
| 98 | >>> cg = ComputationGraph([y]) |
||
| 99 | >>> from blocks.filter import VariableFilter |
||
| 100 | >>> var_filter = VariableFilter(roles=[AUXILIARY]) |
||
| 101 | >>> var_filter(cg.variables) # doctest: +SKIP |
||
| 102 | {x_minus_1, mean_W, mean_x} |
||
| 103 | >>> var_filter = VariableFilter(roles=[COST]) |
||
| 104 | >>> var_filter(cg.variables) # doctest: +SKIP |
||
| 105 | {mean_x} |
||
| 106 | |||
| 107 | """ |
||
| 108 | add_annotation(variable, self) |
||
| 109 | if name is not None: |
||
| 110 | variable.name = name |
||
| 111 | variable.tag.name = name |
||
| 112 | add_role(variable, AUXILIARY) |
||
| 113 | if roles is not None: |
||
| 114 | for role in roles: |
||
| 115 | add_role(variable, role) |
||
| 116 | self.auxiliary_variables.append(variable) |
||
| 117 |