Caffe2 - Python API
A deep learning, cross platform ML framework
regularizer_context.py
1 # Copyright (c) 2016-present, Facebook, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ##############################################################################
15 
16 # @package regularizer_context
17 # Module caffe2.python.regularizer_context
18 from __future__ import absolute_import
19 from __future__ import division
20 from __future__ import print_function
21 from __future__ import unicode_literals
22 
23 from caffe2.python import context
25  ModifierContext, UseModifierBase)
26 
27 
28 @context.define_context(allow_default=True)
29 class RegularizerContext(ModifierContext):
30  """
31  provide context to allow param_info to have different regularizers
32  """
33 
34  def has_regularizer(self, name):
35  return self._has_modifier(name)
36 
37  def get_regularizer(self, name):
38  assert self.has_regularizer(name), (
39  "{} regularizer is not provided!".format(name))
40  return self._get_modifier(name)
41 
42 
43 class UseRegularizer(UseModifierBase):
44  '''
45  context class to allow setting the current context.
46  Example useage with layer:
47  regularizers = {'reg1': reg1, 'reg2': reg2}
48  with Regularizers(regularizers):
49  reg = RegularizerContext.current().get_regularizer('reg1')
50  layer(reg=reg)
51  '''
52  def _context_class(self):
53  return RegularizerContext