pipeline-sshe-lr-multi.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 Evaluation
  20. from pipeline.component import HeteroSSHELR
  21. from pipeline.component import Intersection
  22. from pipeline.component import Reader
  23. from pipeline.interface import Data, Model
  24. from pipeline.utils.tools import load_job_config, JobConfig
  25. from federatedml.evaluation.metrics import classification_metric
  26. from fate_test.utils import extract_data, parse_summary_result
  27. def main(config="../../config.yaml", param="./vehicle_sshe_lr_config.yaml", namespace=""):
  28. # obtain config
  29. if isinstance(config, str):
  30. config = load_job_config(config)
  31. parties = config.parties
  32. guest = parties.guest[0]
  33. host = parties.host[0]
  34. arbiter = parties.arbiter[0]
  35. if isinstance(param, str):
  36. param = JobConfig.load_from_file(param)
  37. assert isinstance(param, dict)
  38. data_set = param.get("data_guest").split('/')[-1]
  39. if data_set == "vehicle_scale_hetero_guest.csv":
  40. guest_data_table = 'vehicle_scale_hetero_guest'
  41. host_data_table = 'vehicle_scale_hetero_host'
  42. else:
  43. raise ValueError(f"Cannot recognized data_set: {data_set}")
  44. guest_train_data = {"name": guest_data_table, "namespace": f"experiment{namespace}"}
  45. host_train_data = {"name": host_data_table, "namespace": f"experiment{namespace}"}
  46. # initialize pipeline
  47. pipeline = PipeLine()
  48. # set job initiator
  49. pipeline.set_initiator(role='guest', party_id=guest)
  50. # set participants information
  51. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  52. # define Reader components to read in data
  53. reader_0 = Reader(name="reader_0")
  54. # configure Reader for guest
  55. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  56. # configure Reader for host
  57. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  58. # define DataTransform components
  59. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  60. # get DataTransform party instance of guest
  61. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  62. # configure DataTransform for guest
  63. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense")
  64. # get and configure DataTransform party instance of host
  65. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  66. # define Intersection component
  67. intersection_0 = Intersection(name="intersection_0")
  68. lr_param = {
  69. }
  70. config_param = {
  71. "penalty": param["penalty"],
  72. "max_iter": param["max_iter"],
  73. "alpha": param["alpha"],
  74. "learning_rate": param["learning_rate"],
  75. "optimizer": param["optimizer"], # use sgd
  76. "batch_size": param["batch_size"],
  77. "early_stop": "diff",
  78. "init_param": {
  79. "init_method": param.get("init_method", 'random_uniform'),
  80. "random_seed": param.get("random_seed", 103),
  81. "fit_intercept": True
  82. },
  83. "reveal_strategy": param.get("reveal_strategy", "respectively"),
  84. "reveal_every_iter": True
  85. }
  86. lr_param.update(config_param)
  87. print(f"lr_param: {lr_param}, data_set: {data_set}")
  88. hetero_sshe_lr_0 = HeteroSSHELR(name='hetero_sshe_lr_0', **lr_param)
  89. hetero_sshe_lr_1 = HeteroSSHELR(name='hetero_sshe_lr_1')
  90. evaluation_0 = Evaluation(name='evaluation_0', eval_type="multi")
  91. # add components to pipeline, in order of task execution
  92. pipeline.add_component(reader_0)
  93. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  94. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  95. pipeline.add_component(hetero_sshe_lr_0, data=Data(train_data=intersection_0.output.data))
  96. pipeline.add_component(hetero_sshe_lr_1, data=Data(test_data=intersection_0.output.data),
  97. model=Model(hetero_sshe_lr_0.output.model))
  98. pipeline.add_component(evaluation_0, data=Data(data=hetero_sshe_lr_0.output.data))
  99. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  100. pipeline.compile()
  101. # fit model
  102. pipeline.fit()
  103. # query component summary
  104. result_summary = parse_summary_result(pipeline.get_component("evaluation_0").get_summary())
  105. lr_0_data = pipeline.get_component("hetero_sshe_lr_0").get_output_data()
  106. lr_1_data = pipeline.get_component("hetero_sshe_lr_1").get_output_data()
  107. lr_0_score_label = extract_data(lr_0_data, "predict_result", keep_id=True)
  108. lr_1_score_label = extract_data(lr_1_data, "predict_result", keep_id=True)
  109. metric_lr = {
  110. "score_diversity_ratio": classification_metric.Distribution.compute(lr_0_score_label, lr_1_score_label)}
  111. result_summary["distribution_metrics"] = {"hetero_lr": metric_lr}
  112. data_summary = {"train": {"guest": guest_train_data["name"], "host": host_train_data["name"]},
  113. "test": {"guest": guest_train_data["name"], "host": host_train_data["name"]}
  114. }
  115. print(f"result_summary: {result_summary}; data_summary: {data_summary}")
  116. return data_summary, result_summary
  117. if __name__ == "__main__":
  118. parser = argparse.ArgumentParser("BENCHMARK-QUALITY PIPELINE JOB")
  119. parser.add_argument("-c", "--config", type=str,
  120. help="config file", default="../../config.yaml")
  121. parser.add_argument("-p", "--param", type=str,
  122. help="config file for params", default="./vehicle_sshe_lr_config.yaml")
  123. args = parser.parse_args()
  124. if args.config is not None:
  125. main(args.config, args.param)
  126. else:
  127. main()