pipeline-hetero-nn-train-with-warm_start.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 torch as t
  18. from torch import nn
  19. from pipeline import fate_torch_hook
  20. from pipeline.backend.pipeline import PipeLine
  21. from pipeline.component import DataTransform
  22. from pipeline.component import Evaluation
  23. from pipeline.component import HeteroNN
  24. from pipeline.component import Intersection
  25. from pipeline.component import Reader
  26. from pipeline.interface import Data, Model
  27. from pipeline.utils.tools import load_job_config
  28. fate_torch_hook(t)
  29. def main(config="../../config.yaml", namespace=""):
  30. # obtain config
  31. if isinstance(config, str):
  32. config = load_job_config(config)
  33. parties = config.parties
  34. guest = parties.guest[0]
  35. host = parties.host[0]
  36. guest_train_data = {"name": "breast_hetero_guest", "namespace": "experiment"}
  37. host_train_data = {"name": "breast_hetero_host", "namespace": "experiment"}
  38. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host)
  39. reader_0 = Reader(name="reader_0")
  40. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  41. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  42. data_transform_0 = DataTransform(name="data_transform_0")
  43. data_transform_0.get_party_instance(role='guest', party_id=guest).component_param(with_label=True)
  44. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  45. intersection_0 = Intersection(name="intersection_0")
  46. hetero_nn_0 = HeteroNN(name="hetero_nn_0", epochs=20,
  47. interactive_layer_lr=0.01, batch_size=-1, validation_freqs=1, task_type='classification')
  48. guest_nn_0 = hetero_nn_0.get_party_instance(role='guest', party_id=guest)
  49. host_nn_0 = hetero_nn_0.get_party_instance(role='host', party_id=host)
  50. # define model
  51. guest_bottom = t.nn.Sequential(
  52. nn.Linear(10, 4),
  53. nn.ReLU()
  54. )
  55. guest_top = t.nn.Sequential(
  56. nn.Linear(4, 1),
  57. nn.Sigmoid()
  58. )
  59. host_bottom = t.nn.Sequential(
  60. nn.Linear(20, 4),
  61. nn.ReLU()
  62. )
  63. # use interactive layer after fate_torch_hook
  64. interactive_layer = t.nn.InteractiveLayer(out_dim=4, guest_dim=4, host_dim=4, host_num=1, dropout=0.2)
  65. guest_nn_0.add_top_model(guest_top)
  66. guest_nn_0.add_bottom_model(guest_bottom)
  67. host_nn_0.add_bottom_model(host_bottom)
  68. optimizer = t.optim.Adam(lr=0.01) # you can initialize optimizer without parameters after fate_torch_hook
  69. loss = t.nn.BCELoss()
  70. hetero_nn_0.set_interactive_layer(interactive_layer)
  71. hetero_nn_0.compile(optimizer=optimizer, loss=loss)
  72. hetero_nn_1 = HeteroNN(name="hetero_nn_1", epochs=10,
  73. interactive_layer_lr=0.01, batch_size=128, validation_freqs=1, task_type='classification')
  74. evaluation_0 = Evaluation(name='eval_0', eval_type='binary')
  75. # define components IO
  76. pipeline.add_component(reader_0)
  77. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  78. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  79. pipeline.add_component(hetero_nn_0, data=Data(train_data=intersection_0.output.data))
  80. pipeline.add_component(hetero_nn_1, data=Data(train_data=intersection_0.output.data),
  81. model=Model(model=hetero_nn_0.output.model))
  82. pipeline.add_component(evaluation_0, data=Data(data=hetero_nn_0.output.data))
  83. pipeline.compile()
  84. pipeline.fit()
  85. print(pipeline.get_component("hetero_nn_0").get_summary())
  86. if __name__ == "__main__":
  87. parser = argparse.ArgumentParser("PIPELINE DEMO")
  88. parser.add_argument("-config", type=str,
  89. help="config file")
  90. args = parser.parse_args()
  91. if args.config is not None:
  92. main(args.config)
  93. else:
  94. main()