trainer.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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. import os
  15. import json
  16. import time
  17. import tarfile
  18. from pathlib import Path
  19. from ..base import BaseTrainer
  20. from ...utils.config import AttrDict
  21. from .model_list import MODELS
  22. class TSCLSTrainer(BaseTrainer):
  23. """TS Classification Model Trainer"""
  24. entities = MODELS
  25. def train(self):
  26. """firstly, update and dump train config, then train model"""
  27. # XXX: using super().train() instead when the train_hook() is supported.
  28. os.makedirs(self.global_config.output, exist_ok=True)
  29. self.update_config()
  30. self.dump_config()
  31. train_result = self.pdx_model.train(**self.get_train_kwargs())
  32. assert (
  33. train_result.returncode == 0
  34. ), f"Encountered an unexpected error({train_result.returncode}) in \
  35. training!"
  36. self.make_tar_file()
  37. def make_tar_file(self):
  38. """make tar file to package the training outputs"""
  39. tar_path = Path(self.global_config.output) / "best_accuracy.pdparams.tar"
  40. with tarfile.open(tar_path, "w") as tar:
  41. tar.add(self.global_config.output, arcname="best_accuracy.pdparams")
  42. def update_config(self):
  43. """update training config"""
  44. self.pdx_config.update_dataset(self.global_config.dataset_dir, "TSCLSDataset")
  45. if self.train_config.time_col is not None:
  46. self.pdx_config.update_basic_info({"time_col": self.train_config.time_col})
  47. if self.train_config.target_cols is not None:
  48. self.pdx_config.update_basic_info(
  49. {"target_cols": self.train_config.target_cols.split(",")}
  50. )
  51. if self.train_config.group_id is not None:
  52. self.pdx_config.update_basic_info({"group_id": self.train_config.group_id})
  53. if self.train_config.static_cov_cols is not None:
  54. self.pdx_config.update_basic_info(
  55. {"static_cov_cols": self.train_config.static_cov_cols}
  56. )
  57. if self.train_config.freq is not None:
  58. try:
  59. self.train_config.freq = int(self.train_config.freq)
  60. except ValueError:
  61. pass
  62. self.pdx_config.update_basic_info({"freq": self.train_config.freq})
  63. if self.train_config.batch_size is not None:
  64. self.pdx_config.update_batch_size(self.train_config.batch_size)
  65. if self.train_config.learning_rate is not None:
  66. self.pdx_config.update_learning_rate(self.train_config.learning_rate)
  67. if self.train_config.epochs_iters is not None:
  68. self.pdx_config.update_epochs(self.train_config.epochs_iters)
  69. if self.train_config.log_interval is not None:
  70. self.pdx_config.update_log_interval(self.train_config.log_interval)
  71. if self.global_config.output is not None:
  72. self.pdx_config.update_save_dir(self.global_config.output)
  73. def get_train_kwargs(self) -> dict:
  74. """get key-value arguments of model training function
  75. Returns:
  76. dict: the arguments of training function.
  77. """
  78. train_args = {"device": self.get_device()}
  79. if self.global_config.output is not None:
  80. train_args["save_dir"] = self.global_config.output
  81. return train_args