pipeline-lr-multi.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 HeteroLR
  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_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"],
  76. "batch_size": param["batch_size"],
  77. "masked_rate": 0,
  78. "shuffle": False,
  79. "early_stop": "diff",
  80. "init_param": {
  81. "init_method": param.get("init_method", 'random_uniform'),
  82. "random_seed": param.get("random_seed", 103)
  83. }
  84. }
  85. lr_param.update(config_param)
  86. print(f"lr_param: {lr_param}, data_set: {data_set}")
  87. hetero_lr_0 = HeteroLR(name='hetero_lr_0', **lr_param)
  88. hetero_lr_1 = HeteroLR(name='hetero_lr_1')
  89. evaluation_0 = Evaluation(name='evaluation_0', eval_type="multi")
  90. # add components to pipeline, in order of task execution
  91. pipeline.add_component(reader_0)
  92. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  93. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  94. pipeline.add_component(hetero_lr_0, data=Data(train_data=intersection_0.output.data))
  95. pipeline.add_component(hetero_lr_1, data=Data(test_data=intersection_0.output.data),
  96. model=Model(hetero_lr_0.output.model))
  97. pipeline.add_component(evaluation_0, data=Data(data=hetero_lr_0.output.data))
  98. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  99. pipeline.compile()
  100. # fit model
  101. pipeline.fit()
  102. # query component summary
  103. result_summary = parse_summary_result(pipeline.get_component("evaluation_0").get_summary())
  104. lr_0_data = pipeline.get_component("hetero_lr_0").get_output_data()
  105. lr_1_data = pipeline.get_component("hetero_lr_1").get_output_data()
  106. lr_0_score_label = extract_data(lr_0_data, "predict_result", keep_id=True)
  107. lr_1_score_label = extract_data(lr_1_data, "predict_result", keep_id=True)
  108. metric_lr = {
  109. "score_diversity_ratio": classification_metric.Distribution.compute(lr_0_score_label, lr_1_score_label)}
  110. result_summary["distribution_metrics"] = {"hetero_lr": metric_lr}
  111. data_summary = {"train": {"guest": guest_train_data["name"], "host": host_train_data["name"]},
  112. "test": {"guest": guest_train_data["name"], "host": host_train_data["name"]}
  113. }
  114. return data_summary, result_summary
  115. if __name__ == "__main__":
  116. parser = argparse.ArgumentParser("BENCHMARK-QUALITY PIPELINE JOB")
  117. parser.add_argument("-c", "--config", type=str,
  118. help="config file", default="../../config.yaml")
  119. parser.add_argument("-p", "--param", type=str,
  120. help="config file for params", default="./vehicle_config.yaml")
  121. args = parser.parse_args()
  122. if args.config is not None:
  123. main(args.config, args.param)
  124. else:
  125. main()