pipeline-homo-lr-eval.py 5.3 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.interface import Model
  26. from pipeline.utils.tools import load_job_config
  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{namespace}"}
  36. host_train_data = {"name": "breast_homo_host", "namespace": f"experiment{namespace}"}
  37. guest_eval_data = {"name": "breast_homo_guest", "namespace": f"experiment{namespace}"}
  38. host_eval_data = {"name": "breast_homo_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. 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(
  56. name="data_transform_0",
  57. with_label=True,
  58. output_format="dense") # start component numbering at 0
  59. data_transform_1 = DataTransform(name="data_transform_1") # start component numbering at 0
  60. scale_0 = FeatureScale(name='scale_0')
  61. scale_1 = FeatureScale(name='scale_1')
  62. param = {
  63. "penalty": "L2",
  64. "optimizer": "sgd",
  65. "tol": 1e-05,
  66. "alpha": 0.01,
  67. "max_iter": 3,
  68. "early_stop": "diff",
  69. "batch_size": 320,
  70. "learning_rate": 0.15,
  71. "validation_freqs": 1,
  72. "init_param": {
  73. "init_method": "zeros"
  74. },
  75. "cv_param": {
  76. "n_splits": 4,
  77. "shuffle": True,
  78. "random_seed": 33,
  79. "need_cv": False
  80. }
  81. }
  82. homo_lr_0 = HomoLR(name='homo_lr_0', **param)
  83. homo_lr_1 = HomoLR(name='homo_lr_1')
  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. pipeline.add_component(homo_lr_1, data=Data(test_data=scale_1.output.data),
  97. model=Model(homo_lr_0.output.model))
  98. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
  99. evaluation_0.get_party_instance(role='host', party_id=host).component_param(need_run=False)
  100. pipeline.add_component(evaluation_0, data=Data(data=[homo_lr_0.output.data,
  101. homo_lr_1.output.data]))
  102. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  103. pipeline.compile()
  104. # fit model
  105. pipeline.fit()
  106. # query component summary
  107. print(json.dumps(pipeline.get_component("homo_lr_0").get_summary(), indent=4, ensure_ascii=False))
  108. print(json.dumps(pipeline.get_component("evaluation_0").get_summary(), indent=4, ensure_ascii=False))
  109. if __name__ == "__main__":
  110. parser = argparse.ArgumentParser("PIPELINE DEMO")
  111. parser.add_argument("-config", type=str,
  112. help="config file")
  113. args = parser.parse_args()
  114. if args.config is not None:
  115. main(args.config)
  116. else:
  117. main()