pipeline-hetero-binning-model-loader.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 copy
  18. from pipeline.backend.pipeline import PipeLine
  19. from pipeline.component import DataTransform
  20. from pipeline.component import HeteroFeatureBinning
  21. from pipeline.component import Intersection
  22. from pipeline.component import Reader, ModelLoader
  23. from pipeline.interface import Data, Model
  24. from pipeline.utils.tools import load_job_config
  25. def main(config="../../config.yaml", namespace=""):
  26. # obtain config
  27. if isinstance(config, str):
  28. config = load_job_config(config)
  29. parties = config.parties
  30. guest = parties.guest[0]
  31. host = parties.host[0]
  32. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  33. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  34. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host)
  35. reader_0 = Reader(name="reader_0")
  36. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  37. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  38. data_transform_0 = DataTransform(name="data_transform_0")
  39. data_transform_0.get_party_instance(role='guest', party_id=guest).component_param(with_label=True)
  40. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  41. intersection_0 = Intersection(name="intersection_0")
  42. param = {
  43. "method": "quantile",
  44. "optimal_binning_param": {
  45. "metric_method": "gini",
  46. "min_bin_pct": 0.05,
  47. "max_bin_pct": 0.8,
  48. "init_bucket_method": "quantile",
  49. "init_bin_nums": 100,
  50. "mixture": True
  51. },
  52. "compress_thres": 10000,
  53. "head_size": 10000,
  54. "error": 0.001,
  55. "bin_num": 10,
  56. "bin_indexes": [0, 1, 2, 3, 5],
  57. "bin_names": None,
  58. "category_names": None,
  59. "adjustment_factor": 0.5,
  60. "local_only": False,
  61. "transform_param": {
  62. "transform_cols": -1,
  63. "transform_names": None,
  64. "transform_type": "bin_num"
  65. }
  66. }
  67. guest_param = copy.deepcopy(param)
  68. guest_param["category_indexes"] = [0]
  69. host_param = copy.deepcopy(param)
  70. host_param["method"] = "optimal"
  71. hetero_feature_binning_0 = HeteroFeatureBinning(name="hetero_feature_binning_0", **param)
  72. hetero_feature_binning_0.get_party_instance(role="guest", party_id=guest).component_param(**guest_param)
  73. hetero_feature_binning_0.get_party_instance(role="host", party_id=host).component_param(**host_param)
  74. pipeline.add_component(reader_0)
  75. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  76. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  77. pipeline.add_component(hetero_feature_binning_0, data=Data(data=intersection_0.output.data))
  78. pipeline.compile()
  79. pipeline.fit()
  80. loader_pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host)
  81. model_param = {
  82. "model_id": pipeline.get_model_info().model_id,
  83. "model_version": pipeline.get_model_info().model_version,
  84. "component_name": "hetero_feature_binning_0"
  85. }
  86. model_loader_0 = ModelLoader(name="model_loader_0", **model_param)
  87. hetero_feature_binning_1 = HeteroFeatureBinning(name="hetero_feature_binning_1", **param)
  88. hetero_feature_binning_1.get_party_instance(role="host", party_id=host).component_param(
  89. transform_param={"transform_type": "woe"})
  90. hetero_feature_binning_1.get_party_instance(role="guest", party_id=guest).component_param(
  91. **guest_param)
  92. hetero_feature_binning_2 = HeteroFeatureBinning(name="hetero_feature_binning_2",
  93. transform_param={"transform_type": "bin_num"})
  94. # add selected components from train pipeline onto predict pipeline
  95. # specify data source
  96. loader_pipeline.add_component(model_loader_0)
  97. loader_pipeline.add_component(reader_0)
  98. loader_pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  99. loader_pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  100. loader_pipeline.add_component(hetero_feature_binning_1,
  101. data=Data(data=intersection_0.output.data),
  102. model=Model(model=model_loader_0.output.model))
  103. loader_pipeline.add_component(hetero_feature_binning_2,
  104. data=Data(data=intersection_0.output.data),
  105. model=Model(model=hetero_feature_binning_1.output.model))
  106. loader_pipeline.compile()
  107. # run predict model
  108. loader_pipeline.fit()
  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()