The PatchTST 모델은 Yuqi Nie, Nam H. Nguyen, Phanwadee Sinthong, Jayant Kalagnanam이 제안한 시계열 하나가 64개의 단어만큼 가치있다: 트랜스포머를 이용한 장기예측라는 논문에서 소개되었습니다.
이 모델은 고수준에서 시계열을 주어진 크기의 패치로 벡터화하고, 결과로 나온 벡터 시퀀스를 트랜스포머를 통해 인코딩한 다음 적절한 헤드를 통해 예측 길이의 예측을 출력합니다. 모델은 다음 그림과 같이 도식화됩니다:
해당 논문의 초록입니다:
*우리는 다변량 시계열 예측과 자기 감독 표현 학습을 위한 효율적인 트랜스포머 기반 모델 설계를 제안합니다. 이는 두 가지 주요 구성 요소를 기반으로 합니다:
(i) 시계열을 하위 시리즈 수준의 패치로 분할하여 트랜스포머의 입력 토큰으로 사용 (ii) 각 채널이 모든 시리즈에 걸쳐 동일한 임베딩과 트랜스포머 가중치를 공유하는 단일 단변량 시계열을 포함하는 채널 독립성. 패칭 설계는 자연스럽게 세 가지 이점을 가집니다:
이 모델은 namctin, gsinthong, diepi, vijaye12, wmgifford, kashif에 의해 기여 되었습니다. 원본코드는 이곳에서 확인할 수 있습니다.
이 모델은 시계열 분류와 시계열 회귀에도 사용될 수 있습니다. 각각 PatchTSTForClassification와 PatchTSTForRegression 클래스를 참조하세요.
( num_input_channels: int = 1 context_length: int = 32 distribution_output: str = 'student_t' loss: str = 'mse' patch_length: int = 1 patch_stride: int = 1 num_hidden_layers: int = 3 d_model: int = 128 num_attention_heads: int = 4 share_embedding: bool = True channel_attention: bool = False ffn_dim: int = 512 norm_type: str = 'batchnorm' norm_eps: float = 1e-05 attention_dropout: float = 0.0 positional_dropout: float = 0.0 path_dropout: float = 0.0 ff_dropout: float = 0.0 bias: bool = True activation_function: str = 'gelu' pre_norm: bool = True positional_encoding_type: str = 'sincos' use_cls_token: bool = False init_std: float = 0.02 share_projection: bool = True scaling: typing.Union[bool, str, NoneType] = 'std' do_mask_input: typing.Optional[bool] = None mask_type: str = 'random' random_mask_ratio: float = 0.5 num_forecast_mask_patches: typing.Union[typing.List[int], int, NoneType] = [2] channel_consistent_masking: typing.Optional[bool] = False unmasked_channel_indices: typing.Optional[typing.List[int]] = None mask_value: int = 0 pooling_type: str = 'mean' head_dropout: float = 0.0 prediction_length: int = 24 num_targets: int = 1 output_range: typing.Optional[typing.List] = None num_parallel_samples: int = 100 **kwargs )
Parameters
int
, optional, defaults to 1) —
The size of the target variable which by default is 1 for univariate targets. Would be > 1 in case of
multivariate targets. int
, optional, defaults to 32) —
The context length of the input sequence. str
, optional, defaults to "student_t"
) —
The distribution emission head for the model when loss is “nll”. Could be either “student_t”, “normal” or
“negative_binomial”. str
, optional, defaults to "mse"
) —
The loss function for the model corresponding to the distribution_output
head. For parametric
distributions it is the negative log likelihood (“nll”) and for point estimates it is the mean squared
error “mse”. int
, optional, defaults to 1) —
Define the patch length of the patchification process. int
, optional, defaults to 1) —
Define the stride of the patchification process. int
, optional, defaults to 3) —
Number of hidden layers. int
, optional, defaults to 128) —
Dimensionality of the transformer layers. int
, optional, defaults to 4) —
Number of attention heads for each attention layer in the Transformer encoder. bool
, optional, defaults to True
) —
Sharing the input embedding across all channels. bool
, optional, defaults to False
) —
Activate channel attention block in the Transformer to allow channels to attend each other. int
, optional, defaults to 512) —
Dimension of the “intermediate” (often named feed-forward) layer in the Transformer encoder. str
, optional, defaults to "batchnorm"
) —
Normalization at each Transformer layer. Can be "batchnorm"
or "layernorm"
. float
, optional, defaults to 1e-05) —
A value added to the denominator for numerical stability of normalization. float
, optional, defaults to 0.0) —
The dropout probability for the attention probabilities. float
, optional, defaults to 0.0) —
The dropout probability in the positional embedding layer. float
, optional, defaults to 0.0) —
The dropout path in the residual block. float
, optional, defaults to 0.0) —
The dropout probability used between the two layers of the feed-forward networks. bool
, optional, defaults to True
) —
Whether to add bias in the feed-forward networks. str
, optional, defaults to "gelu"
) —
The non-linear activation function (string) in the Transformer."gelu"
and "relu"
are supported. bool
, optional, defaults to True
) —
Normalization is applied before self-attention if pre_norm is set to True
. Otherwise, normalization is
applied after residual block. str
, optional, defaults to "sincos"
) —
Positional encodings. Options "random"
and "sincos"
are supported. bool
, optional, defaults to False
) —
Whether cls token is used. float
, optional, defaults to 0.02) —
The standard deviation of the truncated normal weight initialization distribution. bool
, optional, defaults to True
) —
Sharing the projection layer across different channels in the forecast head. Union
, optional, defaults to "std"
) —
Whether to scale the input targets via “mean” scaler, “std” scaler or no scaler if None
. If True
, the
scaler is set to “mean”. bool
, optional) —
Apply masking during the pretraining. str
, optional, defaults to "random"
) —
Masking type. Only "random"
and "forecast"
are currently supported. float
, optional, defaults to 0.5) —
Masking ratio applied to mask the input data during random pretraining. int
or list
, optional, defaults to [2]
) —
Number of patches to be masked at the end of each batch sample. If it is an integer,
all the samples in the batch will have the same number of masked patches. If it is a list,
samples in the batch will be randomly masked by numbers defined in the list. This argument is only used
for forecast pretraining. bool
, optional, defaults to False
) —
If channel consistent masking is True, all the channels will have the same masking pattern. list
, optional) —
Indices of channels that are not masked during pretraining. Values in the list are number between 1 and
num_input_channels
int
, optional, defaults to 0) —
Values in the masked patches will be filled by mask_value
. str
, optional, defaults to "mean"
) —
Pooling of the embedding. "mean"
, "max"
and None
are supported. float
, optional, defaults to 0.0) —
The dropout probability for head. int
, optional, defaults to 24) —
The prediction horizon that the model will output. int
, optional, defaults to 1) —
Number of targets for regression and classification tasks. For classification, it is the number of
classes. list
, optional) —
Output range for regression task. The range of output values can be set to enforce the model to produce
values within a range. int
, optional, defaults to 100) —
The number of samples is generated in parallel for probabilistic prediction. This is the configuration class to store the configuration of an PatchTSTModel. It is used to instantiate an PatchTST model according to the specified arguments, defining the model architecture. ibm/patchtst architecture.
Configuration objects inherit from PretrainedConfig can be used to control the model outputs. Read the documentation from PretrainedConfig for more information.
>>> from transformers import PatchTSTConfig, PatchTSTModel
>>> # Initializing an PatchTST configuration with 12 time steps for prediction
>>> configuration = PatchTSTConfig(prediction_length=12)
>>> # Randomly initializing a model (with random weights) from the configuration
>>> model = PatchTSTModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
( config: PatchTSTConfig )
Parameters
The bare PatchTST Model outputting raw hidden-states without any specific head. This model inherits from PreTrainedModel. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.)
This model is also a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
( past_values: Tensor past_observed_mask: typing.Optional[torch.Tensor] = None future_values: typing.Optional[torch.Tensor] = None output_hidden_states: typing.Optional[bool] = None output_attentions: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None )
Parameters
torch.Tensor
of shape (bs, sequence_length, num_input_channels)
, required) —
Input sequence to the model torch.BoolTensor
of shape (batch_size, sequence_length, num_input_channels)
, optional) —
Boolean mask to indicate which past_values
were observed and which were missing. Mask values selected
in [0, 1]
:
torch.BoolTensor
of shape (batch_size, prediction_length, num_input_channels)
, optional) —
Future target values associated with the past_values
bool
, optional) —
Whether or not to return the hidden states of all layers bool
, optional) —
Whether or not to return the output attention of all layers bool
, optional) —
Whether or not to return a ModelOutput
instead of a plain tuple. Examples:
>>> from huggingface_hub import hf_hub_download
>>> import torch
>>> from transformers import PatchTSTModel
>>> file = hf_hub_download(
... repo_id="hf-internal-testing/etth1-hourly-batch", filename="train-batch.pt", repo_type="dataset"
... )
>>> batch = torch.load(file)
>>> model = PatchTSTModel.from_pretrained("namctin/patchtst_etth1_pretrain")
>>> # during training, one provides both past and future values
>>> outputs = model(
... past_values=batch["past_values"],
... future_values=batch["future_values"],
... )
>>> last_hidden_state = outputs.last_hidden_state
( config: PatchTSTConfig )
Parameters
The PatchTST for prediction model. This model inherits from PreTrainedModel. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.)
This model is also a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
( past_values: Tensor past_observed_mask: typing.Optional[torch.Tensor] = None future_values: typing.Optional[torch.Tensor] = None output_hidden_states: typing.Optional[bool] = None output_attentions: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None )
Parameters
torch.Tensor
of shape (bs, sequence_length, num_input_channels)
, required) —
Input sequence to the model torch.BoolTensor
of shape (batch_size, sequence_length, num_input_channels)
, optional) —
Boolean mask to indicate which past_values
were observed and which were missing. Mask values selected
in [0, 1]
:
torch.Tensor
of shape (bs, forecast_len, num_input_channels)
, optional) —
Future target values associated with the past_values
bool
, optional) —
Whether or not to return the hidden states of all layers bool
, optional) —
Whether or not to return the output attention of all layers bool
, optional) —
Whether or not to return a ModelOutput
instead of a plain tuple. Examples:
>>> from huggingface_hub import hf_hub_download
>>> import torch
>>> from transformers import PatchTSTConfig, PatchTSTForPrediction
>>> file = hf_hub_download(
... repo_id="hf-internal-testing/etth1-hourly-batch", filename="train-batch.pt", repo_type="dataset"
... )
>>> batch = torch.load(file)
>>> # Prediction task with 7 input channels and prediction length is 96
>>> model = PatchTSTForPrediction.from_pretrained("namctin/patchtst_etth1_forecast")
>>> # during training, one provides both past and future values
>>> outputs = model(
... past_values=batch["past_values"],
... future_values=batch["future_values"],
... )
>>> loss = outputs.loss
>>> loss.backward()
>>> # during inference, one only provides past values, the model outputs future values
>>> outputs = model(past_values=batch["past_values"])
>>> prediction_outputs = outputs.prediction_outputs
( config: PatchTSTConfig )
Parameters
The PatchTST for classification model. This model inherits from PreTrainedModel. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.)
This model is also a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
( past_values: Tensor target_values: Tensor = None past_observed_mask: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None output_attentions: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None )
Parameters
torch.Tensor
of shape (bs, sequence_length, num_input_channels)
, required) —
Input sequence to the model torch.Tensor
, optional) —
Labels associates with the past_values
torch.BoolTensor
of shape (batch_size, sequence_length, num_input_channels)
, optional) —
Boolean mask to indicate which past_values
were observed and which were missing. Mask values selected
in [0, 1]
:
bool
, optional) —
Whether or not to return the hidden states of all layers bool
, optional) —
Whether or not to return the output attention of all layers bool
, optional) —
Whether or not to return a ModelOutput
instead of a plain tuple. Examples:
>>> from transformers import PatchTSTConfig, PatchTSTForClassification
>>> # classification task with two input channel2 and 3 classes
>>> config = PatchTSTConfig(
... num_input_channels=2,
... num_targets=3,
... context_length=512,
... patch_length=12,
... stride=12,
... use_cls_token=True,
... )
>>> model = PatchTSTForClassification(config=config)
>>> # during inference, one only provides past values
>>> past_values = torch.randn(20, 512, 2)
>>> outputs = model(past_values=past_values)
>>> labels = outputs.prediction_logits
( config: PatchTSTConfig )
Parameters
The PatchTST for pretrain model. This model inherits from PreTrainedModel. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.)
This model is also a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
( past_values: Tensor past_observed_mask: typing.Optional[torch.Tensor] = None output_hidden_states: typing.Optional[bool] = None output_attentions: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None )
Parameters
torch.Tensor
of shape (bs, sequence_length, num_input_channels)
, required) —
Input sequence to the model torch.BoolTensor
of shape (batch_size, sequence_length, num_input_channels)
, optional) —
Boolean mask to indicate which past_values
were observed and which were missing. Mask values selected
in [0, 1]
:
bool
, optional) —
Whether or not to return the hidden states of all layers bool
, optional) —
Whether or not to return the output attention of all layers bool
, optional) — Whether or not to return a ModelOutput
instead of a plain tuple. Examples:
>>> from huggingface_hub import hf_hub_download
>>> import torch
>>> from transformers import PatchTSTConfig, PatchTSTForPretraining
>>> file = hf_hub_download(
... repo_id="hf-internal-testing/etth1-hourly-batch", filename="train-batch.pt", repo_type="dataset"
... )
>>> batch = torch.load(file)
>>> # Config for random mask pretraining
>>> config = PatchTSTConfig(
... num_input_channels=7,
... context_length=512,
... patch_length=12,
... stride=12,
... mask_type='random',
... random_mask_ratio=0.4,
... use_cls_token=True,
... )
>>> # Config for forecast mask pretraining
>>> config = PatchTSTConfig(
... num_input_channels=7,
... context_length=512,
... patch_length=12,
... stride=12,
... mask_type='forecast',
... num_forecast_mask_patches=5,
... use_cls_token=True,
... )
>>> model = PatchTSTForPretraining(config)
>>> # during training, one provides both past and future values
>>> outputs = model(past_values=batch["past_values"])
>>> loss = outputs.loss
>>> loss.backward()
( config: PatchTSTConfig )
Parameters
The PatchTST for regression model. This model inherits from PreTrainedModel. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.)
This model is also a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
( past_values: Tensor target_values: Tensor = None past_observed_mask: typing.Optional[torch.Tensor] = None output_hidden_states: typing.Optional[bool] = None output_attentions: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None )
Parameters
torch.Tensor
of shape (bs, sequence_length, num_input_channels)
, required) —
Input sequence to the model torch.Tensor
of shape (bs, num_input_channels)
) —
Target values associates with the past_values
torch.BoolTensor
of shape (batch_size, sequence_length, num_input_channels)
, optional) —
Boolean mask to indicate which past_values
were observed and which were missing. Mask values selected
in [0, 1]
:
bool
, optional) —
Whether or not to return the hidden states of all layers bool
, optional) —
Whether or not to return the output attention of all layers bool
, optional) —
Whether or not to return a ModelOutput
instead of a plain tuple. Examples:
>>> from transformers import PatchTSTConfig, PatchTSTForRegression
>>> # Regression task with 6 input channels and regress 2 targets
>>> model = PatchTSTForRegression.from_pretrained("namctin/patchtst_etth1_regression")
>>> # during inference, one only provides past values, the model outputs future values
>>> past_values = torch.randn(20, 512, 6)
>>> outputs = model(past_values=past_values)
>>> regression_outputs = outputs.regression_outputs