config.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # !/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. ################################################################################
  4. #
  5. # Copyright (c) 2024 Baidu.com, Inc. All Rights Reserved
  6. #
  7. ################################################################################
  8. """
  9. Author: PaddlePaddle Authors
  10. """
  11. import os
  12. import yaml
  13. from ..ts_base.config import BaseTSConfig
  14. from ....utils.misc import abspath
  15. class LongForecastConfig(BaseTSConfig):
  16. """ Long Forecast Config """
  17. def update_input_len(self, seq_len: int):
  18. """
  19. upadte the input sequence length
  20. Args:
  21. seq_len (int): input length
  22. Raises:
  23. TypeError: if seq_len is not dict, raising TypeError
  24. """
  25. if 'seq_len' not in self:
  26. raise RuntimeError(
  27. "Not able to update seq_len, because no seq_len config was found."
  28. )
  29. self.set_val('seq_len', seq_len)
  30. def update_predict_len(self, predict_len: int):
  31. """
  32. updaet the predict sequence length
  33. Args:
  34. predict_len (int): predict length
  35. Raises:
  36. RuntimeError: if predict_len is not set, raising RuntimeError
  37. """
  38. if 'predict_len' not in self:
  39. raise RuntimeError(
  40. "Not able to update predict_len, because no predict_len config was found."
  41. )
  42. self.set_val('predict_len', predict_len)
  43. def update_sampling_stride(self, sampling_stride: int):
  44. """
  45. updaet the sampling stride of sequence to reduce the training time
  46. Args:
  47. sampling_stride (int): sampling rate
  48. Raises:
  49. RuntimeError: if sampling stride is not set, raising RuntimeError
  50. """
  51. if 'sampling_stride' not in self:
  52. raise RuntimeError(
  53. "Not able to update sampling_stride, because no sampling_stride config was found."
  54. )
  55. self.set_val('sampling_stride', sampling_stride)
  56. def update_dataset(self, dataset_dir: str, dataset_type: str=None):
  57. """
  58. upadte the dataset
  59. Args:
  60. dataset_dir (str): dataset root path
  61. dataset_type (str, optional): type to set for dataset. Default='TSDataset'
  62. """
  63. if dataset_type is None:
  64. dataset_type = 'TSDataset'
  65. dataset_dir = abspath(dataset_dir)
  66. ds_cfg = self._make_custom_dataset_config(dataset_dir)
  67. self.update(ds_cfg)
  68. def update_basic_info(self, info_params: dict):
  69. """
  70. update basic info including time_col, freq, target_cols.
  71. Args:
  72. info_params (dict): upadte basic info
  73. Raises:
  74. TypeError: if info_params is not dict, raising TypeError
  75. """
  76. if isinstance(info_params, dict):
  77. self.update({'info_params': info_params})
  78. else:
  79. raise TypeError("`info_params` must be dict.")
  80. def update_patience(self, patience: int):
  81. """
  82. update patience.
  83. Args:
  84. patience (int): upadte patience
  85. Raises:
  86. RuntimeError: if patience is not found, raising RuntimeError
  87. """
  88. if 'patience' not in self.model['model_cfg']:
  89. raise RuntimeError(
  90. "Not able to update patience, because no patience config was found."
  91. )
  92. self.model['model_cfg']['patience'] = patience
  93. def _make_custom_dataset_config(self, dataset_root_path: str):
  94. """construct the dataset config that meets the format requirements
  95. Args:
  96. dataset_root_path (str): the root directory of dataset.
  97. Returns:
  98. dict: the dataset config.
  99. """
  100. ds_cfg = {
  101. 'dataset': {
  102. 'name': 'TSDataset',
  103. 'dataset_root': dataset_root_path,
  104. 'train_path': os.path.join(dataset_root_path, 'train.csv'),
  105. 'val_path': os.path.join(dataset_root_path, 'val.csv'),
  106. },
  107. }
  108. return ds_cfg