pipeline-hetero-poisson-cv.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 HeteroPoisson
  20. from pipeline.component import Intersection
  21. from pipeline.component import Reader
  22. from pipeline.interface import Data
  23. from pipeline.utils.tools import load_job_config
  24. def main(config="../../config.yaml", namespace=""):
  25. # obtain config
  26. if isinstance(config, str):
  27. config = load_job_config(config)
  28. parties = config.parties
  29. guest = parties.guest[0]
  30. host = parties.host[0]
  31. arbiter = parties.arbiter[0]
  32. guest_train_data = {"name": "dvisits_hetero_guest", "namespace": f"experiment{namespace}"}
  33. host_train_data = {"name": "dvisits_hetero_host", "namespace": f"experiment{namespace}"}
  34. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host, arbiter=arbiter)
  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(
  40. role='guest',
  41. party_id=guest).component_param(
  42. with_label=True,
  43. label_name="doctorco",
  44. label_type="float",
  45. output_format="dense",
  46. missing_fill=True,
  47. outlier_replace=False)
  48. data_transform_0.get_party_instance(
  49. role='host',
  50. party_id=host).component_param(
  51. with_label=False,
  52. output_format="dense",
  53. outlier_replace=False)
  54. intersection_0 = Intersection(name="intersection_0")
  55. hetero_poisson_0 = HeteroPoisson(name="hetero_poisson_0", early_stop="weight_diff", max_iter=10,
  56. optimizer="rmsprop", tol=0.001, penalty="L2",
  57. alpha=100.0, batch_size=-1, learning_rate=0.01,
  58. exposure_colname="exposure", decay_sqrt=False,
  59. init_param={"init_method": "zeros"},
  60. cv_param={
  61. "n_splits": 5,
  62. "shuffle": False,
  63. "random_seed": 103,
  64. "need_cv": True
  65. })
  66. pipeline.add_component(reader_0)
  67. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  68. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  69. pipeline.add_component(hetero_poisson_0, data=Data(train_data=intersection_0.output.data))
  70. pipeline.compile()
  71. pipeline.fit()
  72. if __name__ == "__main__":
  73. parser = argparse.ArgumentParser("PIPELINE DEMO")
  74. parser.add_argument("-config", type=str,
  75. help="config file")
  76. args = parser.parse_args()
  77. if args.config is not None:
  78. main(args.config)
  79. else:
  80. main()