regularizer.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. from __future__ import unicode_literals
  18. import paddle
  19. class L1Decay(object):
  20. """
  21. L1 Weight Decay Regularization, which encourages the weights to be sparse.
  22. Args:
  23. factor(float): regularization coeff. Default:0.0.
  24. """
  25. def __init__(self, factor=0.0):
  26. super(L1Decay, self).__init__()
  27. self.coeff = factor
  28. def __call__(self):
  29. reg = paddle.regularizer.L1Decay(self.coeff)
  30. return reg
  31. class L2Decay(object):
  32. """
  33. L2 Weight Decay Regularization, which helps to prevent the model over-fitting.
  34. Args:
  35. factor(float): regularization coeff. Default:0.0.
  36. """
  37. def __init__(self, factor=0.0):
  38. super(L2Decay, self).__init__()
  39. self.coeff = float(factor)
  40. def __call__(self):
  41. return self.coeff