hetero_sshe_lr_param.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import copy
  16. from pipeline.param.logistic_regression_param import LogisticParam
  17. from pipeline.param.cross_validation_param import CrossValidationParam
  18. from pipeline.param.callback_param import CallbackParam
  19. from pipeline.param.encrypt_param import EncryptParam
  20. from pipeline.param.encrypted_mode_calculation_param import EncryptedModeCalculatorParam
  21. from pipeline.param.init_model_param import InitParam
  22. from pipeline.param.predict_param import PredictParam
  23. from pipeline.param import consts
  24. class HeteroSSHELRParam(LogisticParam):
  25. """
  26. Parameters used for Hetero SSHE Logistic Regression
  27. Parameters
  28. ----------
  29. penalty : str, 'L1', 'L2' or None. default: 'L2'
  30. Penalty method used in LR. If it is not None, weights are required to be reconstruct every iter.
  31. tol : float, default: 1e-4
  32. The tolerance of convergence
  33. alpha : float, default: 1.0
  34. Regularization strength coefficient.
  35. optimizer : str, 'sgd', 'rmsprop', 'adam', 'nesterov_momentum_sgd', or 'adagrad', default: 'sgd'
  36. Optimizer
  37. batch_size : int, default: -1
  38. Batch size when updating model. -1 means use all data in a batch. i.e. Not to use mini-batch strategy.
  39. learning_rate : float, default: 0.01
  40. Learning rate
  41. max_iter : int, default: 100
  42. The maximum iteration for training.
  43. early_stop : str, 'diff', 'weight_diff' or 'abs', default: 'diff'
  44. Method used to judge converge or not.
  45. a) diff: Use difference of loss between two iterations to judge whether converge.
  46. b) weight_diff: Use difference between weights of two consecutive iterations
  47. c) abs: Use the absolute value of loss to judge whether converge. i.e. if loss < eps, it is converged.
  48. decay: int or float, default: 1
  49. Decay rate for learning rate. learning rate will follow the following decay schedule.
  50. lr = lr0/(1+decay*t) if decay_sqrt is False. If decay_sqrt is True, lr = lr0 / sqrt(1+decay*t)
  51. where t is the iter number.
  52. decay_sqrt: Bool, default: True
  53. lr = lr0/(1+decay*t) if decay_sqrt is False, otherwise, lr = lr0 / sqrt(1+decay*t)
  54. encrypt_param: EncryptParam object, default: default EncryptParam object
  55. encrypt param
  56. predict_param: PredictParam object, default: default PredictParam object
  57. predict param
  58. cv_param: CrossValidationParam object, default: default CrossValidationParam object
  59. cv param
  60. multi_class: str, 'ovr', default: 'ovr'
  61. If it is a multi_class task, indicate what strategy to use. Currently, support 'ovr' short for one_vs_rest only.
  62. reveal_strategy: str, "respectively", "encrypted_reveal_in_host", default: "respectively"
  63. "respectively": Means guest and host can reveal their own part of weights only.
  64. "encrypted_reveal_in_host": Means host can be revealed his weights in encrypted mode, and guest can be revealed in normal mode.
  65. reveal_every_iter: bool, default: False
  66. Whether reconstruct model weights every iteration. If so, Regularization is available.
  67. The performance will be better as well since the algorithm process is simplified.
  68. """
  69. def __init__(self, penalty='L2',
  70. tol=1e-4, alpha=1.0, optimizer='sgd',
  71. batch_size=-1, learning_rate=0.01, init_param=InitParam(),
  72. max_iter=100, early_stop='diff', encrypt_param=EncryptParam(),
  73. predict_param=PredictParam(), cv_param=CrossValidationParam(),
  74. decay=1, decay_sqrt=True,
  75. multi_class='ovr', use_mix_rand=True,
  76. reveal_strategy="respectively",
  77. reveal_every_iter=False,
  78. callback_param=CallbackParam(),
  79. encrypted_mode_calculator_param=EncryptedModeCalculatorParam()
  80. ):
  81. super(HeteroSSHELRParam, self).__init__(penalty=penalty, tol=tol, alpha=alpha, optimizer=optimizer,
  82. batch_size=batch_size,
  83. learning_rate=learning_rate,
  84. init_param=init_param, max_iter=max_iter, early_stop=early_stop,
  85. predict_param=predict_param, cv_param=cv_param,
  86. decay=decay,
  87. decay_sqrt=decay_sqrt, multi_class=multi_class,
  88. encrypt_param=encrypt_param, callback_param=callback_param)
  89. self.use_mix_rand = use_mix_rand
  90. self.reveal_strategy = reveal_strategy
  91. self.reveal_every_iter = reveal_every_iter
  92. self.encrypted_mode_calculator_param = copy.deepcopy(encrypted_mode_calculator_param)
  93. def check(self):
  94. descr = "logistic_param's"
  95. super(HeteroSSHELRParam, self).check()
  96. self.check_boolean(self.reveal_every_iter, descr)
  97. if self.penalty is None:
  98. pass
  99. elif type(self.penalty).__name__ != "str":
  100. raise ValueError(
  101. "logistic_param's penalty {} not supported, should be str type".format(self.penalty))
  102. else:
  103. self.penalty = self.penalty.upper()
  104. """
  105. if self.penalty not in [consts.L1_PENALTY, consts.L2_PENALTY]:
  106. raise ValueError(
  107. "logistic_param's penalty not supported, penalty should be 'L1', 'L2' or 'none'")
  108. """
  109. if not self.reveal_every_iter:
  110. if self.penalty not in [consts.L2_PENALTY, consts.NONE.upper()]:
  111. raise ValueError(
  112. f"penalty should be 'L2' or 'none', when reveal_every_iter is False"
  113. )
  114. if type(self.optimizer).__name__ != "str":
  115. raise ValueError(
  116. "logistic_param's optimizer {} not supported, should be str type".format(self.optimizer))
  117. else:
  118. self.optimizer = self.optimizer.lower()
  119. if self.reveal_every_iter:
  120. if self.optimizer not in ['sgd', 'rmsprop', 'adam', 'adagrad', 'nesterov_momentum_sgd']:
  121. raise ValueError(
  122. "When reveal_every_iter is True, "
  123. "sshe logistic_param's optimizer not supported, optimizer should be"
  124. " 'sgd', 'rmsprop', 'adam', 'nesterov_momentum_sgd', or 'adagrad'")
  125. else:
  126. if self.optimizer not in ['sgd', 'nesterov_momentum_sgd']:
  127. raise ValueError("When reveal_every_iter is False, "
  128. "sshe logistic_param's optimizer not supported, optimizer should be"
  129. " 'sgd', 'nesterov_momentum_sgd'")
  130. if self.encrypt_param.method not in [consts.PAILLIER, None]:
  131. raise ValueError(
  132. "logistic_param's encrypted method support 'Paillier' or None only")
  133. if self.callback_param.validation_freqs is not None:
  134. if self.reveal_every_iter is False:
  135. raise ValueError(f"When reveal_every_iter is False, validation every iter"
  136. f" is not supported.")
  137. self.reveal_strategy = self.check_and_change_lower(self.reveal_strategy,
  138. ["respectively", "encrypted_reveal_in_host"],
  139. f"{descr} reveal_strategy")
  140. if self.reveal_strategy == "encrypted_reveal_in_host" and self.reveal_every_iter:
  141. raise PermissionError("reveal strategy: encrypted_reveal_in_host mode is not allow to reveal every iter.")
  142. self.encrypted_mode_calculator_param.check()
  143. return True