callback_param.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. from pipeline.param.base_param import BaseParam
  19. class CallbackParam(BaseParam):
  20. """
  21. Define callback method that used in federated ml.
  22. Parameters
  23. ----------
  24. callbacks : list, default: []
  25. Indicate what kinds of callback functions is desired during the training process.
  26. Accepted values: {'EarlyStopping', 'ModelCheckpoint', 'PerformanceEvaluate'}
  27. validation_freqs: {None, int, list, tuple, set}
  28. validation frequency during training.
  29. early_stopping_rounds: None or int
  30. Will stop training if one metric doesn’t improve in last early_stopping_round rounds
  31. metrics: None, or list, default None
  32. Indicate when executing evaluation during train process, which metrics will be used. If set as empty,
  33. default metrics for specific task type will be used. As for binary classification, default metrics are
  34. ['auc', 'ks']
  35. use_first_metric_only: bool, default: False
  36. Indicate whether use the first metric only for early stopping judgement.
  37. save_freq: int, default: 1
  38. The callbacks save model every save_freq epoch
  39. """
  40. def __init__(self, callbacks=None, validation_freqs=None, early_stopping_rounds=None,
  41. metrics=None, use_first_metric_only=False, save_freq=1):
  42. super(CallbackParam, self).__init__()
  43. self.callbacks = callbacks or []
  44. self.validation_freqs = validation_freqs
  45. self.early_stopping_rounds = early_stopping_rounds
  46. self.metrics = metrics or []
  47. self.use_first_metric_only = use_first_metric_only
  48. self.save_freq = save_freq
  49. def check(self):
  50. if self.early_stopping_rounds is None:
  51. pass
  52. elif isinstance(self.early_stopping_rounds, int):
  53. if self.early_stopping_rounds < 1:
  54. raise ValueError("early stopping rounds should be larger than 0 when it's integer")
  55. if self.validation_freqs is None:
  56. raise ValueError("validation freqs must be set when early stopping is enabled")
  57. if self.validation_freqs is not None:
  58. if type(self.validation_freqs).__name__ not in ["int", "list", "tuple", "set"]:
  59. raise ValueError(
  60. "validation strategy param's validate_freqs's type not supported ,"
  61. " should be int or list or tuple or set"
  62. )
  63. if type(self.validation_freqs).__name__ == "int" and \
  64. self.validation_freqs <= 0:
  65. raise ValueError("validation strategy param's validate_freqs should greater than 0")
  66. if self.metrics is not None and not isinstance(self.metrics, list):
  67. raise ValueError("metrics should be a list")
  68. if not isinstance(self.use_first_metric_only, bool):
  69. raise ValueError("use_first_metric_only should be a boolean")
  70. return True