pipeline_hetero_pearson_mix_rand.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright 2019 The FATE Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. import argparse
  16. from pipeline.backend.pipeline import PipeLine
  17. from pipeline.component import DataTransform, HeteroPearson, Intersection, Reader
  18. from pipeline.interface import Data
  19. from pipeline.utils.tools import load_job_config
  20. def main(config="../../config.yaml", namespace=""):
  21. common_param = dict(column_indexes=-1, use_mix_rand=True)
  22. pipeline = run_pearson_pipeline(
  23. config=config,
  24. namespace=namespace,
  25. data=dataset.breast,
  26. common_param=common_param,
  27. )
  28. print(pipeline.get_component("hetero_pearson_0").get_model_param())
  29. print(pipeline.get_component("hetero_pearson_0").get_summary())
  30. def run_pearson_pipeline(
  31. config,
  32. namespace,
  33. data,
  34. common_param=None,
  35. guest_only_param=None,
  36. host_only_param=None,
  37. ):
  38. if isinstance(config, str):
  39. config = load_job_config(config)
  40. guest_data = data["guest"]
  41. host_data = data["host"][0]
  42. guest_data["namespace"] = f"{guest_data['namespace']}{namespace}"
  43. host_data["namespace"] = f"{host_data['namespace']}{namespace}"
  44. pipeline = (
  45. PipeLine()
  46. .set_initiator(role="guest", party_id=config.parties.guest[0])
  47. .set_roles(guest=config.parties.guest[0], host=config.parties.host[0])
  48. )
  49. reader_0 = Reader(name="reader_0")
  50. reader_0.get_party_instance(
  51. role="guest", party_id=config.parties.guest[0]
  52. ).component_param(table=guest_data)
  53. reader_0.get_party_instance(
  54. role="host", party_id=config.parties.host[0]
  55. ).component_param(table=host_data)
  56. data_transform_0 = DataTransform(name="data_transform_0")
  57. data_transform_0.get_party_instance(
  58. role="guest", party_id=config.parties.guest[0]
  59. ).component_param(with_label=True, output_format="dense")
  60. data_transform_0.get_party_instance(
  61. role="host", party_id=config.parties.host[0]
  62. ).component_param(with_label=False)
  63. intersect_0 = Intersection(name="intersection_0")
  64. if common_param is None:
  65. common_param = {}
  66. hetero_pearson_component = HeteroPearson(name="hetero_pearson_0", **common_param)
  67. if guest_only_param:
  68. hetero_pearson_component.get_party_instance(
  69. "guest", config.parties.guest[0]
  70. ).component_param(**guest_only_param)
  71. if host_only_param:
  72. hetero_pearson_component.get_party_instance(
  73. "host", config.parties.host[0]
  74. ).component_param(**host_only_param)
  75. pipeline.add_component(reader_0)
  76. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  77. pipeline.add_component(intersect_0, data=Data(data=data_transform_0.output.data))
  78. pipeline.add_component(
  79. hetero_pearson_component, data=Data(train_data=intersect_0.output.data)
  80. )
  81. pipeline.compile()
  82. pipeline.fit()
  83. return pipeline
  84. class dataset_meta(type):
  85. @property
  86. def breast(cls):
  87. return {
  88. "guest": {"name": "breast_hetero_guest", "namespace": "experiment"},
  89. "host": [{"name": "breast_hetero_host", "namespace": "experiment"}],
  90. }
  91. class dataset(metaclass=dataset_meta):
  92. ...
  93. if __name__ == "__main__":
  94. parser = argparse.ArgumentParser("PIPELINE DEMO")
  95. parser.add_argument("-config", type=str, help="config file")
  96. args = parser.parse_args()
  97. if args.config is not None:
  98. main(args.config)
  99. else:
  100. main()