hetero_sshe_linr_param.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #
  18. import copy
  19. from federatedml.param.glm_param import LinearModelParam
  20. from federatedml.param.callback_param import CallbackParam
  21. from federatedml.param.encrypt_param import EncryptParam
  22. from federatedml.param.encrypted_mode_calculation_param import EncryptedModeCalculatorParam
  23. from federatedml.param.cross_validation_param import CrossValidationParam
  24. from federatedml.param.init_model_param import InitParam
  25. from federatedml.util import consts
  26. class HeteroSSHELinRParam(LinearModelParam):
  27. """
  28. Parameters used for Hetero SSHE Linear Regression.
  29. Parameters
  30. ----------
  31. penalty : {'L2' or 'L1'}
  32. Penalty method used in LinR. Please note that, when using encrypted version in HeteroLinR,
  33. 'L1' is not supported.
  34. tol : float, default: 1e-4
  35. The tolerance of convergence
  36. alpha : float, default: 1.0
  37. Regularization strength coefficient.
  38. optimizer : {'sgd', 'rmsprop', 'adam', 'adagrad'}
  39. Optimize method
  40. batch_size : int, default: -1
  41. Batch size when updating model. -1 means use all data in a batch. i.e. Not to use mini-batch strategy.
  42. learning_rate : float, default: 0.01
  43. Learning rate
  44. max_iter : int, default: 20
  45. The maximum iteration for training.
  46. init_param: InitParam object, default: default InitParam object
  47. Init param method object.
  48. early_stop : {'diff', 'abs', 'weight_dff'}
  49. Method used to judge convergence.
  50. a) diff: Use difference of loss between two iterations to judge whether converge.
  51. b) abs: Use the absolute value of loss to judge whether converge. i.e. if loss < tol, it is converged.
  52. c) weight_diff: Use difference between weights of two consecutive iterations
  53. encrypt_param: EncryptParam object, default: default EncryptParam object
  54. encrypt param
  55. encrypted_mode_calculator_param: EncryptedModeCalculatorParam object, default: default EncryptedModeCalculatorParam object
  56. encrypted mode calculator param
  57. cv_param: CrossValidationParam object, default: default CrossValidationParam object
  58. cv param
  59. decay: int or float, default: 1
  60. Decay rate for learning rate. learning rate will follow the following decay schedule.
  61. lr = lr0/(1+decay*t) if decay_sqrt is False. If decay_sqrt is True, lr = lr0 / sqrt(1+decay*t)
  62. where t is the iter number.
  63. decay_sqrt: Bool, default: True
  64. lr = lr0/(1+decay*t) if decay_sqrt is False, otherwise, lr = lr0 / sqrt(1+decay*t)
  65. callback_param: CallbackParam object
  66. callback param
  67. reveal_strategy: str, "respectively", "encrypted_reveal_in_host", default: "respectively"
  68. "respectively": Means guest and host can reveal their own part of weights only.
  69. "encrypted_reveal_in_host": Means host can be revealed his weights in encrypted mode, and guest can be revealed in normal mode.
  70. reveal_every_iter: bool, default: False
  71. Whether reconstruct model weights every iteration. If so, Regularization is available.
  72. The performance will be better as well since the algorithm process is simplified.
  73. """
  74. def __init__(self, penalty='L2',
  75. tol=1e-4, alpha=1.0, optimizer='sgd',
  76. batch_size=-1, learning_rate=0.01, init_param=InitParam(),
  77. max_iter=20, early_stop='diff',
  78. encrypt_param=EncryptParam(),
  79. encrypted_mode_calculator_param=EncryptedModeCalculatorParam(),
  80. cv_param=CrossValidationParam(), decay=1, decay_sqrt=True,
  81. callback_param=CallbackParam(),
  82. use_mix_rand=True,
  83. reveal_strategy="respectively",
  84. reveal_every_iter=False
  85. ):
  86. super(HeteroSSHELinRParam, self).__init__(penalty=penalty, tol=tol, alpha=alpha, optimizer=optimizer,
  87. batch_size=batch_size, learning_rate=learning_rate,
  88. init_param=init_param, max_iter=max_iter, early_stop=early_stop,
  89. encrypt_param=encrypt_param, cv_param=cv_param, decay=decay,
  90. decay_sqrt=decay_sqrt,
  91. callback_param=callback_param)
  92. self.encrypted_mode_calculator_param = copy.deepcopy(encrypted_mode_calculator_param)
  93. self.use_mix_rand = use_mix_rand
  94. self.reveal_strategy = reveal_strategy
  95. self.reveal_every_iter = reveal_every_iter
  96. def check(self):
  97. descr = "sshe linear_regression_param's "
  98. super(HeteroSSHELinRParam, self).check()
  99. if self.encrypt_param.method != consts.PAILLIER:
  100. raise ValueError(
  101. descr + "encrypt method supports 'Paillier' only")
  102. self.check_boolean(self.reveal_every_iter, descr)
  103. if self.penalty is None:
  104. pass
  105. elif type(self.penalty).__name__ != "str":
  106. raise ValueError(
  107. f"{descr} penalty {self.penalty} not supported, should be str type")
  108. else:
  109. self.penalty = self.penalty.upper()
  110. """
  111. if self.penalty not in [consts.L1_PENALTY, consts.L2_PENALTY]:
  112. raise ValueError(
  113. "logistic_param's penalty not supported, penalty should be 'L1', 'L2' or 'none'")
  114. """
  115. if not self.reveal_every_iter:
  116. if self.penalty not in [consts.L2_PENALTY, consts.NONE.upper()]:
  117. raise ValueError(
  118. f"penalty should be 'L2' or 'none', when reveal_every_iter is False"
  119. )
  120. if type(self.optimizer).__name__ != "str":
  121. raise ValueError(
  122. f"{descr} optimizer {self.optimizer} not supported, should be str type")
  123. else:
  124. self.optimizer = self.optimizer.lower()
  125. if self.reveal_every_iter:
  126. if self.optimizer not in ['sgd', 'rmsprop', 'adam', 'adagrad']:
  127. raise ValueError(
  128. "When reveal_every_iter is True, "
  129. f"{descr} optimizer not supported, optimizer should be"
  130. " 'sgd', 'rmsprop', 'adam', or 'adagrad'")
  131. else:
  132. if self.optimizer not in ['sgd']:
  133. raise ValueError("When reveal_every_iter is False, "
  134. f"{descr} optimizer not supported, optimizer should be"
  135. " 'sgd'")
  136. if self.callback_param.validation_freqs is not None:
  137. if self.reveal_every_iter is False:
  138. raise ValueError(f"When reveal_every_iter is False, validation every iter"
  139. f" is not supported.")
  140. self.reveal_strategy = self.check_and_change_lower(self.reveal_strategy,
  141. ["respectively", "encrypted_reveal_in_host"],
  142. f"{descr} reveal_strategy")
  143. if self.reveal_strategy == "encrypted_reveal_in_host" and self.reveal_every_iter:
  144. raise PermissionError("reveal strategy: encrypted_reveal_in_host mode is not allow to reveal every iter.")
  145. return True