configuration_patchtst.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright 2023 The HuggingFace Inc. team. All rights reserved.
  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. """PatchTST model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from transformers.configuration_utils import PreTrainedConfig
  17. from transformers.utils import auto_docstring
  18. @auto_docstring(checkpoint="ibm-granite/granite-timeseries-patchtst")
  19. @strict
  20. class PatchTSTConfig(PreTrainedConfig):
  21. r"""
  22. context_length (`int`, *optional*, defaults to 32):
  23. The context length of the input sequence.
  24. distribution_output (`str`, *optional*, defaults to `"student_t"`):
  25. The distribution emission head for the model when loss is "nll". Could be either "student_t", "normal" or
  26. "negative_binomial".
  27. loss (`str`, *optional*, defaults to `"mse"`):
  28. The loss function for the model corresponding to the `distribution_output` head. For parametric
  29. distributions it is the negative log likelihood ("nll") and for point estimates it is the mean squared
  30. error "mse".
  31. patch_length (`int`, *optional*, defaults to 1):
  32. Define the patch length of the patchification process.
  33. patch_stride (`int`, *optional*, defaults to 1):
  34. Define the stride of the patchification process.
  35. num_attention_heads (`int`, *optional*, defaults to 4):
  36. Number of attention heads for each attention layer in the Transformer encoder.
  37. share_embedding (`bool`, *optional*, defaults to `True`):
  38. Sharing the input embedding across all channels.
  39. channel_attention (`bool`, *optional*, defaults to `False`):
  40. Activate channel attention block in the Transformer to allow channels to attend each other.
  41. ffn_dim (`int`, *optional*, defaults to 512):
  42. Dimension of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
  43. norm_type (`str` , *optional*, defaults to `"batchnorm"`):
  44. Normalization at each Transformer layer. Can be `"batchnorm"` or `"layernorm"`.
  45. norm_eps (`float`, *optional*, defaults to 1e-05):
  46. A value added to the denominator for numerical stability of normalization.
  47. positional_dropout (`float`, *optional*, defaults to 0.0):
  48. The dropout probability in the positional embedding layer.
  49. path_dropout (`float`, *optional*, defaults to 0.0):
  50. The dropout path in the residual block.
  51. ff_dropout (`float`, *optional*, defaults to 0.0):
  52. The dropout probability used between the two layers of the feed-forward networks.
  53. bias (`bool`, *optional*, defaults to `True`):
  54. Whether to add bias in the feed-forward networks.
  55. activation_function (`str`, *optional*, defaults to `"gelu"`):
  56. The non-linear activation function (string) in the Transformer.`"gelu"` and `"relu"` are supported.
  57. pre_norm (`bool`, *optional*, defaults to `True`):
  58. Normalization is applied before self-attention if pre_norm is set to `True`. Otherwise, normalization is
  59. applied after residual block.
  60. positional_encoding_type (`str`, *optional*, defaults to `"sincos"`):
  61. Positional encodings. Options `"random"` and `"sincos"` are supported.
  62. use_cls_token (`bool`, *optional*, defaults to `False`):
  63. Whether cls token is used.
  64. init_std (`float`, *optional*, defaults to 0.02):
  65. The standard deviation of the truncated normal weight initialization distribution.
  66. share_projection (`bool`, *optional*, defaults to `True`):
  67. Sharing the projection layer across different channels in the forecast head.
  68. scaling (`Union`, *optional*, defaults to `"std"`):
  69. Whether to scale the input targets via "mean" scaler, "std" scaler or no scaler if `None`. If `True`, the
  70. scaler is set to "mean".
  71. do_mask_input (`bool`, *optional*):
  72. Apply masking during the pretraining.
  73. mask_type (`str`, *optional*, defaults to `"random"`):
  74. Masking type. Only `"random"` and `"forecast"` are currently supported.
  75. random_mask_ratio (`float`, *optional*, defaults to 0.5):
  76. Masking ratio applied to mask the input data during random pretraining.
  77. num_forecast_mask_patches (`int` or `list`, *optional*, defaults to `[2]`):
  78. Number of patches to be masked at the end of each batch sample. If it is an integer,
  79. all the samples in the batch will have the same number of masked patches. If it is a list,
  80. samples in the batch will be randomly masked by numbers defined in the list. This argument is only used
  81. for forecast pretraining.
  82. channel_consistent_masking (`bool`, *optional*, defaults to `False`):
  83. If channel consistent masking is True, all the channels will have the same masking pattern.
  84. unmasked_channel_indices (`list`, *optional*):
  85. Indices of channels that are not masked during pretraining. Values in the list are number between 1 and
  86. `num_input_channels`
  87. mask_value (`int`, *optional*, defaults to 0):
  88. Values in the masked patches will be filled by `mask_value`.
  89. pooling_type (`str`, *optional*, defaults to `"mean"`):
  90. Pooling of the embedding. `"mean"`, `"max"` and `None` are supported.
  91. head_dropout (`float`, *optional*, defaults to 0.0):
  92. The dropout probability for head.
  93. prediction_length (`int`, *optional*, defaults to 24):
  94. The prediction horizon that the model will output.
  95. num_targets (`int`, *optional*, defaults to 1):
  96. Number of targets for regression and classification tasks. For classification, it is the number of
  97. classes.
  98. output_range (`list`, *optional*):
  99. Output range for regression task. The range of output values can be set to enforce the model to produce
  100. values within a range.
  101. num_parallel_samples (`int`, *optional*, defaults to 100):
  102. The number of samples is generated in parallel for probabilistic prediction.
  103. ```python
  104. >>> from transformers import PatchTSTConfig, PatchTSTModel
  105. >>> # Initializing an PatchTST configuration with 12 time steps for prediction
  106. >>> configuration = PatchTSTConfig(prediction_length=12)
  107. >>> # Randomly initializing a model (with random weights) from the configuration
  108. >>> model = PatchTSTModel(configuration)
  109. >>> # Accessing the model configuration
  110. >>> configuration = model.config
  111. ```
  112. """
  113. model_type = "patchtst"
  114. attribute_map = {
  115. "hidden_size": "d_model",
  116. "num_attention_heads": "num_attention_heads",
  117. "num_hidden_layers": "num_hidden_layers",
  118. }
  119. num_input_channels: int = 1
  120. context_length: int = 32
  121. distribution_output: str = "student_t"
  122. loss: str | None = "mse"
  123. patch_length: int = 1
  124. patch_stride: int = 1
  125. num_hidden_layers: int = 3
  126. d_model: int = 128
  127. num_attention_heads: int = 4
  128. share_embedding: bool = True
  129. channel_attention: bool = False
  130. ffn_dim: int = 512
  131. norm_type: str = "batchnorm"
  132. norm_eps: float = 1e-05
  133. attention_dropout: float | int = 0.0
  134. positional_dropout: float | int = 0.0
  135. path_dropout: float | int = 0.0
  136. ff_dropout: float | int = 0.0
  137. bias: bool = True
  138. activation_function: str = "gelu"
  139. pre_norm: bool = True
  140. positional_encoding_type: str = "sincos"
  141. use_cls_token: bool = False
  142. init_std: float = 0.02
  143. share_projection: bool = True
  144. scaling: str | bool | None = "std"
  145. do_mask_input: bool | None = None
  146. mask_type: str = "random"
  147. random_mask_ratio: float = 0.5
  148. num_forecast_mask_patches: list[int] | tuple[int, ...] | int | None = (2,)
  149. channel_consistent_masking: bool | None = False
  150. unmasked_channel_indices: list[int] | None = None
  151. mask_value: int = 0
  152. pooling_type: str | None = "mean"
  153. head_dropout: float | int = 0.0
  154. prediction_length: int = 24
  155. num_targets: int = 1
  156. output_range: list | None = None
  157. num_parallel_samples: int = 100
  158. __all__ = ["PatchTSTConfig"]