pipeline-hetero-nn-train-multi.py 4.5 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.component.nn import DatasetParam
  27. from pipeline.interface import Data
  28. from pipeline.utils.tools import load_job_config
  29. fate_torch_hook(t)
  30. def main(config="../../config.yaml", namespace=""):
  31. # obtain config
  32. if isinstance(config, str):
  33. config = load_job_config(config)
  34. parties = config.parties
  35. guest = parties.guest[0]
  36. host = parties.host[0]
  37. guest_train_data = {"name": "vehicle_scale_hetero_guest", "namespace": "experiment"}
  38. host_train_data = {"name": "vehicle_scale_hetero_host", "namespace": "experiment"}
  39. pipeline = PipeLine().set_initiator(role='guest', party_id=guest).set_roles(guest=guest, host=host)
  40. reader_0 = Reader(name="reader_0")
  41. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  42. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  43. data_transform_0 = DataTransform(name="data_transform_0")
  44. data_transform_0.get_party_instance(role='guest', party_id=guest).component_param(with_label=True)
  45. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  46. intersection_0 = Intersection(name="intersection_0")
  47. hetero_nn_0 = HeteroNN(name="hetero_nn_0", epochs=15,
  48. interactive_layer_lr=0.05, batch_size=256, validation_freqs=1, task_type='classification',
  49. selector_param={"method": "relative"})
  50. guest_nn_0 = hetero_nn_0.get_party_instance(role='guest', party_id=guest)
  51. host_nn_0 = hetero_nn_0.get_party_instance(role='host', party_id=host)
  52. # define model
  53. guest_bottom = t.nn.Sequential(
  54. nn.Linear(9, 9),
  55. nn.ReLU(),
  56. )
  57. guest_top = t.nn.Sequential(
  58. nn.Linear(4, 4),
  59. nn.Softmax(dim=1)
  60. )
  61. host_bottom = t.nn.Sequential(
  62. nn.Linear(9, 9),
  63. nn.ReLU(),
  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=9, host_dim=9, 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.05) # you can initialize optimizer without parameters after fate_torch_hook
  72. loss = t.nn.CrossEntropyLoss()
  73. hetero_nn_0.set_interactive_layer(interactive_layer)
  74. # add dataset param, because CrossEntropy loss need flatten long label, so add this parameter
  75. # will use table dataset in federatedml/nn/dataset/table.py
  76. hetero_nn_0.add_dataset(DatasetParam(dataset_name='table', flatten_label=True, label_dtype='long'))
  77. hetero_nn_0.compile(optimizer=optimizer, loss=loss)
  78. evaluation_0 = Evaluation(name='eval_0', eval_type='multi')
  79. # define components IO
  80. pipeline.add_component(reader_0)
  81. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  82. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  83. pipeline.add_component(hetero_nn_0, data=Data(train_data=intersection_0.output.data))
  84. pipeline.add_component(evaluation_0, data=Data(data=hetero_nn_0.output.data))
  85. pipeline.compile()
  86. pipeline.fit()
  87. print(pipeline.get_component("hetero_nn_0").get_summary())
  88. if __name__ == "__main__":
  89. parser = argparse.ArgumentParser("PIPELINE DEMO")
  90. parser.add_argument("-config", type=str,
  91. help="config file")
  92. args = parser.parse_args()
  93. if args.config is not None:
  94. main(args.config)
  95. else:
  96. main()