pipeline-hetero-lr-ovr-encrypted-reveal-in-host.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 HeteroFeatureSelection
  22. from pipeline.component import HeteroSSHELR
  23. from pipeline.component import Intersection
  24. from pipeline.component import Reader
  25. from pipeline.interface import Data
  26. from pipeline.interface import Model
  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. guest_train_data = {"name": "vehicle_scale_hetero_guest", "namespace": f"experiment{namespace}"}
  40. host_train_data = {"name": "vehicle_scale_hetero_host", "namespace": f"experiment{namespace}"}
  41. guest_eval_data = {"name": "vehicle_scale_hetero_guest", "namespace": f"experiment{namespace}"}
  42. host_eval_data = {"name": "vehicle_scale_hetero_host", "namespace": f"experiment{namespace}"}
  43. # initialize pipeline
  44. pipeline = PipeLine()
  45. # set job initiator
  46. pipeline.set_initiator(role='guest', party_id=guest)
  47. # set participants information
  48. pipeline.set_roles(guest=guest, host=hosts)
  49. # define Reader components to read in data
  50. reader_0 = Reader(name="reader_0")
  51. # configure Reader for guest
  52. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  53. # configure Reader for host
  54. reader_0.get_party_instance(role='host', party_id=hosts).component_param(table=host_train_data)
  55. reader_1 = Reader(name="reader_1")
  56. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_eval_data)
  57. reader_1.get_party_instance(role='host', party_id=hosts).component_param(table=host_eval_data)
  58. data_transform_0 = DataTransform(name="data_transform_0", output_format='dense')
  59. data_transform_1 = DataTransform(name="data_transform_1", output_format='dense')
  60. # get DataTransform party instance of guest
  61. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  62. # configure DataTransform for guest
  63. data_transform_0_guest_party_instance.component_param(with_label=True)
  64. # get and configure DataTransform party instance of host
  65. data_transform_0.get_party_instance(role='host', party_id=hosts).component_param(with_label=False)
  66. # define Intersection components
  67. intersection_0 = Intersection(name="intersection_0")
  68. intersection_1 = Intersection(name="intersection_1")
  69. selection_param = {
  70. "select_col_indexes": -1,
  71. "filter_methods": ["manually"]
  72. }
  73. hetero_feature_selection_0 = HeteroFeatureSelection(name="hetero_feature_selection_0",
  74. **selection_param)
  75. hetero_feature_selection_0.get_party_instance(role='guest', party_id=guest).component_param(
  76. manually_param={"left_col_indexes": [0]}
  77. )
  78. hetero_feature_selection_1 = HeteroFeatureSelection(name="hetero_feature_selection_1")
  79. pipeline.add_component(reader_0)
  80. pipeline.add_component(reader_1)
  81. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  82. pipeline.add_component(data_transform_1, data=Data(data=reader_1.output.data),
  83. model=Model(data_transform_0.output.model))
  84. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  85. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  86. pipeline.add_component(hetero_feature_selection_0, data=Data(data=intersection_0.output.data))
  87. pipeline.add_component(hetero_feature_selection_1, data=Data(data=intersection_1.output.data),
  88. model=Model(hetero_feature_selection_0.output.model))
  89. lr_param = {
  90. "name": "hetero_sshe_lr_0",
  91. "penalty": None,
  92. "optimizer": "sgd",
  93. "tol": 0.0001,
  94. "alpha": 0.01,
  95. "max_iter": 1,
  96. "early_stop": "diff",
  97. "batch_size": -1,
  98. "learning_rate": 0.15,
  99. "init_param": {
  100. "init_method": "random_uniform"
  101. },
  102. "reveal_strategy": "encrypted_reveal_in_host",
  103. "reveal_every_iter": False
  104. }
  105. hetero_sshe_lr_0 = HeteroSSHELR(**lr_param)
  106. pipeline.add_component(hetero_sshe_lr_0, data=Data(train_data=hetero_feature_selection_0.output.data,
  107. validate_data=hetero_feature_selection_1.output.data))
  108. evaluation_data = [hetero_sshe_lr_0.output.data]
  109. hetero_sshe_lr_1 = HeteroSSHELR(name='hetero_sshe_lr_1')
  110. pipeline.add_component(hetero_sshe_lr_1, data=Data(test_data=hetero_feature_selection_1.output.data),
  111. model=Model(hetero_sshe_lr_0.output.model))
  112. evaluation_data.append(hetero_sshe_lr_1.output.data)
  113. evaluation_0 = Evaluation(name="evaluation_0", eval_type="multi")
  114. pipeline.add_component(evaluation_0, data=Data(data=evaluation_data))
  115. pipeline.compile()
  116. # fit model
  117. pipeline.fit()
  118. # query component summary
  119. prettify(pipeline.get_component("hetero_sshe_lr_0").get_summary())
  120. prettify(pipeline.get_component("evaluation_0").get_summary())
  121. return pipeline
  122. if __name__ == "__main__":
  123. parser = argparse.ArgumentParser("PIPELINE DEMO")
  124. parser.add_argument("-config", type=str,
  125. help="config file")
  126. args = parser.parse_args()
  127. if args.config is not None:
  128. main(args.config)
  129. else:
  130. main()