pipeline-homo-lr-train.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. import json
  18. from pipeline.backend.pipeline import PipeLine
  19. from pipeline.component import DataTransform
  20. from pipeline.component import Evaluation
  21. from pipeline.component import HomoLR
  22. from pipeline.component import Reader
  23. from pipeline.component import FeatureScale
  24. from pipeline.interface import Data
  25. from pipeline.utils.tools import load_job_config
  26. def main(config="../../config.yaml", namespace=""):
  27. # obtain config
  28. if isinstance(config, str):
  29. config = load_job_config(config)
  30. parties = config.parties
  31. guest = parties.guest[0]
  32. host = parties.host[0]
  33. arbiter = parties.arbiter[0]
  34. guest_train_data = {"name": "breast_homo_guest", "namespace": f"experiment{namespace}"}
  35. host_train_data = {"name": "breast_homo_host", "namespace": f"experiment{namespace}"}
  36. # initialize pipeline
  37. pipeline = PipeLine()
  38. # set job initiator
  39. pipeline.set_initiator(role='guest', party_id=guest)
  40. # set participants information
  41. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  42. # define Reader components to read in data
  43. reader_0 = Reader(name="reader_0")
  44. # configure Reader for guest
  45. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  46. # configure Reader for host
  47. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  48. # define DataTransform components
  49. data_transform_0 = DataTransform(
  50. name="data_transform_0",
  51. with_label=True,
  52. output_format="dense") # start component numbering at 0
  53. scale_0 = FeatureScale(name='scale_0')
  54. param = {
  55. "penalty": "L2",
  56. "optimizer": "sgd",
  57. "tol": 1e-05,
  58. "alpha": 0.01,
  59. "max_iter": 30,
  60. "early_stop": "diff",
  61. "batch_size": -1,
  62. "learning_rate": 0.15,
  63. "decay": 1,
  64. "decay_sqrt": True,
  65. "init_param": {
  66. "init_method": "zeros"
  67. },
  68. "cv_param": {
  69. "n_splits": 4,
  70. "shuffle": True,
  71. "random_seed": 33,
  72. "need_cv": False
  73. }
  74. }
  75. homo_lr_0 = HomoLR(name='homo_lr_0', **param)
  76. # add components to pipeline, in order of task execution
  77. pipeline.add_component(reader_0)
  78. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  79. # set data input sources of intersection components
  80. pipeline.add_component(scale_0, data=Data(data=data_transform_0.output.data))
  81. pipeline.add_component(homo_lr_0, data=Data(train_data=scale_0.output.data))
  82. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
  83. evaluation_0.get_party_instance(role='host', party_id=host).component_param(need_run=False)
  84. pipeline.add_component(evaluation_0, data=Data(data=homo_lr_0.output.data))
  85. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  86. pipeline.compile()
  87. # fit model
  88. pipeline.fit()
  89. deploy_components = [data_transform_0, scale_0, homo_lr_0]
  90. pipeline.deploy_component(components=deploy_components)
  91. #
  92. predict_pipeline = PipeLine()
  93. # # add data reader onto predict pipeline
  94. predict_pipeline.add_component(reader_0)
  95. # # add selected components from train pipeline onto predict pipeline
  96. # # specify data source
  97. predict_pipeline.add_component(
  98. pipeline, data=Data(
  99. predict_input={
  100. pipeline.data_transform_0.input.data: reader_0.output.data}))
  101. predict_pipeline.compile()
  102. predict_pipeline.predict()
  103. dsl_json = predict_pipeline.get_predict_dsl()
  104. conf_json = predict_pipeline.get_predict_conf()
  105. # import json
  106. json.dump(dsl_json, open('./homo-lr-normal-predict-dsl.json', 'w'), indent=4)
  107. json.dump(conf_json, open('./homo-lr-normal-predict-conf.json', 'w'), indent=4)
  108. # query component summary
  109. print(json.dumps(pipeline.get_component("homo_lr_0").get_summary(), indent=4, ensure_ascii=False))
  110. print(json.dumps(pipeline.get_component("evaluation_0").get_summary(), indent=4, ensure_ascii=False))
  111. if __name__ == "__main__":
  112. parser = argparse.ArgumentParser("PIPELINE DEMO")
  113. parser.add_argument("-config", type=str,
  114. help="config file")
  115. args = parser.parse_args()
  116. if args.config is not None:
  117. main(args.config)
  118. else:
  119. main()