init_model_param.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2019 The FATE Authors. All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. from federatedml.param.base_param import BaseParam
  18. class InitParam(BaseParam):
  19. """
  20. Initialize Parameters used in initializing a model.
  21. Parameters
  22. ----------
  23. init_method : {'random_uniform', 'random_normal', 'ones', 'zeros' or 'const'}
  24. Initial method.
  25. init_const : int or float, default: 1
  26. Required when init_method is 'const'. Specify the constant.
  27. fit_intercept : bool, default: True
  28. Whether to initialize the intercept or not.
  29. """
  30. def __init__(self, init_method='random_uniform', init_const=1, fit_intercept=True, random_seed=None):
  31. super().__init__()
  32. self.init_method = init_method
  33. self.init_const = init_const
  34. self.fit_intercept = fit_intercept
  35. self.random_seed = random_seed
  36. def check(self):
  37. if type(self.init_method).__name__ != "str":
  38. raise ValueError(
  39. "Init param's init_method {} not supported, should be str type".format(self.init_method))
  40. else:
  41. self.init_method = self.init_method.lower()
  42. if self.init_method not in ['random_uniform', 'random_normal', 'ones', 'zeros', 'const']:
  43. raise ValueError(
  44. "Init param's init_method {} not supported, init_method should in 'random_uniform',"
  45. " 'random_normal' 'ones', 'zeros' or 'const'".format(self.init_method))
  46. if type(self.init_const).__name__ not in ['int', 'float']:
  47. raise ValueError(
  48. "Init param's init_const {} not supported, should be int or float type".format(self.init_const))
  49. if type(self.fit_intercept).__name__ != 'bool':
  50. raise ValueError(
  51. "Init param's fit_intercept {} not supported, should be bool type".format(self.fit_intercept))
  52. if self.random_seed is not None:
  53. if type(self.random_seed).__name__ != 'int':
  54. raise ValueError(
  55. "Init param's random_seed {} not supported, should be int or float type".format(self.random_seed))
  56. return True