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.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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
  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. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=True)
  23. def get_WideDeep_net(configure):
  24. WideDeep_net = WideDeepModel(configure)
  25. loss_net = NetWithLossClass(WideDeep_net, configure)
  26. train_net = TrainStepWrap(loss_net)
  27. eval_net = PredictWithSigmoid(WideDeep_net)
  28. return train_net, eval_net
  29. class ModelBuilder():
  30. """
  31. Build the model.
  32. """
  33. def __init__(self):
  34. pass
  35. def get_hook(self):
  36. pass
  37. def get_train_hook(self):
  38. hooks = []
  39. callback = LossCallBack()
  40. hooks.append(callback)
  41. if int(os.getenv('DEVICE_ID')) == 0:
  42. pass
  43. return hooks
  44. def get_net(self, configure):
  45. return get_WideDeep_net(configure)
  46. def test_train(configure):
  47. """
  48. test_train
  49. """
  50. data_path = configure.data_path
  51. batch_size = configure.batch_size
  52. epochs = configure.epochs
  53. ds_train = create_dataset(data_path, train_mode=True, epochs=epochs, batch_size=batch_size)
  54. print("ds_train.size: {}".format(ds_train.get_dataset_size()))
  55. net_builder = ModelBuilder()
  56. train_net, _ = net_builder.get_net(configure)
  57. train_net.set_train()
  58. model = Model(train_net)
  59. callback = LossCallBack(config=configure)
  60. ckptconfig = CheckpointConfig(save_checkpoint_steps=1,
  61. keep_checkpoint_max=5)
  62. ckpoint_cb = ModelCheckpoint(prefix='widedeep_train', directory=configure.ckpt_path, config=ckptconfig)
  63. model.train(epochs, ds_train, callbacks=[callback, ckpoint_cb])
  64. if __name__ == "__main__":
  65. config = WideDeepConfig()
  66. config.argparse_init()
  67. test_train(config)