fate-sbt.py 7.0 KB

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