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 4.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. # ============================================================================
  15. """Training entry file"""
  16. import os
  17. from absl import logging
  18. from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
  19. from mindspore import context, Model
  20. from mindspore.context import ParallelMode
  21. from mindspore.communication.management import init
  22. from mindspore.common import set_seed
  23. from src.dataset import create_dataset
  24. from src.ncf import NCFModel, NetWithLossClass, TrainStepWrap
  25. from utils.moxing_adapter import moxing_wrapper
  26. from utils.config import config
  27. from utils.device_adapter import get_device_id, get_device_num, get_rank_id, get_job_id
  28. set_seed(1)
  29. logging.set_verbosity(logging.INFO)
  30. def modelarts_pre_process():
  31. config.checkpoint_path = os.path.join(config.output_path, str(get_rank_id()), config.checkpoint_path)
  32. @moxing_wrapper(pre_process=modelarts_pre_process)
  33. def run_train():
  34. """train entry method"""
  35. print(config)
  36. print("device id: ", get_device_id())
  37. print("device num: ", get_device_num())
  38. print("rank id: ", get_rank_id())
  39. print("job id: ", get_job_id())
  40. context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
  41. config.is_distributed = bool(get_device_num() > 1)
  42. if config.is_distributed:
  43. config.group_size = get_device_num()
  44. context.reset_auto_parallel_context()
  45. context.set_auto_parallel_context(device_num=config.group_size, parallel_mode=ParallelMode.DATA_PARALLEL,
  46. parameter_broadcast=True, gradients_mean=True)
  47. if config.device_target == "Ascend":
  48. context.set_context(device_id=get_device_id())
  49. init()
  50. elif config.device_target == "GPU":
  51. init()
  52. else:
  53. context.set_context(device_id=get_device_id())
  54. layers = config.layers
  55. num_factors = config.num_factors
  56. epochs = config.train_epochs
  57. ds_train, num_train_users, num_train_items = create_dataset(test_train=True, data_dir=config.data_path,
  58. dataset=config.dataset, train_epochs=1,
  59. batch_size=config.batch_size, num_neg=config.num_neg)
  60. print("ds_train.size: {}".format(ds_train.get_dataset_size()))
  61. ncf_net = NCFModel(num_users=num_train_users,
  62. num_items=num_train_items,
  63. num_factors=num_factors,
  64. model_layers=layers,
  65. mf_regularization=0,
  66. mlp_reg_layers=[0.0, 0.0, 0.0, 0.0],
  67. mf_dim=16)
  68. loss_net = NetWithLossClass(ncf_net)
  69. train_net = TrainStepWrap(loss_net, ds_train.get_dataset_size() * (epochs + 1))
  70. train_net.set_train()
  71. model = Model(train_net)
  72. callback = LossMonitor(per_print_times=ds_train.get_dataset_size())
  73. ckpt_config = CheckpointConfig(save_checkpoint_steps=(4970845+config.batch_size-1)//(config.batch_size),
  74. keep_checkpoint_max=100)
  75. ckpoint_cb = ModelCheckpoint(prefix='NCF', directory=config.checkpoint_path, config=ckpt_config)
  76. model.train(epochs,
  77. ds_train,
  78. callbacks=[TimeMonitor(ds_train.get_dataset_size()), callback, ckpoint_cb],
  79. dataset_sink_mode=True)
  80. print("="*100 + "Training Finish!" + "="*100)
  81. if __name__ == '__main__':
  82. run_train()