fate-sshe-linr.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 HeteroSSHELinR
  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 regression_metric
  26. from fate_test.utils import extract_data, parse_summary_result
  27. def main(config="../../config.yaml", param="./sshe_linr_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. guest_train_data = {"name": "motor_hetero_guest", "namespace": f"experiment{namespace}"}
  38. host_train_data = {"name": "motor_hetero_host", "namespace": f"experiment{namespace}"}
  39. # initialize pipeline
  40. pipeline = PipeLine()
  41. # set job initiator
  42. pipeline.set_initiator(role='guest', party_id=guest)
  43. # set participants information
  44. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  45. # define Reader components to read in data
  46. reader_0 = Reader(name="reader_0")
  47. # configure Reader for guest
  48. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  49. # configure Reader for host
  50. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  51. # define DataTransform components
  52. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  53. # get DataTransform party instance of guest
  54. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  55. # configure DataTransform for guest
  56. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense",
  57. label_name=param["label_name"], label_type="float")
  58. # get and configure DataTransform party instance of host
  59. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  60. # define Intersection component
  61. intersection_0 = Intersection(name="intersection_0")
  62. param = {
  63. "penalty": param["penalty"],
  64. "max_iter": param["max_iter"],
  65. "optimizer": param["optimizer"],
  66. "learning_rate": param["learning_rate"],
  67. "init_param": param["init_param"],
  68. "batch_size": param["batch_size"],
  69. "alpha": param["alpha"],
  70. "early_stop": param["early_stop"],
  71. "reveal_strategy": param["reveal_strategy"],
  72. "tol": 1e-6,
  73. "reveal_every_iter": True
  74. }
  75. hetero_sshe_linr_0 = HeteroSSHELinR(name='hetero_sshe_linr_0', **param)
  76. hetero_sshe_linr_1 = HeteroSSHELinR(name='hetero_sshe_linr_1')
  77. evaluation_0 = Evaluation(name='evaluation_0', eval_type="regression",
  78. metrics=["r2_score",
  79. "mean_squared_error",
  80. "root_mean_squared_error",
  81. "explained_variance"])
  82. # add components to pipeline, in order of task execution
  83. pipeline.add_component(reader_0)
  84. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  85. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  86. pipeline.add_component(hetero_sshe_linr_0, data=Data(train_data=intersection_0.output.data))
  87. pipeline.add_component(hetero_sshe_linr_1, data=Data(test_data=intersection_0.output.data),
  88. model=Model(hetero_sshe_linr_0.output.model))
  89. pipeline.add_component(evaluation_0, data=Data(data=hetero_sshe_linr_0.output.data))
  90. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  91. pipeline.compile()
  92. # fit model
  93. pipeline.fit()
  94. metric_summary = parse_summary_result(pipeline.get_component("evaluation_0").get_summary())
  95. data_linr_0 = extract_data(pipeline.get_component(
  96. "hetero_sshe_linr_0").get_output_data().get("data"), "predict_result")
  97. data_linr_1 = extract_data(pipeline.get_component(
  98. "hetero_sshe_linr_1").get_output_data().get("data"), "predict_result")
  99. desc_linr_0 = regression_metric.Describe().compute(data_linr_0)
  100. desc_linr_1 = regression_metric.Describe().compute(data_linr_1)
  101. metric_summary["script_metrics"] = {"linr_train": desc_linr_0,
  102. "linr_validate": desc_linr_1}
  103. data_summary = {"train": {"guest": guest_train_data["name"], "host": host_train_data["name"]},
  104. "test": {"guest": guest_train_data["name"], "host": host_train_data["name"]}
  105. }
  106. return data_summary, metric_summary
  107. if __name__ == "__main__":
  108. parser = argparse.ArgumentParser("BENCHMARK-QUALITY FATE JOB")
  109. parser.add_argument("-config", type=str,
  110. help="config file")
  111. parser.add_argument("-param", type=str,
  112. help="config file for params")
  113. args = parser.parse_args()
  114. if args.config is not None:
  115. main(args.config, args.param)
  116. else:
  117. main()