pipeline-homo-lr-train-eval.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 HomoLR
  20. from pipeline.component import Reader
  21. from pipeline.component import Evaluation
  22. from pipeline.component import FeatureScale
  23. from pipeline.interface import Data
  24. from pipeline.interface import Model
  25. from pipeline.utils.tools import load_job_config
  26. import json
  27. def main(config="../../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. guest_train_data = {"name": "breast_homo_guest", "namespace": f"experiment_sid{namespace}"}
  36. host_train_data = {"name": "breast_homo_host", "namespace": f"experiment_sid{namespace}"}
  37. guest_eval_data = {"name": "breast_homo_guest", "namespace": f"experiment_sid{namespace}"}
  38. host_eval_data = {"name": "breast_homo_host", "namespace": f"experiment_sid{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. reader_1 = Reader(name="reader_1")
  52. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_eval_data)
  53. reader_1.get_party_instance(role='host', party_id=host).component_param(table=host_eval_data)
  54. # define DataTransform components
  55. data_transform_0 = DataTransform(name="data_transform_0", with_match_id=True,
  56. with_label=True, output_format="dense")
  57. data_transform_1 = DataTransform(name="data_transform_1") # start component numbering at 0
  58. scale_0 = FeatureScale(name='scale_0')
  59. scale_1 = FeatureScale(name='scale_1')
  60. param = {
  61. "penalty": "L2",
  62. "optimizer": "sgd",
  63. "tol": 1e-05,
  64. "alpha": 0.01,
  65. "max_iter": 3,
  66. "early_stop": "diff",
  67. "batch_size": 320,
  68. "learning_rate": 0.15,
  69. "validation_freqs": 1,
  70. "init_param": {
  71. "init_method": "zeros"
  72. },
  73. "encrypt_param": {
  74. "method": None
  75. },
  76. "cv_param": {
  77. "n_splits": 4,
  78. "shuffle": True,
  79. "random_seed": 33,
  80. "need_cv": False
  81. }
  82. }
  83. homo_lr_0 = HomoLR(name='homo_lr_0', **param)
  84. # add components to pipeline, in order of task execution
  85. pipeline.add_component(reader_0)
  86. pipeline.add_component(reader_1)
  87. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  88. pipeline.add_component(data_transform_1, data=Data(data=reader_1.output.data),
  89. model=Model(data_transform_0.output.model))
  90. # set data input sources of intersection components
  91. pipeline.add_component(scale_0, data=Data(data=data_transform_0.output.data))
  92. pipeline.add_component(scale_1, data=Data(data=data_transform_1.output.data),
  93. model=Model(scale_0.output.model))
  94. pipeline.add_component(homo_lr_0, data=Data(train_data=scale_0.output.data,
  95. validate_data=scale_1.output.data))
  96. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
  97. evaluation_0.get_party_instance(role='host', party_id=host).component_param(need_run=False)
  98. pipeline.add_component(evaluation_0, data=Data(data=homo_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. print(json.dumps(pipeline.get_component("homo_lr_0").get_summary(), indent=4, ensure_ascii=False))
  105. print(json.dumps(pipeline.get_component("evaluation_0").get_summary(), indent=4, ensure_ascii=False))
  106. if __name__ == "__main__":
  107. parser = argparse.ArgumentParser("PIPELINE DEMO")
  108. parser.add_argument("-config", type=str,
  109. help="config file")
  110. args = parser.parse_args()
  111. if args.config is not None:
  112. main(args.config)
  113. else:
  114. main()