pipeline-hetero-sshe-lr-model-loader.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 HeteroSSHELR
  20. from pipeline.component import DataTransform
  21. from pipeline.component import Evaluation
  22. from pipeline.component import Intersection
  23. from pipeline.component import Reader
  24. from pipeline.interface import Data
  25. from pipeline.interface import Model
  26. from pipeline.component import ModelLoader
  27. from pipeline.utils.tools import load_job_config
  28. def prettify(response, verbose=True):
  29. if verbose:
  30. print(json.dumps(response, indent=4, ensure_ascii=False))
  31. print()
  32. return response
  33. def main(config="../../config.yaml", namespace=""):
  34. if isinstance(config, str):
  35. config = load_job_config(config)
  36. parties = config.parties
  37. guest = parties.guest[0]
  38. hosts = parties.host[0]
  39. arbiter = parties.arbiter[0]
  40. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  41. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  42. # guest_train_data = {"name": "default_credit_hetero_guest", "namespace": f"experiment{namespace}"}
  43. # host_train_data = {"name": "default_credit_hetero_host", "namespace": f"experiment{namespace}"}
  44. # initialize pipeline
  45. pipeline = PipeLine()
  46. # set job initiator
  47. pipeline.set_initiator(role='guest', party_id=guest)
  48. # set participants information
  49. pipeline.set_roles(guest=guest, host=hosts, arbiter=arbiter)
  50. # define Reader components to read in data
  51. reader_0 = Reader(name="reader_0")
  52. # configure Reader for guest
  53. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  54. # configure Reader for host
  55. reader_0.get_party_instance(role='host', party_id=hosts).component_param(table=host_train_data)
  56. data_transform_0 = DataTransform(name="data_transform_0", output_format='dense')
  57. # get DataTransform party instance of guest
  58. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  59. # configure DataTransform for guest
  60. data_transform_0_guest_party_instance.component_param(with_label=True)
  61. # get and configure DataTransform party instance of host
  62. data_transform_0.get_party_instance(role='host', party_id=hosts).component_param(with_label=False)
  63. # define Intersection components
  64. intersection_0 = Intersection(name="intersection_0")
  65. pipeline.add_component(reader_0)
  66. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  67. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  68. param = {
  69. "model_id": "",
  70. "model_version": "",
  71. "component_name": "hetero_sshe_lr_0",
  72. "step_index": 7
  73. }
  74. model_loader_0 = ModelLoader(name="model_loader_0", **param)
  75. lr_param = {
  76. "penalty": "L2",
  77. "optimizer": "sgd",
  78. "tol": 0.0001,
  79. "alpha": 0.01,
  80. "early_stop": "diff",
  81. "batch_size": -1,
  82. "learning_rate": 0.15,
  83. "init_param": {
  84. "init_method": "random_uniform",
  85. "fit_intercept": True
  86. },
  87. "callback_param": {
  88. "callbacks": ["ModelCheckpoint"],
  89. "validation_freqs": 1,
  90. "early_stopping_rounds": 1,
  91. "metrics": None,
  92. "use_first_metric_only": False,
  93. "save_freq": 1
  94. }
  95. }
  96. hetero_sshe_lr_0 = HeteroSSHELR(name="hetero_sshe_lr_0", max_iter=10, **lr_param)
  97. pipeline.add_component(model_loader_0)
  98. pipeline.add_component(hetero_sshe_lr_0, data=Data(train_data=intersection_0.output.data),
  99. model=Model(model=model_loader_0.output.model))
  100. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
  101. pipeline.add_component(evaluation_0, data=Data(data=hetero_sshe_lr_0.output.data))
  102. pipeline.compile()
  103. # fit model
  104. pipeline.fit()
  105. # query component summary
  106. prettify(pipeline.get_component("hetero_sshe_lr_0").get_summary())
  107. prettify(pipeline.get_component("evaluation_0").get_summary())
  108. return pipeline
  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()