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

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. """train_criteo."""
  16. import os
  17. import sys
  18. import argparse
  19. from mindspore import context, ParallelMode
  20. from mindspore.communication.management import init
  21. from mindspore.train.model import Model
  22. from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, TimeMonitor
  23. from src.deepfm import ModelBuilder, AUCMetric
  24. from src.config import DataConfig, ModelConfig, TrainConfig
  25. from src.dataset import create_dataset, DataType
  26. from src.callback import EvalCallBack, LossCallBack
  27. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  28. parser = argparse.ArgumentParser(description='CTR Prediction')
  29. parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
  30. parser.add_argument('--ckpt_path', type=str, default=None, help='Checkpoint path')
  31. parser.add_argument('--eval_file_name', type=str, default="./auc.log", help='eval file path')
  32. parser.add_argument('--loss_file_name', type=str, default="./loss.log", help='loss file path')
  33. parser.add_argument('--do_eval', type=bool, default=True, help='Do evaluation or not.')
  34. args_opt, _ = parser.parse_known_args()
  35. device_id = int(os.getenv('DEVICE_ID'))
  36. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", device_id=device_id)
  37. if __name__ == '__main__':
  38. data_config = DataConfig()
  39. model_config = ModelConfig()
  40. train_config = TrainConfig()
  41. rank_size = int(os.environ.get("RANK_SIZE", 1))
  42. if rank_size > 1:
  43. context.reset_auto_parallel_context()
  44. context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL, mirror_mean=True)
  45. init()
  46. rank_id = int(os.environ.get('RANK_ID'))
  47. else:
  48. rank_size = None
  49. rank_id = None
  50. ds_train = create_dataset(args_opt.dataset_path,
  51. train_mode=True,
  52. epochs=train_config.train_epochs,
  53. batch_size=train_config.batch_size,
  54. data_type=DataType(data_config.data_format),
  55. rank_size=rank_size,
  56. rank_id=rank_id)
  57. model_builder = ModelBuilder(ModelConfig, TrainConfig)
  58. train_net, eval_net = model_builder.get_train_eval_net()
  59. auc_metric = AUCMetric()
  60. model = Model(train_net, eval_network=eval_net, metrics={"auc": auc_metric})
  61. time_callback = TimeMonitor(data_size=ds_train.get_dataset_size())
  62. loss_callback = LossCallBack(loss_file_path=args_opt.loss_file_name)
  63. callback_list = [time_callback, loss_callback]
  64. if train_config.save_checkpoint:
  65. config_ck = CheckpointConfig(save_checkpoint_steps=train_config.save_checkpoint_steps,
  66. keep_checkpoint_max=train_config.keep_checkpoint_max)
  67. ckpt_cb = ModelCheckpoint(prefix=train_config.ckpt_file_name_prefix,
  68. directory=args_opt.ckpt_path,
  69. config=config_ck)
  70. callback_list.append(ckpt_cb)
  71. if args_opt.do_eval:
  72. ds_eval = create_dataset(args_opt.dataset_path, train_mode=False,
  73. epochs=train_config.train_epochs,
  74. batch_size=train_config.batch_size,
  75. data_type=DataType(data_config.data_format))
  76. eval_callback = EvalCallBack(model, ds_eval, auc_metric,
  77. eval_file_path=args_opt.eval_file_name)
  78. callback_list.append(eval_callback)
  79. model.train(train_config.train_epochs, ds_train, callbacks=callback_list)