fate-linr.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 HeteroLinR
  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="./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. }
  71. hetero_linr_0 = HeteroLinR(name='hetero_linr_0', **param)
  72. hetero_linr_1 = HeteroLinR(name='hetero_linr_1')
  73. evaluation_0 = Evaluation(name='evaluation_0', eval_type="regression",
  74. metrics=["r2_score",
  75. "mean_squared_error",
  76. "root_mean_squared_error",
  77. "explained_variance"])
  78. # add components to pipeline, in order of task execution
  79. pipeline.add_component(reader_0)
  80. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  81. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  82. pipeline.add_component(hetero_linr_0, data=Data(train_data=intersection_0.output.data))
  83. pipeline.add_component(hetero_linr_1, data=Data(test_data=intersection_0.output.data),
  84. model=Model(hetero_linr_0.output.model))
  85. pipeline.add_component(evaluation_0, data=Data(data=hetero_linr_0.output.data))
  86. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  87. pipeline.compile()
  88. # fit model
  89. pipeline.fit()
  90. metric_summary = parse_summary_result(pipeline.get_component("evaluation_0").get_summary())
  91. data_linr_0 = extract_data(pipeline.get_component("hetero_linr_0").get_output_data(), "predict_result")
  92. data_linr_1 = extract_data(pipeline.get_component("hetero_linr_1").get_output_data(), "predict_result")
  93. desc_linr_0 = regression_metric.Describe().compute(data_linr_0)
  94. desc_linr_1 = regression_metric.Describe().compute(data_linr_1)
  95. metric_summary["script_metrics"] = {"linr_train": desc_linr_0,
  96. "linr_validate": desc_linr_1}
  97. data_summary = {"train": {"guest": guest_train_data["name"], "host": host_train_data["name"]},
  98. "test": {"guest": guest_train_data["name"], "host": host_train_data["name"]}
  99. }
  100. return data_summary, metric_summary
  101. if __name__ == "__main__":
  102. parser = argparse.ArgumentParser("BENCHMARK-QUALITY FATE JOB")
  103. parser.add_argument("-config", type=str,
  104. help="config file")
  105. parser.add_argument("-param", type=str,
  106. help="config file for params")
  107. args = parser.parse_args()
  108. if args.config is not None:
  109. main(args.config, args.param)
  110. else:
  111. main()