Caffe2 - Python API
A deep learning, cross platform ML framework
normalizer_context.py
1 # @package regularizer_context
2 # Module caffe2.python.normalizer_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 NormalizerContext(ModifierContext):
15  """
16  provide context to allow param_info to have different normalizers
17  """
18 
19  def has_normalizer(self, name):
20  return self._has_modifier(name)
21 
22  def get_normalizer(self, name):
23  assert self.has_normalizer(name), (
24  "{} normalizer is not provided!".format(name))
25  return self._get_modifier(name)
26 
27 
28 class UseNormalizer(UseModifierBase):
29  '''
30  context class to allow setting the current context.
31  Example useage with layer:
32  normalizers = {'norm1': norm1, 'norm2': norm2}
33  with UseNormalizer(normalizers):
34  norm = NormalizerContext.current().get_normalizer('norm1')
35  layer(norm=norm)
36  '''
37  def _context_class(self):
38  return NormalizerContext