pipeline-homo-lr-train-eval.py 5.1 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 HomoLR
  20. from pipeline.component import Reader
  21. from pipeline.interface import Data
  22. from pipeline.interface import Model
  23. from pipeline.component import Evaluation
  24. from pipeline.component import FeatureScale
  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{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. "callback_param": {
  72. "callbacks": ["EarlyStopping"],
  73. "validation_freqs": 1
  74. },
  75. "init_param": {
  76. "init_method": "zeros"
  77. },
  78. "cv_param": {
  79. "n_splits": 4,
  80. "shuffle": True,
  81. "random_seed": 33,
  82. "need_cv": False
  83. }
  84. }
  85. homo_lr_0 = HomoLR(name='homo_lr_0', **param)
  86. # add components to pipeline, in order of task execution
  87. pipeline.add_component(reader_0)
  88. pipeline.add_component(reader_1)
  89. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  90. pipeline.add_component(data_transform_1, data=Data(data=reader_1.output.data),
  91. model=Model(data_transform_0.output.model))
  92. # set data input sources of intersection components
  93. pipeline.add_component(scale_0, data=Data(data=data_transform_0.output.data))
  94. pipeline.add_component(scale_1, data=Data(data=data_transform_1.output.data),
  95. model=Model(scale_0.output.model))
  96. pipeline.add_component(homo_lr_0, data=Data(train_data=scale_0.output.data,
  97. validate_data=scale_1.output.data))
  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. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  102. pipeline.compile()
  103. # fit model
  104. pipeline.fit()
  105. # query component summary
  106. print(json.dumps(pipeline.get_component("homo_lr_0").get_summary(), indent=4, ensure_ascii=False))
  107. print(json.dumps(pipeline.get_component("evaluation_0").get_summary(), indent=4, ensure_ascii=False))
  108. if __name__ == "__main__":
  109. parser = argparse.ArgumentParser("PIPELINE DEMO")
  110. parser.add_argument("-config", type=str,
  111. help="config file")
  112. args = parser.parse_args()
  113. if args.config is not None:
  114. main(args.config)
  115. else:
  116. main()