Caffe2 - Python API
A deep learning, cross platform ML framework
regularizer_context.py
1 # @package regularizer_context
2 # Module caffe2.python.regularizer_context
3 from __future__ import absolute_import
4 from __future__ import division
5 from __future__ import print_function
6 from __future__ import unicode_literals
7 
8 from caffe2.python import context
10  ModifierContext, UseModifierBase)
11 
12 
13 @context.define_context(allow_default=True)
14 class RegularizerContext(ModifierContext):
15  """
16  provide context to allow param_info to have different regularizers
17  """
18 
19  def has_regularizer(self, name):
20  return self._has_modifier(name)
21 
22  def get_regularizer(self, name):
23  assert self.has_regularizer(name), (
24  "{} regularizer is not provided!".format(name))
25  return self._get_modifier(name)
26 
27 
28 class UseRegularizer(UseModifierBase):
29  '''
30  context class to allow setting the current context.
31  Example useage with layer:
32  regularizers = {'reg1': reg1, 'reg2': reg2}
33  with UseRegularizer(regularizers):
34  reg = RegularizerContext.current().get_regularizer('reg1')
35  layer(reg=reg)
36  '''
37  def _context_class(self):
38  return RegularizerContext