pipeline-hetero-nn-train-binary-drop-out.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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
  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=10,
  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. nn.Dropout(p=0.2)
  55. )
  56. guest_top = t.nn.Sequential(
  57. nn.Linear(4, 1),
  58. nn.Sigmoid()
  59. )
  60. host_bottom = t.nn.Sequential(
  61. nn.Linear(20, 4),
  62. nn.ReLU(),
  63. nn.Dropout(p=0.2)
  64. )
  65. # use interactive layer after fate_torch_hook
  66. # add drop out in this layer
  67. interactive_layer = t.nn.InteractiveLayer(out_dim=4, guest_dim=4, host_dim=4, host_num=1, dropout=0.2)
  68. guest_nn_0.add_top_model(guest_top)
  69. guest_nn_0.add_bottom_model(guest_bottom)
  70. host_nn_0.add_bottom_model(host_bottom)
  71. optimizer = t.optim.Adam(lr=0.01) # you can initialize optimizer without parameters after fate_torch_hook
  72. loss = t.nn.BCELoss()
  73. hetero_nn_0.set_interactive_layer(interactive_layer)
  74. hetero_nn_0.compile(optimizer=optimizer, loss=loss)
  75. evaluation_0 = Evaluation(name='eval_0', eval_type='binary')
  76. # define components IO
  77. pipeline.add_component(reader_0)
  78. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  79. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  80. pipeline.add_component(hetero_nn_0, data=Data(train_data=intersection_0.output.data))
  81. pipeline.add_component(evaluation_0, data=Data(data=hetero_nn_0.output.data))
  82. pipeline.compile()
  83. pipeline.fit()
  84. print(pipeline.get_component("hetero_nn_0").get_summary())
  85. if __name__ == "__main__":
  86. parser = argparse.ArgumentParser("PIPELINE DEMO")
  87. parser.add_argument("-config", type=str,
  88. help="config file")
  89. args = parser.parse_args()
  90. if args.config is not None:
  91. main(args.config)
  92. else:
  93. main()