pipeline-hetero-lr-with-validate.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.evaluation import Evaluation
  21. from pipeline.component.hetero_sshe_lr import HeteroSSHELR
  22. from pipeline.component.intersection import Intersection
  23. from pipeline.component.reader import Reader
  24. from pipeline.interface.data import Data
  25. from pipeline.interface.model import Model
  26. from pipeline.utils.tools import load_job_config
  27. def prettify(response, verbose=True):
  28. if verbose:
  29. print(json.dumps(response, indent=4, ensure_ascii=False))
  30. print()
  31. return response
  32. def main(config="../../config.yaml", namespace=""):
  33. if isinstance(config, str):
  34. config = load_job_config(config)
  35. parties = config.parties
  36. guest = parties.guest[0]
  37. hosts = parties.host[0]
  38. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  39. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  40. guest_eval_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  41. host_eval_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  42. # initialize pipeline
  43. pipeline = PipeLine()
  44. # set job initiator
  45. pipeline.set_initiator(role='guest', party_id=guest)
  46. # set participants information
  47. pipeline.set_roles(guest=guest, host=hosts)
  48. # define Reader components to read in data
  49. reader_0 = Reader(name="reader_0")
  50. # configure Reader for guest
  51. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  52. # configure Reader for host
  53. reader_0.get_party_instance(role='host', party_id=hosts).component_param(table=host_train_data)
  54. reader_1 = Reader(name="reader_1")
  55. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_eval_data)
  56. reader_1.get_party_instance(role='host', party_id=hosts).component_param(table=host_eval_data)
  57. data_transform_0 = DataTransform(name="data_transform_0", output_format='dense')
  58. data_transform_1 = DataTransform(name="data_transform_1", output_format='dense')
  59. # get DataTransform party instance of guest
  60. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  61. # configure DataTransform for guest
  62. data_transform_0_guest_party_instance.component_param(with_label=True)
  63. # get and configure DataTransform party instance of host
  64. data_transform_0.get_party_instance(role='host', party_id=hosts).component_param(with_label=False)
  65. # define Intersection components
  66. intersection_0 = Intersection(name="intersection_0")
  67. intersection_1 = Intersection(name="intersection_1")
  68. pipeline.add_component(reader_0)
  69. pipeline.add_component(reader_1)
  70. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  71. pipeline.add_component(data_transform_1, data=Data(data=reader_1.output.data),
  72. model=Model(data_transform_0.output.model))
  73. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  74. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  75. lr_param = {
  76. "name": "hetero_sshe_lr_0",
  77. "penalty": "L2",
  78. "optimizer": "rmsprop",
  79. "tol": 0.0001,
  80. "alpha": 0.01,
  81. "max_iter": 30,
  82. "early_stop": "diff",
  83. "batch_size": -1,
  84. "callback_param": {
  85. "callbacks": [
  86. "EarlyStopping",
  87. "PerformanceEvaluate"
  88. ],
  89. "validation_freqs": 1,
  90. "early_stopping_rounds": 3
  91. },
  92. "learning_rate": 0.15,
  93. "init_param": {
  94. "init_method": "zeros"
  95. },
  96. "reveal_strategy": "respectively",
  97. "reveal_every_iter": True
  98. }
  99. hetero_sshe_lr_0 = HeteroSSHELR(**lr_param)
  100. pipeline.add_component(hetero_sshe_lr_0, data=Data(train_data=intersection_0.output.data,
  101. validate_data=intersection_1.output.data))
  102. evaluation_data = [hetero_sshe_lr_0.output.data]
  103. hetero_sshe_lr_1 = HeteroSSHELR(name='hetero_sshe_lr_1')
  104. pipeline.add_component(hetero_sshe_lr_1, data=Data(test_data=intersection_1.output.data),
  105. model=Model(hetero_sshe_lr_0.output.model))
  106. evaluation_data.append(hetero_sshe_lr_1.output.data)
  107. evaluation_0 = Evaluation(name="evaluation_0", eval_type="binary")
  108. pipeline.add_component(evaluation_0, data=Data(data=evaluation_data))
  109. pipeline.compile()
  110. # fit model
  111. pipeline.fit()
  112. # query component summary
  113. prettify(pipeline.get_component("hetero_sshe_lr_0").get_summary())
  114. prettify(pipeline.get_component("evaluation_0").get_summary())
  115. return pipeline
  116. if __name__ == "__main__":
  117. parser = argparse.ArgumentParser("PIPELINE DEMO")
  118. parser.add_argument("-config", type=str,
  119. help="config file")
  120. args = parser.parse_args()
  121. if args.config is not None:
  122. main(args.config)
  123. else:
  124. main()