pipeline-hetero-lr-normal-not-fit-intercept.py 4.9 KB

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