fate-sbt.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #
  16. import argparse
  17. from pipeline.backend.pipeline import PipeLine
  18. from pipeline.component import DataTransform
  19. from pipeline.component import HeteroSecureBoost
  20. from pipeline.component import Intersection
  21. from pipeline.component import Reader
  22. from pipeline.interface import Data
  23. from pipeline.component import Evaluation
  24. from pipeline.interface import Model
  25. from pipeline.utils.tools import load_job_config
  26. from pipeline.utils.tools import JobConfig
  27. from federatedml.evaluation.metrics import regression_metric, classification_metric
  28. from fate_test.utils import extract_data, parse_summary_result
  29. def main(config="../../config.yaml", param="./xgb_config_binary.yaml", namespace=""):
  30. # obtain config
  31. if isinstance(config, str):
  32. config = load_job_config(config)
  33. if isinstance(param, str):
  34. param = JobConfig.load_from_file(param)
  35. parties = config.parties
  36. guest = parties.guest[0]
  37. host = parties.host[0]
  38. # data sets
  39. guest_train_data = {"name": param['data_guest_train'], "namespace": f"experiment{namespace}"}
  40. host_train_data = {"name": param['data_host_train'], "namespace": f"experiment{namespace}"}
  41. guest_validate_data = {"name": param['data_guest_val'], "namespace": f"experiment{namespace}"}
  42. host_validate_data = {"name": param['data_host_val'], "namespace": f"experiment{namespace}"}
  43. # init pipeline
  44. pipeline = PipeLine().set_initiator(role="guest", party_id=guest).set_roles(guest=guest, host=host,)
  45. # set data reader and data-io
  46. reader_0, reader_1 = Reader(name="reader_0"), Reader(name="reader_1")
  47. reader_0.get_party_instance(role="guest", party_id=guest).component_param(table=guest_train_data)
  48. reader_0.get_party_instance(role="host", party_id=host).component_param(table=host_train_data)
  49. reader_1.get_party_instance(role="guest", party_id=guest).component_param(table=guest_validate_data)
  50. reader_1.get_party_instance(role="host", party_id=host).component_param(table=host_validate_data)
  51. data_transform_0, data_transform_1 = DataTransform(name="data_transform_0"), DataTransform(name="data_transform_1")
  52. data_transform_0.get_party_instance(role="guest", party_id=guest).\
  53. component_param(with_label=True, output_format="dense")
  54. data_transform_0.get_party_instance(role="host", party_id=host).component_param(with_label=False)
  55. data_transform_1.get_party_instance(role="guest", party_id=guest).\
  56. component_param(with_label=True, output_format="dense")
  57. data_transform_1.get_party_instance(role="host", party_id=host).component_param(with_label=False)
  58. # data intersect component
  59. intersect_0 = Intersection(name="intersection_0")
  60. intersect_1 = Intersection(name="intersection_1")
  61. # secure boost component
  62. multi_mode = 'single_output'
  63. if 'multi_mode' in param:
  64. multi_mode = param['multi_mode']
  65. hetero_secure_boost_0 = HeteroSecureBoost(name="hetero_secure_boost_0",
  66. num_trees=param['tree_num'],
  67. task_type=param['task_type'],
  68. objective_param={"objective": param['loss_func']},
  69. encrypt_param={"method": "Paillier"},
  70. tree_param={"max_depth": param['tree_depth']},
  71. validation_freqs=1,
  72. learning_rate=param['learning_rate'],
  73. multi_mode=multi_mode
  74. )
  75. hetero_secure_boost_1 = HeteroSecureBoost(name="hetero_secure_boost_1")
  76. # evaluation component
  77. evaluation_0 = Evaluation(name="evaluation_0", eval_type=param['eval_type'])
  78. pipeline.add_component(reader_0)
  79. pipeline.add_component(reader_1)
  80. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  81. pipeline.add_component(data_transform_1,
  82. data=Data(data=reader_1.output.data), model=Model(data_transform_0.output.model))
  83. pipeline.add_component(intersect_0, data=Data(data=data_transform_0.output.data))
  84. pipeline.add_component(intersect_1, data=Data(data=data_transform_1.output.data))
  85. pipeline.add_component(hetero_secure_boost_0, data=Data(train_data=intersect_0.output.data,
  86. validate_data=intersect_1.output.data))
  87. pipeline.add_component(hetero_secure_boost_1, data=Data(test_data=intersect_1.output.data),
  88. model=Model(hetero_secure_boost_0.output.model))
  89. pipeline.add_component(evaluation_0, data=Data(data=hetero_secure_boost_0.output.data))
  90. pipeline.compile()
  91. pipeline.fit()
  92. sbt_0_data = pipeline.get_component("hetero_secure_boost_0").get_output_data()
  93. sbt_1_data = pipeline.get_component("hetero_secure_boost_1").get_output_data()
  94. sbt_0_score = extract_data(sbt_0_data, "predict_result")
  95. sbt_0_label = extract_data(sbt_0_data, "label")
  96. sbt_1_score = extract_data(sbt_1_data, "predict_result")
  97. sbt_1_label = extract_data(sbt_1_data, "label")
  98. sbt_0_score_label = extract_data(sbt_0_data, "predict_result", keep_id=True)
  99. sbt_1_score_label = extract_data(sbt_1_data, "predict_result", keep_id=True)
  100. metric_summary = parse_summary_result(pipeline.get_component("evaluation_0").get_summary())
  101. if param['eval_type'] == "regression":
  102. desc_sbt_0 = regression_metric.Describe().compute(sbt_0_score)
  103. desc_sbt_1 = regression_metric.Describe().compute(sbt_1_score)
  104. metric_summary["script_metrics"] = {"hetero_sbt_train": desc_sbt_0,
  105. "hetero_sbt_validate": desc_sbt_1}
  106. elif param['eval_type'] == "binary":
  107. metric_sbt = {
  108. "score_diversity_ratio": classification_metric.Distribution.compute(sbt_0_score_label, sbt_1_score_label),
  109. "ks_2samp": classification_metric.KSTest.compute(sbt_0_score, sbt_1_score),
  110. "mAP_D_value": classification_metric.AveragePrecisionScore().compute(sbt_0_score, sbt_1_score, sbt_0_label,
  111. sbt_1_label)}
  112. metric_summary["distribution_metrics"] = {"hetero_sbt": metric_sbt}
  113. elif param['eval_type'] == "multi":
  114. metric_sbt = {
  115. "score_diversity_ratio": classification_metric.Distribution.compute(sbt_0_score_label, sbt_1_score_label)}
  116. metric_summary["distribution_metrics"] = {"hetero_sbt": metric_sbt}
  117. data_summary = {"train": {"guest": guest_train_data["name"], "host": host_train_data["name"]},
  118. "test": {"guest": guest_train_data["name"], "host": host_train_data["name"]}
  119. }
  120. return data_summary, metric_summary
  121. if __name__ == "__main__":
  122. parser = argparse.ArgumentParser("BENCHMARK-QUALITY PIPELINE JOB")
  123. parser.add_argument("-config", type=str,
  124. help="config file")
  125. parser.add_argument("-param", type=str,
  126. help="config file for params")
  127. args = parser.parse_args()
  128. if args.config is not None:
  129. main(args.config, args.param)
  130. else:
  131. main()