pipeline-hetero-binning-skip-statistic.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. from pipeline.backend.pipeline import PipeLine
  18. from pipeline.component import DataTransform
  19. from pipeline.component import HeteroFeatureBinning
  20. from pipeline.component import Intersection
  21. from pipeline.component import OneHotEncoder
  22. from pipeline.component import Reader
  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. guest_eval_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  35. host_eval_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  36. # initialize pipeline
  37. pipeline = PipeLine()
  38. # set job initiator
  39. pipeline.set_initiator(role='guest', party_id=guest)
  40. pipeline.set_roles(guest=guest, host=host)
  41. # define Reader components to read in data
  42. reader_0 = Reader(name="reader_0")
  43. # configure Reader for guest
  44. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  45. # configure Reader for host
  46. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  47. reader_1 = Reader(name="reader_1")
  48. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_eval_data)
  49. reader_1.get_party_instance(role='host', party_id=host).component_param(table=host_eval_data)
  50. # define DataTransform components
  51. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  52. data_transform_1 = DataTransform(name="data_transform_1")
  53. # get DataTransform party instance of guest
  54. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  55. # configure DataTransform for guest
  56. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense")
  57. # get and configure DataTransform party instance of host
  58. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  59. # define Intersection components
  60. intersection_0 = Intersection(name="intersection_0")
  61. intersection_1 = Intersection(name="intersection_1")
  62. param = {
  63. "method": "quantile",
  64. "compress_thres": 10000,
  65. "head_size": 10000,
  66. "error": 0.001,
  67. "bin_num": 10,
  68. "bin_indexes": -1,
  69. "bin_names": None,
  70. "category_indexes": None,
  71. "category_names": None,
  72. "adjustment_factor": 0.5,
  73. "local_only": False,
  74. "skip_static": True,
  75. "transform_param": {
  76. "transform_cols": -1,
  77. "transform_names": None,
  78. "transform_type": "bin_num"
  79. }
  80. }
  81. hetero_feature_binning_0 = HeteroFeatureBinning(name="hetero_feature_binning_0", **param)
  82. hetero_feature_binning_1 = HeteroFeatureBinning(name='hetero_feature_binning_1')
  83. one_hot_encoder_0 = OneHotEncoder(name='one_hot_encoder_0',
  84. transform_col_indexes=-1,
  85. transform_col_names=None,
  86. need_run=True)
  87. # add components to pipeline, in order of task execution
  88. pipeline.add_component(reader_0)
  89. pipeline.add_component(reader_1)
  90. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  91. # set data_transform_1 to replicate model from data_transform_0
  92. pipeline.add_component(
  93. data_transform_1, data=Data(
  94. data=reader_1.output.data), model=Model(
  95. data_transform_0.output.model))
  96. # set data input sources of intersection components
  97. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  98. pipeline.add_component(intersection_1, data=Data(data=data_transform_1.output.data))
  99. # set train & validate data of hetero_lr_0 component
  100. pipeline.add_component(hetero_feature_binning_0, data=Data(data=intersection_0.output.data))
  101. pipeline.add_component(hetero_feature_binning_1, data=Data(data=intersection_1.output.data),
  102. model=Model(hetero_feature_binning_0.output.model))
  103. pipeline.add_component(one_hot_encoder_0, data=Data(data=hetero_feature_binning_0.output.data))
  104. pipeline.compile()
  105. pipeline.fit()
  106. # common_tools.prettify(pipeline.get_component("hetero_feature_binning_0").get_summary())
  107. if __name__ == "__main__":
  108. parser = argparse.ArgumentParser("PIPELINE DEMO")
  109. parser.add_argument("-config", type=str,
  110. help="config file")
  111. args = parser.parse_args()
  112. if args.config is not None:
  113. main(args.config)
  114. else:
  115. main()