You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

train.py 2.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. """ test_training """
  15. import os
  16. from mindspore import Model, context
  17. from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, TimeMonitor
  18. from src.wide_and_deep import PredictWithSigmoid, TrainStepWrap, NetWithLossClass, WideDeepModel
  19. from src.callbacks import LossCallBack
  20. from src.datasets import create_dataset
  21. from src.config import WideDeepConfig
  22. def get_WideDeep_net(configure):
  23. """
  24. Get network of wide&deep model.
  25. """
  26. WideDeep_net = WideDeepModel(configure)
  27. loss_net = NetWithLossClass(WideDeep_net, configure)
  28. train_net = TrainStepWrap(loss_net)
  29. eval_net = PredictWithSigmoid(WideDeep_net)
  30. return train_net, eval_net
  31. class ModelBuilder():
  32. """
  33. Build the model.
  34. """
  35. def __init__(self):
  36. pass
  37. def get_hook(self):
  38. pass
  39. def get_train_hook(self):
  40. hooks = []
  41. callback = LossCallBack()
  42. hooks.append(callback)
  43. if int(os.getenv('DEVICE_ID')) == 0:
  44. pass
  45. return hooks
  46. def get_net(self, configure):
  47. return get_WideDeep_net(configure)
  48. def test_train(configure):
  49. """
  50. test_train
  51. """
  52. data_path = configure.data_path
  53. batch_size = configure.batch_size
  54. epochs = configure.epochs
  55. ds_train = create_dataset(data_path, train_mode=True, epochs=epochs, batch_size=batch_size)
  56. print("ds_train.size: {}".format(ds_train.get_dataset_size()))
  57. net_builder = ModelBuilder()
  58. train_net, _ = net_builder.get_net(configure)
  59. train_net.set_train()
  60. model = Model(train_net)
  61. callback = LossCallBack(config=configure)
  62. ckptconfig = CheckpointConfig(save_checkpoint_steps=ds_train.get_dataset_size(),
  63. keep_checkpoint_max=5)
  64. ckpoint_cb = ModelCheckpoint(prefix='widedeep_train', directory=configure.ckpt_path, config=ckptconfig)
  65. model.train(epochs, ds_train, callbacks=[TimeMonitor(ds_train.get_dataset_size()), callback, ckpoint_cb])
  66. if __name__ == "__main__":
  67. config = WideDeepConfig()
  68. config.argparse_init()
  69. context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
  70. test_train(config)