time series forecasting model (#17965)
* initial files * initial model via cli * typos * make a start on the model config * ready with configuation * remove tokenizer ref. * init the transformer * added initial model forward to return dec_output * require gluonts * update dep. ver table and add as extra * fixed typo * add type for prediction_length * use num_time_features * use config * more config * typos * opps another typo * freq can be none * default via transformation is 1 * initial transformations * fix imports * added transform_start_field * add helper to create pytorch dataloader * added inital val and test data loader * added initial distr head and loss * training working * remove TimeSeriesTransformerTokenizer Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * Update src/transformers/__init__.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * Update src/transformers/models/time_series_transformer/__init__.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * fixed copyright * removed docs * remove time series tokenizer * fixed docs * fix text * fix second * fix default * fix order * use config directly * undo change * fix comment * fix year * fix import * add additional arguments for training vs. test * initial greedy inference loop * fix inference * comment out token inputs to enc dec * Use HF encoder/decoder * fix inference * Use Seq2SeqTSModelOutput output * return Seq2SeqTSPredictionOutput * added default arguments * fix return_dict true * scale is a tensor * output static_features for inference * clean up some unused bits * fixed typo * set return_dict if none * call model once for both train/predict * use cache if future_target is none * initial generate func * generate arguments * future_time_feat is required * return SampleTSPredictionOutput * removed unneeded classes * fix when params is none * fix return dict * fix num_attention_heads * fix arguments * remove unused shift_tokens_right * add different dropout configs * implement FeatureEmbedder, Scaler and weighted_average * remove gluonts dependency * fix class names * avoid _variable names * remove gluonts dependency * fix imports * remove gluonts from configuration * fix docs * fixed typo * move utils to examples * add example requirements * config has no freq * initial run_ts_no_trainer * remove from ignore * fix output_attentions and removed unsued getters/setters * removed unsed tests * add dec seq len * add test_attention_outputs * set has_text_modality=False * add config attribute_map * make style * make fix-copies * add encoder_outputs to TimeSeriesTransformerForPrediction forward * Improve docs, add model to README * added test_forward_signature * More improvements * Add more copied from * Fix README * Fix remaining quality issues * updated encoder and decoder * fix generate * output_hidden_states and use_cache are optional * past key_values returned too * initialize weights of distribution_output module * fixed more tests * update test_forward_signature * fix return_dict outputs * Update src/transformers/models/time_series_transformer/configuration_time_series_transformer.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Update src/transformers/models/time_series_transformer/configuration_time_series_transformer.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Update src/transformers/models/time_series_transformer/configuration_time_series_transformer.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Update src/transformers/models/time_series_transformer/configuration_time_series_transformer.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Update src/transformers/models/time_series_transformer/modeling_time_series_transformer.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Update src/transformers/models/time_series_transformer/modeling_time_series_transformer.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Update src/transformers/models/time_series_transformer/modeling_time_series_transformer.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * removed commented out tests * added neg. bin and normal output * Update src/transformers/models/time_series_transformer/configuration_time_series_transformer.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * move to one line * Add docstrings * Update src/transformers/models/time_series_transformer/configuration_time_series_transformer.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * add try except for assert and raise * try and raise exception * fix the documentation formatting * fix assert call * fix docstring formatting * removed input_ids from DOCSTRING * Update input docstring * Improve variable names * Update order of inputs * Improve configuration * Improve variable names * Improve docs * Remove key_length from tests * Add extra docs * initial unittests * added test_inference_no_head test * added test_inference_head * add test_seq_to_seq_generation * make style * one line * assert mean prediction * removed comments * Update src/transformers/models/time_series_transformer/modeling_time_series_transformer.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * Update src/transformers/models/time_series_transformer/modeling_time_series_transformer.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * fix order of args * make past_observed_mask optional as well * added Amazon license header * updated utils with new fieldnames * make style * cleanup * undo position of past_observed_mask * fix import * typo * more typo * rename example files * remove example for now * Update docs/source/en/_toctree.yml Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Update src/transformers/models/time_series_transformer/configuration_time_series_transformer.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Update src/transformers/models/time_series_transformer/modeling_time_series_transformer.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Update src/transformers/models/time_series_transformer/modeling_time_series_transformer.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Update modeling_time_series_transformer.py fix style * fixed typo * fix typo and grammer * fix style Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> Co-authored-by: NielsRogge <niels.rogge1@gmail.com> Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
This commit is contained in:
@@ -375,6 +375,7 @@ Current number of checkpoints: ** (from Google AI) released in the repository [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
|
||||
1. **[TAPAS](https://huggingface.co/docs/transformers/model_doc/tapas)** (from Google AI) released with the paper [TAPAS: Weakly Supervised Table Parsing via Pre-training](https://arxiv.org/abs/2004.02349) by Jonathan Herzig, Paweł Krzysztof Nowak, Thomas Müller, Francesco Piccinno and Julian Martin Eisenschlos.
|
||||
1. **[TAPEX](https://huggingface.co/docs/transformers/model_doc/tapex)** (from Microsoft Research) released with the paper [TAPEX: Table Pre-training via Learning a Neural SQL Executor](https://arxiv.org/abs/2107.07653) by Qian Liu, Bei Chen, Jiaqi Guo, Morteza Ziyadi, Zeqi Lin, Weizhu Chen, Jian-Guang Lou.
|
||||
1. **[Time Series Transformer](https://huggingface.co/docs/transformers/main/model_doc/time_series_transformer)** (from HuggingFace).
|
||||
1. **[Trajectory Transformer](https://huggingface.co/docs/transformers/model_doc/trajectory_transformers)** (from the University of California at Berkeley) released with the paper [Offline Reinforcement Learning as One Big Sequence Modeling Problem](https://arxiv.org/abs/2106.02039) by Michael Janner, Qiyang Li, Sergey Levine
|
||||
1. **[Transformer-XL](https://huggingface.co/docs/transformers/model_doc/transfo-xl)** (from Google/CMU) released with the paper [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://arxiv.org/abs/1901.02860) by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.
|
||||
1. **[TrOCR](https://huggingface.co/docs/transformers/model_doc/trocr)** (from Microsoft), released together with the paper [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://arxiv.org/abs/2109.10282) by Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei.
|
||||
|
||||
@@ -325,6 +325,7 @@ Flax, PyTorch, TensorFlow 설치 페이지에서 이들을 conda로 설치하는
|
||||
1. **[T5v1.1](https://huggingface.co/docs/transformers/model_doc/t5v1.1)** (from Google AI) released in the repository [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
|
||||
1. **[TAPAS](https://huggingface.co/docs/transformers/model_doc/tapas)** (from Google AI) released with the paper [TAPAS: Weakly Supervised Table Parsing via Pre-training](https://arxiv.org/abs/2004.02349) by Jonathan Herzig, Paweł Krzysztof Nowak, Thomas Müller, Francesco Piccinno and Julian Martin Eisenschlos.
|
||||
1. **[TAPEX](https://huggingface.co/docs/transformers/model_doc/tapex)** (from Microsoft Research) released with the paper [TAPEX: Table Pre-training via Learning a Neural SQL Executor](https://arxiv.org/abs/2107.07653) by Qian Liu, Bei Chen, Jiaqi Guo, Morteza Ziyadi, Zeqi Lin, Weizhu Chen, Jian-Guang Lou.
|
||||
1. **[Time Series Transformer](https://huggingface.co/docs/transformers/main/model_doc/time_series_transformer)** (from HuggingFace).
|
||||
1. **[Trajectory Transformer](https://huggingface.co/docs/transformers/model_doc/trajectory_transformers)** (from the University of California at Berkeley) released with the paper [Offline Reinforcement Learning as One Big Sequence Modeling Problem](https://arxiv.org/abs/2106.02039) by Michael Janner, Qiyang Li, Sergey Levine
|
||||
1. **[Transformer-XL](https://huggingface.co/docs/transformers/model_doc/transfo-xl)** (from Google/CMU) released with the paper [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://arxiv.org/abs/1901.02860) by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.
|
||||
1. **[TrOCR](https://huggingface.co/docs/transformers/model_doc/trocr)** (from Microsoft), released together with the paper [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://arxiv.org/abs/2109.10282) by Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei.
|
||||
|
||||
@@ -349,6 +349,7 @@ conda install -c huggingface transformers
|
||||
1. **[T5v1.1](https://huggingface.co/docs/transformers/model_doc/t5v1.1)** (来自 Google AI) 伴随论文 [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511) 由 Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu 发布。
|
||||
1. **[TAPAS](https://huggingface.co/docs/transformers/model_doc/tapas)** (来自 Google AI) 伴随论文 [TAPAS: Weakly Supervised Table Parsing via Pre-training](https://arxiv.org/abs/2004.02349) 由 Jonathan Herzig, Paweł Krzysztof Nowak, Thomas Müller, Francesco Piccinno and Julian Martin Eisenschlos 发布。
|
||||
1. **[TAPEX](https://huggingface.co/docs/transformers/model_doc/tapex)** (来自 Microsoft Research) 伴随论文 [TAPEX: Table Pre-training via Learning a Neural SQL Executor](https://arxiv.org/abs/2107.07653) 由 Qian Liu, Bei Chen, Jiaqi Guo, Morteza Ziyadi, Zeqi Lin, Weizhu Chen, Jian-Guang Lou 发布。
|
||||
1. **[Time Series Transformer](https://huggingface.co/docs/transformers/main/model_doc/time_series_transformer)** (from HuggingFace).
|
||||
1. **[Trajectory Transformer](https://huggingface.co/docs/transformers/model_doc/trajectory_transformers)** (from the University of California at Berkeley) released with the paper [Offline Reinforcement Learning as One Big Sequence Modeling Problem](https://arxiv.org/abs/2106.02039) by Michael Janner, Qiyang Li, Sergey Levine
|
||||
1. **[Transformer-XL](https://huggingface.co/docs/transformers/model_doc/transfo-xl)** (来自 Google/CMU) 伴随论文 [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://arxiv.org/abs/1901.02860) 由 Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov 发布。
|
||||
1. **[TrOCR](https://huggingface.co/docs/transformers/model_doc/trocr)** (来自 Microsoft) 伴随论文 [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://arxiv.org/abs/2109.10282) 由 Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei 发布。
|
||||
|
||||
@@ -361,6 +361,7 @@ conda install -c huggingface transformers
|
||||
1. **[T5v1.1](https://huggingface.co/docs/transformers/model_doc/t5v1.1)** (from Google AI) released with the paper [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
|
||||
1. **[TAPAS](https://huggingface.co/docs/transformers/model_doc/tapas)** (from Google AI) released with the paper [TAPAS: Weakly Supervised Table Parsing via Pre-training](https://arxiv.org/abs/2004.02349) by Jonathan Herzig, Paweł Krzysztof Nowak, Thomas Müller, Francesco Piccinno and Julian Martin Eisenschlos.
|
||||
1. **[TAPEX](https://huggingface.co/docs/transformers/model_doc/tapex)** (from Microsoft Research) released with the paper [TAPEX: Table Pre-training via Learning a Neural SQL Executor](https://arxiv.org/abs/2107.07653) by Qian Liu, Bei Chen, Jiaqi Guo, Morteza Ziyadi, Zeqi Lin, Weizhu Chen, Jian-Guang Lou.
|
||||
1. **[Time Series Transformer](https://huggingface.co/docs/transformers/main/model_doc/time_series_transformer)** (from HuggingFace).
|
||||
1. **[Trajectory Transformer](https://huggingface.co/docs/transformers/model_doc/trajectory_transformers)** (from the University of California at Berkeley) released with the paper [Offline Reinforcement Learning as One Big Sequence Modeling Problem](https://arxiv.org/abs/2106.02039) by Michael Janner, Qiyang Li, Sergey Levine
|
||||
1. **[Transformer-XL](https://huggingface.co/docs/transformers/model_doc/transfo-xl)** (from Google/CMU) released with the paper [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://arxiv.org/abs/1901.02860) by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.
|
||||
1. **[TrOCR](https://huggingface.co/docs/transformers/model_doc/trocr)** (from Microsoft) released with the paper [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://arxiv.org/abs/2109.10282) by Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei.
|
||||
|
||||
@@ -498,6 +498,11 @@
|
||||
- local: model_doc/trajectory_transformer
|
||||
title: Trajectory Transformer
|
||||
title: Reinforcement learning models
|
||||
- isExpanded: false
|
||||
sections:
|
||||
- local: model_doc/time_series_transformer
|
||||
title: Time Series Transformer
|
||||
title: Time series models
|
||||
title: Models
|
||||
- sections:
|
||||
- local: internal/modeling_utils
|
||||
|
||||
@@ -165,6 +165,7 @@ The documentation is organized into five sections:
|
||||
1. **[T5v1.1](model_doc/t5v1.1)** (from Google AI) released in the repository [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511) by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
|
||||
1. **[TAPAS](model_doc/tapas)** (from Google AI) released with the paper [TAPAS: Weakly Supervised Table Parsing via Pre-training](https://arxiv.org/abs/2004.02349) by Jonathan Herzig, Paweł Krzysztof Nowak, Thomas Müller, Francesco Piccinno and Julian Martin Eisenschlos.
|
||||
1. **[TAPEX](model_doc/tapex)** (from Microsoft Research) released with the paper [TAPEX: Table Pre-training via Learning a Neural SQL Executor](https://arxiv.org/abs/2107.07653) by Qian Liu, Bei Chen, Jiaqi Guo, Morteza Ziyadi, Zeqi Lin, Weizhu Chen, Jian-Guang Lou.
|
||||
1. **[Time Series Transformer](model_doc/time_series_transformer)** (from HuggingFace).
|
||||
1. **[Trajectory Transformer](model_doc/trajectory_transformers)** (from the University of California at Berkeley) released with the paper [Offline Reinforcement Learning as One Big Sequence Modeling Problem](https://arxiv.org/abs/2106.02039) by Michael Janner, Qiyang Li, Sergey Levine
|
||||
1. **[Transformer-XL](model_doc/transfo-xl)** (from Google/CMU) released with the paper [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://arxiv.org/abs/1901.02860) by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.
|
||||
1. **[TrOCR](model_doc/trocr)** (from Microsoft), released together with the paper [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://arxiv.org/abs/2109.10282) by Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei.
|
||||
@@ -310,6 +311,7 @@ Flax), PyTorch, and/or TensorFlow.
|
||||
| Swin Transformer V2 | ❌ | ❌ | ✅ | ❌ | ❌ |
|
||||
| T5 | ✅ | ✅ | ✅ | ✅ | ✅ |
|
||||
| TAPAS | ✅ | ❌ | ✅ | ✅ | ❌ |
|
||||
| Time Series Transformer | ❌ | ❌ | ✅ | ❌ | ❌ |
|
||||
| Trajectory Transformer | ❌ | ❌ | ✅ | ❌ | ❌ |
|
||||
| Transformer-XL | ✅ | ❌ | ✅ | ✅ | ❌ |
|
||||
| TrOCR | ❌ | ❌ | ✅ | ❌ | ❌ |
|
||||
|
||||
73
docs/source/en/model_doc/time_series_transformer.mdx
Normal file
73
docs/source/en/model_doc/time_series_transformer.mdx
Normal file
@@ -0,0 +1,73 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Time Series Transformer
|
||||
|
||||
<Tip>
|
||||
|
||||
This is a recently introduced model so the API hasn't been tested extensively. There may be some bugs or slight
|
||||
breaking changes to fix it in the future. If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title).
|
||||
|
||||
</Tip>
|
||||
|
||||
## Overview
|
||||
|
||||
The Time Series Transformer model is a vanilla encoder-decoder Transformer for time series forecasting.
|
||||
|
||||
Tips:
|
||||
|
||||
- Similar to other models in the library, [`TimeSeriesTransformerModel`] is the raw Transformer without any head on top, and [`TimeSeriesTransformerForPrediction`]
|
||||
adds a distribution head on top of the former, which can be used for time-series forecasting. Note that this is a so-called probabilistic forecasting model, not a
|
||||
point forecasting model. This means that the model learns a distribution, from which one can sample. The model doesn't directly output values.
|
||||
- [`TimeSeriesTransformerForPrediction`] consists of 2 blocks: an encoder, which takes a `context_length` of time series values as input (called `past_values`),
|
||||
and a decoder, which predicts a `prediction_length` of time series values into the future (called `future_values`). During training, one needs to provide
|
||||
pairs of (`past_values` and `future_values`) to the model.
|
||||
- In addition to the raw (`past_values` and `future_values`), one typically provides additional features to the model. These can be the following:
|
||||
- `past_time_features`: temporal features which the model will add to `past_values`. These serve as "positional encodings" for the Transformer encoder.
|
||||
Examples are "day of the month", "month of the year", etc. as scalar values (and then stacked together as a vector).
|
||||
e.g. if a given time-series value was obtained on the 11th of August, then one could have [11, 8] as time feature vector (11 being "day of the month", 8 being "month of the year").
|
||||
- `future_time_features`: temporal features which the model will add to `future_values`. These serve as "positional encodings" for the Transformer decoder.
|
||||
Examples are "day of the month", "month of the year", etc. as scalar values (and then stacked together as a vector).
|
||||
e.g. if a given time-series value was obtained on the 11th of August, then one could have [11, 8] as time feature vector (11 being "day of the month", 8 being "month of the year").
|
||||
- `static_categorical_features`: categorical features which are static over time (i.e., have the same value for all `past_values` and `future_values`).
|
||||
An example here is the store ID or region ID that identifies a given time-series.
|
||||
Note that these features need to be known for ALL data points (also those in the future).
|
||||
- `static_real_features`: real-valued features which are static over time (i.e., have the same value for all `past_values` and `future_values`).
|
||||
An example here is the image representation of the product for which you have the time-series values (like the [ResNet](resnet) embedding of a "shoe" picture,
|
||||
if your time-series is about the sales of shoes).
|
||||
Note that these features need to be known for ALL data points (also those in the future).
|
||||
- The model is trained using "teacher-forcing", similar to how a Transformer is trained for machine translation. This means that, during training, one shifts the
|
||||
`future_values` one position to the right as input to the decoder, prepended by the last value of `past_values`. At each time step, the model needs to predict the
|
||||
next target. So the set-up of training is similar to a GPT model for language, except that there's no notion of `decoder_start_token_id` (we just use the last value
|
||||
of the context as initial input for the decoder).
|
||||
- At inference time, we give the final value of the `past_values` as input to the decoder. Next, we can sample from the model to make a prediction at the next time step,
|
||||
which is then fed to the decoder in order to make the next prediction (also called autoregressive generation).
|
||||
|
||||
|
||||
This model was contributed by [kashif](<https://huggingface.co/kashif).
|
||||
|
||||
|
||||
## TimeSeriesTransformerConfig
|
||||
|
||||
[[autodoc]] TimeSeriesTransformerConfig
|
||||
|
||||
|
||||
## TimeSeriesTransformerModel
|
||||
|
||||
[[autodoc]] TimeSeriesTransformerModel
|
||||
- forward
|
||||
|
||||
|
||||
## TimeSeriesTransformerForPrediction
|
||||
|
||||
[[autodoc]] TimeSeriesTransformerForPrediction
|
||||
- forward
|
||||
1
setup.py
1
setup.py
@@ -281,6 +281,7 @@ extras["vision"] = deps_list("Pillow")
|
||||
extras["timm"] = deps_list("timm")
|
||||
extras["codecarbon"] = deps_list("codecarbon")
|
||||
|
||||
|
||||
extras["sentencepiece"] = deps_list("sentencepiece", "protobuf")
|
||||
extras["testing"] = (
|
||||
deps_list(
|
||||
|
||||
@@ -336,6 +336,10 @@ _import_structure = {
|
||||
"models.t5": ["T5_PRETRAINED_CONFIG_ARCHIVE_MAP", "T5Config"],
|
||||
"models.tapas": ["TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP", "TapasConfig", "TapasTokenizer"],
|
||||
"models.tapex": ["TapexTokenizer"],
|
||||
"models.time_series_transformer": [
|
||||
"TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP",
|
||||
"TimeSeriesTransformerConfig",
|
||||
],
|
||||
"models.trajectory_transformer": [
|
||||
"TRAJECTORY_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP",
|
||||
"TrajectoryTransformerConfig",
|
||||
@@ -815,6 +819,15 @@ else:
|
||||
_import_structure["modeling_utils"] = ["PreTrainedModel"]
|
||||
|
||||
# PyTorch models structure
|
||||
|
||||
_import_structure["models.time_series_transformer"].extend(
|
||||
[
|
||||
"TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
|
||||
"TimeSeriesTransformerForPrediction",
|
||||
"TimeSeriesTransformerModel",
|
||||
"TimeSeriesTransformerPreTrainedModel",
|
||||
]
|
||||
)
|
||||
_import_structure["models.albert"].extend(
|
||||
[
|
||||
"ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
|
||||
@@ -3286,6 +3299,10 @@ if TYPE_CHECKING:
|
||||
from .models.t5 import T5_PRETRAINED_CONFIG_ARCHIVE_MAP, T5Config
|
||||
from .models.tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig, TapasTokenizer
|
||||
from .models.tapex import TapexTokenizer
|
||||
from .models.time_series_transformer import (
|
||||
TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||
TimeSeriesTransformerConfig,
|
||||
)
|
||||
from .models.trajectory_transformer import (
|
||||
TRAJECTORY_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||
TrajectoryTransformerConfig,
|
||||
@@ -4577,6 +4594,12 @@ if TYPE_CHECKING:
|
||||
T5PreTrainedModel,
|
||||
load_tf_weights_in_t5,
|
||||
)
|
||||
from .models.time_series_transformer import (
|
||||
TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
|
||||
TimeSeriesTransformerForPrediction,
|
||||
TimeSeriesTransformerModel,
|
||||
TimeSeriesTransformerPreTrainedModel,
|
||||
)
|
||||
from .models.trajectory_transformer import (
|
||||
TRAJECTORY_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
|
||||
TrajectoryTransformerModel,
|
||||
|
||||
@@ -139,6 +139,7 @@ from . import (
|
||||
t5,
|
||||
tapas,
|
||||
tapex,
|
||||
time_series_transformer,
|
||||
trajectory_transformer,
|
||||
transfo_xl,
|
||||
trocr,
|
||||
|
||||
@@ -134,6 +134,7 @@ CONFIG_MAPPING_NAMES = OrderedDict(
|
||||
("swinv2", "Swinv2Config"),
|
||||
("t5", "T5Config"),
|
||||
("tapas", "TapasConfig"),
|
||||
("time_series_transformer", "TimeSeriesTransformerConfig"),
|
||||
("trajectory_transformer", "TrajectoryTransformerConfig"),
|
||||
("transfo-xl", "TransfoXLConfig"),
|
||||
("trocr", "TrOCRConfig"),
|
||||
@@ -262,6 +263,7 @@ CONFIG_ARCHIVE_MAP_MAPPING_NAMES = OrderedDict(
|
||||
("swinv2", "SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
("t5", "T5_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
("tapas", "TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
("time_series_transformer", "TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
("transfo-xl", "TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
("unispeech", "UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
("unispeech-sat", "UNISPEECH_SAT_PRETRAINED_CONFIG_ARCHIVE_MAP"),
|
||||
@@ -412,6 +414,7 @@ MODEL_NAMES_MAPPING = OrderedDict(
|
||||
("t5v1.1", "T5v1.1"),
|
||||
("tapas", "TAPAS"),
|
||||
("tapex", "TAPEX"),
|
||||
("time_series_transformer", "Time Series Transformer"),
|
||||
("trajectory_transformer", "Trajectory Transformer"),
|
||||
("transfo-xl", "Transformer-XL"),
|
||||
("trocr", "TrOCR"),
|
||||
|
||||
@@ -130,6 +130,7 @@ MODEL_MAPPING_NAMES = OrderedDict(
|
||||
("swinv2", "Swinv2Model"),
|
||||
("t5", "T5Model"),
|
||||
("tapas", "TapasModel"),
|
||||
("time_series_transformer", "TimeSeriesTransformerModel"),
|
||||
("trajectory_transformer", "TrajectoryTransformerModel"),
|
||||
("transfo-xl", "TransfoXLModel"),
|
||||
("unispeech", "UniSpeechModel"),
|
||||
|
||||
67
src/transformers/models/time_series_transformer/__init__.py
Normal file
67
src/transformers/models/time_series_transformer/__init__.py
Normal file
@@ -0,0 +1,67 @@
|
||||
# flake8: noqa
|
||||
# There's no way to ignore "F401 '...' imported but unused" warnings in this
|
||||
# module, but to preserve other warnings. So, don't check this module at all.
|
||||
|
||||
# Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
# rely on isort to merge the imports
|
||||
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available
|
||||
|
||||
|
||||
_import_structure = {
|
||||
"configuration_time_series_transformer": [
|
||||
"TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP",
|
||||
"TimeSeriesTransformerConfig",
|
||||
],
|
||||
}
|
||||
|
||||
try:
|
||||
if not is_torch_available():
|
||||
raise OptionalDependencyNotAvailable()
|
||||
except OptionalDependencyNotAvailable:
|
||||
pass
|
||||
else:
|
||||
_import_structure["modeling_time_series_transformer"] = [
|
||||
"TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
|
||||
"TimeSeriesTransformerForPrediction",
|
||||
"TimeSeriesTransformerModel",
|
||||
"TimeSeriesTransformerPreTrainedModel",
|
||||
]
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .configuration_time_series_transformer import (
|
||||
TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
|
||||
TimeSeriesTransformerConfig,
|
||||
)
|
||||
|
||||
try:
|
||||
if not is_torch_available():
|
||||
raise OptionalDependencyNotAvailable()
|
||||
except OptionalDependencyNotAvailable:
|
||||
pass
|
||||
else:
|
||||
from .modeling_time_series_transformer import (
|
||||
TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
|
||||
TimeSeriesTransformerForPrediction,
|
||||
TimeSeriesTransformerModel,
|
||||
TimeSeriesTransformerPreTrainedModel,
|
||||
)
|
||||
|
||||
else:
|
||||
import sys
|
||||
|
||||
sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
|
||||
@@ -0,0 +1,229 @@
|
||||
# coding=utf-8
|
||||
# Copyright 2022 The HuggingFace Inc. team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
""" Time Series Transformer model configuration"""
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
from ...configuration_utils import PretrainedConfig
|
||||
from ...utils import logging
|
||||
|
||||
|
||||
logger = logging.get_logger(__name__)
|
||||
|
||||
TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
||||
"huggingface/time-series-transformer-tourism-monthly": (
|
||||
"https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json"
|
||||
),
|
||||
# See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer
|
||||
}
|
||||
|
||||
|
||||
class TimeSeriesTransformerConfig(PretrainedConfig):
|
||||
r"""
|
||||
This is the configuration class to store the configuration of a [`TimeSeriesTransformerModel`]. It is used to
|
||||
instantiate a Time Series Transformer model according to the specified arguments, defining the model architecture.
|
||||
Instantiating a configuration with the defaults will yield a similar configuration to that of the Time Series
|
||||
Transformer
|
||||
[huggingface/time-series-transformer-tourism-monthly](https://huggingface.co/huggingface/time-series-transformer-tourism-monthly)
|
||||
architecture.
|
||||
|
||||
Configuration objects inherit from [`PretrainedConfig`] can be used to control the model outputs. Read the
|
||||
documentation from [`PretrainedConfig`] for more information.
|
||||
|
||||
Args:
|
||||
prediction_length (`int`):
|
||||
The prediction length for the decoder. In other words, the prediction horizon of the model.
|
||||
context_length (`int`, *optional*, defaults to `prediction_length`):
|
||||
The context length for the encoder. If `None`, the context length will be the same as the
|
||||
`prediction_length`.
|
||||
distribution_output (`string`, *optional*, defaults to `"student_t"`):
|
||||
The distribution emission head for the model. Could be either "student_t", "normal" or "negative_binomial".
|
||||
loss (`string`, *optional*, defaults to `"nll"`):
|
||||
The loss function for the model corresponding to the `distribution_output` head. For parametric
|
||||
distributions it is the negative log likelihood (nll) - which currently is the only supported one.
|
||||
input_size (`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
|
||||
multivarate targets.
|
||||
scaling (`bool`, *optional* defaults to `True`):
|
||||
Whether to scale the input targets.
|
||||
lags_sequence (`list[int]`, *optional*, defaults to `[1, 2, 3, 4, 5, 6, 7]`):
|
||||
The lags of the input time series as covariates often dictated by the frequency. Default is `[1, 2, 3, 4,
|
||||
5, 6, 7]`.
|
||||
num_time_features (`int`, *optional*, defaults to 0):
|
||||
The number of time features in the input time series.
|
||||
num_dynamic_real_features (`int`, *optional*, defaults to 0):
|
||||
The number of dynamic real valued features.
|
||||
num_static_categorical_features (`int`, *optional*, defaults to 0):
|
||||
The number of static categorical features.
|
||||
num_static_real_features (`int`, *optional*, defaults to 0):
|
||||
The number of static real valued features.
|
||||
cardinality (`list[int]`, *optional*):
|
||||
The cardinality (number of different values) for each of the static categorical features. Should be a list
|
||||
of integers, having the same length as `num_static_categorical_features`. Cannot be `None` if
|
||||
`num_static_categorical_features` is > 0.
|
||||
embedding_dimension (`list[int]`, *optional*):
|
||||
The dimension of the embedding for each of the static categorical features. Should be a list of integers,
|
||||
having the same length as `num_static_categorical_features`. Cannot be `None` if
|
||||
`num_static_categorical_features` is > 0.
|
||||
encoder_layers (`int`, *optional*, defaults to 2):
|
||||
Number of encoder layers.
|
||||
decoder_layers (`int`, *optional*, defaults to 2):
|
||||
Number of decoder layers.
|
||||
encoder_attention_heads (`int`, *optional*, defaults to 2):
|
||||
Number of attention heads for each attention layer in the Transformer encoder.
|
||||
decoder_attention_heads (`int`, *optional*, defaults to 2):
|
||||
Number of attention heads for each attention layer in the Transformer decoder.
|
||||
encoder_ffn_dim (`int`, *optional*, defaults to 32):
|
||||
Dimension of the "intermediate" (often named feed-forward) layer in encoder.
|
||||
decoder_ffn_dim (`int`, *optional*, defaults to 32):
|
||||
Dimension of the "intermediate" (often named feed-forward) layer in decoder.
|
||||
activation_function (`str` or `function`, *optional*, defaults to `"gelu"`):
|
||||
The non-linear activation function (function or string) in the encoder and decoder. If string, `"gelu"` and
|
||||
`"relu"` are supported.
|
||||
dropout (`float`, *optional*, defaults to 0.1):
|
||||
The dropout probability for all fully connected layers in the encoder, and decoder.
|
||||
encoder_layerdrop (`float`, *optional*, defaults to 0.1):
|
||||
The dropout probability for the attention and fully connected layers for each encoder layer.
|
||||
decoder_layerdrop (`float`, *optional*, defaults to 0.1):
|
||||
The dropout probability for the attention and fully connected layers for each decoder layer.
|
||||
attention_dropout (`float`, *optional*, defaults to 0.1):
|
||||
The dropout probability for the attention probabilities.
|
||||
activation_dropout (`float`, *optional*, defaults to 0.1):
|
||||
The dropout probability used between the two layers of the feed-forward networks.
|
||||
num_parallel_samples (`int`, *optional*, defaults to 100):
|
||||
The number of samples to generate in parallel for each time step of inference.
|
||||
init_std (`float`, *optional*, defaults to 0.02):
|
||||
The standard deviation of the truncated normal weight initialization distribution.
|
||||
use_cache (`bool`, *optional*, defaults to `True`):
|
||||
Whether to use the past key/values attentions (if applicable to the model) to speed up decoding.
|
||||
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import TimeSeriesTransformerConfig, TimeSeriesTransformerModel
|
||||
|
||||
>>> # Initializing a default Time Series Transformer configuration
|
||||
>>> configuration = TimeSeriesTransformerConfig()
|
||||
|
||||
>>> # Randomly initializing a model from the configuration
|
||||
>>> model = TimeSeriesTransformerModel(configuration)
|
||||
|
||||
>>> # Accessing the model configuration
|
||||
>>> configuration = model.config
|
||||
```"""
|
||||
model_type = "time_series_transformer"
|
||||
attribute_map = {
|
||||
"hidden_size": "d_model",
|
||||
"num_attention_heads": "encoder_attention_heads",
|
||||
"num_hidden_layers": "encoder_layers",
|
||||
}
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
input_size: int = 1,
|
||||
prediction_length: Optional[int] = None,
|
||||
context_length: Optional[int] = None,
|
||||
distribution_output: str = "student_t",
|
||||
loss: str = "nll",
|
||||
lags_sequence: List[int] = [1, 2, 3, 4, 5, 6, 7],
|
||||
scaling: bool = True,
|
||||
num_dynamic_real_features: int = 0,
|
||||
num_static_categorical_features: int = 0,
|
||||
num_static_real_features: int = 0,
|
||||
num_time_features: int = 0,
|
||||
cardinality: Optional[List[int]] = None,
|
||||
embedding_dimension: Optional[List[int]] = None,
|
||||
encoder_ffn_dim: int = 32,
|
||||
decoder_ffn_dim: int = 32,
|
||||
encoder_attention_heads: int = 2,
|
||||
decoder_attention_heads: int = 2,
|
||||
encoder_layers: int = 2,
|
||||
decoder_layers: int = 2,
|
||||
is_encoder_decoder: bool = True,
|
||||
activation_function: str = "gelu",
|
||||
dropout: float = 0.1,
|
||||
encoder_layerdrop: float = 0.1,
|
||||
decoder_layerdrop: float = 0.1,
|
||||
attention_dropout: float = 0.1,
|
||||
activation_dropout: float = 0.1,
|
||||
num_parallel_samples: int = 100,
|
||||
init_std: float = 0.02,
|
||||
use_cache=True,
|
||||
**kwargs
|
||||
):
|
||||
# time series specific configuration
|
||||
self.prediction_length = prediction_length
|
||||
self.context_length = context_length or prediction_length
|
||||
self.distribution_output = distribution_output
|
||||
self.loss = loss
|
||||
self.input_size = input_size
|
||||
self.num_time_features = num_time_features
|
||||
self.lags_sequence = lags_sequence
|
||||
self.scaling = scaling
|
||||
self.num_dynamic_real_features = num_dynamic_real_features
|
||||
self.num_static_real_features = num_static_real_features
|
||||
self.num_static_categorical_features = num_static_categorical_features
|
||||
if cardinality and num_static_categorical_features > 0:
|
||||
if len(cardinality) != num_static_categorical_features:
|
||||
raise ValueError(
|
||||
"The cardinality should be a list of the same length as `num_static_categorical_features`"
|
||||
)
|
||||
self.cardinality = cardinality
|
||||
else:
|
||||
self.cardinality = [1]
|
||||
if embedding_dimension and num_static_categorical_features > 0:
|
||||
if len(embedding_dimension) != num_static_categorical_features:
|
||||
raise ValueError(
|
||||
"The embedding dimension should be a list of the same length as `num_static_categorical_features`"
|
||||
)
|
||||
self.embedding_dimension = embedding_dimension
|
||||
else:
|
||||
self.embedding_dimension = [min(50, (cat + 1) // 2) for cat in self.cardinality]
|
||||
self.num_parallel_samples = num_parallel_samples
|
||||
|
||||
# Transformer architecture configuration
|
||||
self.d_model = input_size * len(lags_sequence) + self._number_of_features
|
||||
self.encoder_attention_heads = encoder_attention_heads
|
||||
self.decoder_attention_heads = decoder_attention_heads
|
||||
self.encoder_ffn_dim = encoder_ffn_dim
|
||||
self.decoder_ffn_dim = decoder_ffn_dim
|
||||
self.encoder_layers = encoder_layers
|
||||
self.decoder_layers = decoder_layers
|
||||
|
||||
self.dropout = dropout
|
||||
self.attention_dropout = attention_dropout
|
||||
self.activation_dropout = activation_dropout
|
||||
self.encoder_layerdrop = encoder_layerdrop
|
||||
self.decoder_layerdrop = decoder_layerdrop
|
||||
|
||||
self.activation_function = activation_function
|
||||
self.init_std = init_std
|
||||
|
||||
self.output_attentions = False
|
||||
self.output_hidden_states = False
|
||||
|
||||
self.use_cache = use_cache
|
||||
|
||||
super().__init__(is_encoder_decoder=is_encoder_decoder, **kwargs)
|
||||
|
||||
@property
|
||||
def _number_of_features(self) -> int:
|
||||
return (
|
||||
sum(self.embedding_dimension)
|
||||
+ self.num_dynamic_real_features
|
||||
+ self.num_time_features
|
||||
+ max(1, self.num_static_real_features) # there is at least one dummy static real feature
|
||||
+ 1 # the log(scale)
|
||||
)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -4811,6 +4811,30 @@ def load_tf_weights_in_t5(*args, **kwargs):
|
||||
requires_backends(load_tf_weights_in_t5, ["torch"])
|
||||
|
||||
|
||||
TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST = None
|
||||
|
||||
|
||||
class TimeSeriesTransformerForPrediction(metaclass=DummyObject):
|
||||
_backends = ["torch"]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
class TimeSeriesTransformerModel(metaclass=DummyObject):
|
||||
_backends = ["torch"]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
class TimeSeriesTransformerPreTrainedModel(metaclass=DummyObject):
|
||||
_backends = ["torch"]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
requires_backends(self, ["torch"])
|
||||
|
||||
|
||||
TRAJECTORY_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST = None
|
||||
|
||||
|
||||
|
||||
0
tests/models/time_series_transformer/__init__.py
Normal file
0
tests/models/time_series_transformer/__init__.py
Normal file
@@ -0,0 +1,438 @@
|
||||
# coding=utf-8
|
||||
# Copyright 2022 The HuggingFace Inc. team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
""" Testing suite for the PyTorch TimeSeriesTransformer model. """
|
||||
|
||||
import inspect
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from huggingface_hub import hf_hub_download
|
||||
from transformers import is_torch_available
|
||||
from transformers.testing_utils import require_torch, slow, torch_device
|
||||
|
||||
from ...test_configuration_common import ConfigTester
|
||||
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
|
||||
|
||||
|
||||
TOLERANCE = 1e-4
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
from transformers import (
|
||||
TimeSeriesTransformerConfig,
|
||||
TimeSeriesTransformerForPrediction,
|
||||
TimeSeriesTransformerModel,
|
||||
)
|
||||
from transformers.models.time_series_transformer.modeling_time_series_transformer import (
|
||||
TimeSeriesTransformerDecoder,
|
||||
TimeSeriesTransformerEncoder,
|
||||
)
|
||||
|
||||
|
||||
@require_torch
|
||||
class TimeSeriesTransformerModelTester:
|
||||
def __init__(
|
||||
self,
|
||||
parent,
|
||||
batch_size=13,
|
||||
prediction_length=7,
|
||||
context_length=14,
|
||||
cardinality=19,
|
||||
embedding_dimension=5,
|
||||
num_time_features=4,
|
||||
is_training=True,
|
||||
hidden_size=16,
|
||||
num_hidden_layers=2,
|
||||
num_attention_heads=4,
|
||||
intermediate_size=4,
|
||||
hidden_act="gelu",
|
||||
hidden_dropout_prob=0.1,
|
||||
attention_probs_dropout_prob=0.1,
|
||||
lags_sequence=[1, 2, 3, 4, 5],
|
||||
):
|
||||
self.parent = parent
|
||||
self.batch_size = batch_size
|
||||
self.prediction_length = prediction_length
|
||||
self.context_length = context_length
|
||||
self.cardinality = cardinality
|
||||
self.num_time_features = num_time_features
|
||||
self.lags_sequence = lags_sequence
|
||||
self.embedding_dimension = embedding_dimension
|
||||
self.is_training = is_training
|
||||
self.hidden_size = hidden_size
|
||||
self.num_hidden_layers = num_hidden_layers
|
||||
self.num_attention_heads = num_attention_heads
|
||||
self.intermediate_size = intermediate_size
|
||||
self.hidden_act = hidden_act
|
||||
self.hidden_dropout_prob = hidden_dropout_prob
|
||||
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
||||
|
||||
self.encoder_seq_length = context_length
|
||||
self.decoder_seq_length = prediction_length
|
||||
|
||||
def get_config(self):
|
||||
return TimeSeriesTransformerConfig(
|
||||
encoder_layers=self.num_hidden_layers,
|
||||
decoder_layers=self.num_hidden_layers,
|
||||
encoder_attention_heads=self.num_attention_heads,
|
||||
decoder_attention_heads=self.num_attention_heads,
|
||||
encoder_ffn_dim=self.intermediate_size,
|
||||
decoder_ffn_dim=self.intermediate_size,
|
||||
dropout=self.hidden_dropout_prob,
|
||||
attention_dropout=self.attention_probs_dropout_prob,
|
||||
prediction_length=self.prediction_length,
|
||||
context_length=self.context_length,
|
||||
lags_sequence=self.lags_sequence,
|
||||
num_time_features=self.num_time_features,
|
||||
num_static_categorical_features=1,
|
||||
cardinality=[self.cardinality],
|
||||
embedding_dimension=[self.embedding_dimension],
|
||||
)
|
||||
|
||||
def prepare_time_series_transformer_inputs_dict(self, config):
|
||||
_past_length = config.context_length + max(config.lags_sequence)
|
||||
|
||||
static_categorical_features = ids_tensor([self.batch_size, 1], config.cardinality[0])
|
||||
static_real_features = floats_tensor([self.batch_size, 1])
|
||||
|
||||
past_time_features = floats_tensor([self.batch_size, _past_length, config.num_time_features])
|
||||
past_values = floats_tensor([self.batch_size, _past_length])
|
||||
past_observed_mask = floats_tensor([self.batch_size, _past_length])
|
||||
|
||||
# decoder inputs
|
||||
future_time_features = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features])
|
||||
future_values = floats_tensor([self.batch_size, config.prediction_length])
|
||||
|
||||
inputs_dict = {
|
||||
"past_values": past_values,
|
||||
"static_categorical_features": static_categorical_features,
|
||||
"static_real_features": static_real_features,
|
||||
"past_time_features": past_time_features,
|
||||
"past_observed_mask": past_observed_mask,
|
||||
"future_time_features": future_time_features,
|
||||
"future_values": future_values,
|
||||
}
|
||||
return inputs_dict
|
||||
|
||||
def prepare_config_and_inputs(self):
|
||||
config = self.get_config()
|
||||
inputs_dict = self.prepare_time_series_transformer_inputs_dict(config)
|
||||
return config, inputs_dict
|
||||
|
||||
def prepare_config_and_inputs_for_common(self):
|
||||
config, inputs_dict = self.prepare_config_and_inputs()
|
||||
return config, inputs_dict
|
||||
|
||||
def check_encoder_decoder_model_standalone(self, config, inputs_dict):
|
||||
model = TimeSeriesTransformerModel(config=config).to(torch_device).eval()
|
||||
outputs = model(**inputs_dict)
|
||||
|
||||
encoder_last_hidden_state = outputs.encoder_last_hidden_state
|
||||
last_hidden_state = outputs.last_hidden_state
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
encoder = model.get_encoder()
|
||||
encoder.save_pretrained(tmpdirname)
|
||||
encoder = TimeSeriesTransformerEncoder.from_pretrained(tmpdirname).to(torch_device)
|
||||
|
||||
transformer_inputs, _, _ = model.create_network_inputs(**inputs_dict)
|
||||
enc_input = transformer_inputs[:, : config.context_length, ...]
|
||||
dec_input = transformer_inputs[:, config.context_length :, ...]
|
||||
|
||||
encoder_last_hidden_state_2 = encoder(inputs_embeds=enc_input)[0]
|
||||
|
||||
self.parent.assertTrue((encoder_last_hidden_state_2 - encoder_last_hidden_state).abs().max().item() < 1e-3)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
decoder = model.get_decoder()
|
||||
decoder.save_pretrained(tmpdirname)
|
||||
decoder = TimeSeriesTransformerDecoder.from_pretrained(tmpdirname).to(torch_device)
|
||||
|
||||
last_hidden_state_2 = decoder(
|
||||
inputs_embeds=dec_input,
|
||||
encoder_hidden_states=encoder_last_hidden_state,
|
||||
)[0]
|
||||
|
||||
self.parent.assertTrue((last_hidden_state_2 - last_hidden_state).abs().max().item() < 1e-3)
|
||||
|
||||
|
||||
@require_torch
|
||||
class TimeSeriesTransformerModelTest(ModelTesterMixin, unittest.TestCase):
|
||||
all_model_classes = (
|
||||
(TimeSeriesTransformerModel, TimeSeriesTransformerForPrediction) if is_torch_available() else ()
|
||||
)
|
||||
all_generative_model_classes = (TimeSeriesTransformerForPrediction,) if is_torch_available() else ()
|
||||
is_encoder_decoder = True
|
||||
test_pruning = False
|
||||
test_head_masking = False
|
||||
test_missing_keys = False
|
||||
test_torchscript = False
|
||||
test_inputs_embeds = False
|
||||
test_model_common_attributes = False
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = TimeSeriesTransformerModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=TimeSeriesTransformerConfig, has_text_modality=False)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
def test_save_load_strict(self):
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs()
|
||||
for model_class in self.all_model_classes:
|
||||
model = model_class(config)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
model.save_pretrained(tmpdirname)
|
||||
model2, info = model_class.from_pretrained(tmpdirname, output_loading_info=True)
|
||||
self.assertEqual(info["missing_keys"], [])
|
||||
|
||||
def test_encoder_decoder_model_standalone(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
self.model_tester.check_encoder_decoder_model_standalone(*config_and_inputs)
|
||||
|
||||
# Ignore since we have no tokens embeddings
|
||||
def test_resize_tokens_embeddings(self):
|
||||
pass
|
||||
|
||||
# # Input is 'static_categorical_features' not 'input_ids'
|
||||
def test_model_main_input_name(self):
|
||||
model_signature = inspect.signature(getattr(TimeSeriesTransformerModel, "forward"))
|
||||
# The main input is the name of the argument after `self`
|
||||
observed_main_input_name = list(model_signature.parameters.keys())[1]
|
||||
self.assertEqual(TimeSeriesTransformerModel.main_input_name, observed_main_input_name)
|
||||
|
||||
def test_forward_signature(self):
|
||||
config, _ = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
for model_class in self.all_model_classes:
|
||||
model = model_class(config)
|
||||
signature = inspect.signature(model.forward)
|
||||
# signature.parameters is an OrderedDict => so arg_names order is deterministic
|
||||
arg_names = [*signature.parameters.keys()]
|
||||
|
||||
expected_arg_names = [
|
||||
"past_values",
|
||||
"past_time_features",
|
||||
"past_observed_mask",
|
||||
"static_categorical_features",
|
||||
"static_real_features",
|
||||
"future_values",
|
||||
"future_time_features",
|
||||
]
|
||||
|
||||
expected_arg_names.extend(
|
||||
[
|
||||
"future_observed_mask",
|
||||
"decoder_attention_mask",
|
||||
"head_mask",
|
||||
"decoder_head_mask",
|
||||
"cross_attn_head_mask",
|
||||
"encoder_outputs",
|
||||
"past_key_values",
|
||||
"output_hidden_states",
|
||||
"output_attentions",
|
||||
"use_cache",
|
||||
"return_dict",
|
||||
]
|
||||
if "future_observed_mask" in arg_names
|
||||
else [
|
||||
"decoder_attention_mask",
|
||||
"head_mask",
|
||||
"decoder_head_mask",
|
||||
"cross_attn_head_mask",
|
||||
"encoder_outputs",
|
||||
"past_key_values",
|
||||
"output_hidden_states",
|
||||
"output_attentions",
|
||||
"use_cache",
|
||||
"return_dict",
|
||||
]
|
||||
)
|
||||
|
||||
self.assertListEqual(arg_names[: len(expected_arg_names)], expected_arg_names)
|
||||
|
||||
def test_attention_outputs(self):
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
config.return_dict = True
|
||||
|
||||
seq_len = getattr(self.model_tester, "seq_length", None)
|
||||
decoder_seq_length = getattr(self.model_tester, "decoder_seq_length", seq_len)
|
||||
encoder_seq_length = getattr(self.model_tester, "encoder_seq_length", seq_len)
|
||||
|
||||
for model_class in self.all_model_classes:
|
||||
inputs_dict["output_attentions"] = True
|
||||
inputs_dict["output_hidden_states"] = False
|
||||
config.return_dict = True
|
||||
model = model_class(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
|
||||
self.assertEqual(len(attentions), self.model_tester.num_hidden_layers)
|
||||
|
||||
# check that output_attentions also work using config
|
||||
del inputs_dict["output_attentions"]
|
||||
config.output_attentions = True
|
||||
model = model_class(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
attentions = outputs.encoder_attentions
|
||||
self.assertEqual(len(attentions), self.model_tester.num_hidden_layers)
|
||||
|
||||
self.assertListEqual(
|
||||
list(attentions[0].shape[-3:]),
|
||||
[self.model_tester.num_attention_heads, encoder_seq_length, encoder_seq_length],
|
||||
)
|
||||
out_len = len(outputs)
|
||||
|
||||
correct_outlen = 6
|
||||
|
||||
if "last_hidden_state" in outputs:
|
||||
correct_outlen += 1
|
||||
|
||||
if "past_key_values" in outputs:
|
||||
correct_outlen += 1 # past_key_values have been returned
|
||||
|
||||
if "loss" in outputs:
|
||||
correct_outlen += 1
|
||||
|
||||
if "params" in outputs:
|
||||
correct_outlen += 1
|
||||
|
||||
self.assertEqual(out_len, correct_outlen)
|
||||
|
||||
# decoder attentions
|
||||
decoder_attentions = outputs.decoder_attentions
|
||||
self.assertIsInstance(decoder_attentions, (list, tuple))
|
||||
self.assertEqual(len(decoder_attentions), self.model_tester.num_hidden_layers)
|
||||
self.assertListEqual(
|
||||
list(decoder_attentions[0].shape[-3:]),
|
||||
[self.model_tester.num_attention_heads, decoder_seq_length, decoder_seq_length],
|
||||
)
|
||||
|
||||
# cross attentions
|
||||
cross_attentions = outputs.cross_attentions
|
||||
self.assertIsInstance(cross_attentions, (list, tuple))
|
||||
self.assertEqual(len(cross_attentions), self.model_tester.num_hidden_layers)
|
||||
self.assertListEqual(
|
||||
list(cross_attentions[0].shape[-3:]),
|
||||
[
|
||||
self.model_tester.num_attention_heads,
|
||||
decoder_seq_length,
|
||||
encoder_seq_length,
|
||||
],
|
||||
)
|
||||
|
||||
# Check attention is always last and order is fine
|
||||
inputs_dict["output_attentions"] = True
|
||||
inputs_dict["output_hidden_states"] = True
|
||||
model = model_class(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
|
||||
self.assertEqual(out_len + 2, len(outputs))
|
||||
|
||||
self_attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
|
||||
|
||||
self.assertEqual(len(self_attentions), self.model_tester.num_hidden_layers)
|
||||
self.assertListEqual(
|
||||
list(self_attentions[0].shape[-3:]),
|
||||
[self.model_tester.num_attention_heads, encoder_seq_length, encoder_seq_length],
|
||||
)
|
||||
|
||||
|
||||
def prepare_batch(filename="train-batch.pt"):
|
||||
file = hf_hub_download(repo_id="kashif/tourism-monthly-batch", filename=filename, repo_type="dataset")
|
||||
batch = torch.load(file, map_location=torch_device)
|
||||
return batch
|
||||
|
||||
|
||||
@require_torch
|
||||
@slow
|
||||
class TimeSeriesTransformerModelIntegrationTests(unittest.TestCase):
|
||||
def test_inference_no_head(self):
|
||||
model = TimeSeriesTransformerModel.from_pretrained("huggingface/time-series-transformer-tourism-monthly").to(
|
||||
torch_device
|
||||
)
|
||||
batch = prepare_batch()
|
||||
|
||||
with torch.no_grad():
|
||||
output = model(
|
||||
past_values=batch["past_values"],
|
||||
past_time_features=batch["past_time_features"],
|
||||
past_observed_mask=batch["past_observed_mask"],
|
||||
static_categorical_features=batch["static_categorical_features"],
|
||||
static_real_features=batch["static_real_features"],
|
||||
future_values=batch["future_values"],
|
||||
future_time_features=batch["future_time_features"],
|
||||
)[0]
|
||||
|
||||
expected_shape = torch.Size((64, model.config.prediction_length, model.config.d_model))
|
||||
self.assertEqual(output.shape, expected_shape)
|
||||
|
||||
expected_slice = torch.tensor(
|
||||
[[-0.3125, -1.2884, -1.1118], [-0.5801, -1.4907, -0.7782], [0.0849, -1.6557, -0.9755]], device=torch_device
|
||||
)
|
||||
self.assertTrue(torch.allclose(output[0, :3, :3], expected_slice, atol=TOLERANCE))
|
||||
|
||||
def test_inference_head(self):
|
||||
model = TimeSeriesTransformerForPrediction.from_pretrained(
|
||||
"huggingface/time-series-transformer-tourism-monthly"
|
||||
).to(torch_device)
|
||||
batch = prepare_batch("val-batch.pt")
|
||||
with torch.no_grad():
|
||||
output = model(
|
||||
past_values=batch["past_values"],
|
||||
past_time_features=batch["past_time_features"],
|
||||
past_observed_mask=batch["past_observed_mask"],
|
||||
static_categorical_features=batch["static_categorical_features"],
|
||||
static_real_features=batch["static_real_features"],
|
||||
future_time_features=batch["future_time_features"],
|
||||
)[1]
|
||||
expected_shape = torch.Size((64, model.config.prediction_length, model.config.d_model))
|
||||
self.assertEqual(output.shape, expected_shape)
|
||||
|
||||
expected_slice = torch.tensor(
|
||||
[[0.9127, -0.2056, -0.5259], [1.0572, 1.4104, -0.1964], [0.1358, 2.0348, 0.5739]], device=torch_device
|
||||
)
|
||||
self.assertTrue(torch.allclose(output[0, :3, :3], expected_slice, atol=TOLERANCE))
|
||||
|
||||
def test_seq_to_seq_generation(self):
|
||||
model = TimeSeriesTransformerForPrediction.from_pretrained(
|
||||
"huggingface/time-series-transformer-tourism-monthly"
|
||||
).to(torch_device)
|
||||
batch = prepare_batch("val-batch.pt")
|
||||
with torch.no_grad():
|
||||
outputs = model.generate(
|
||||
static_categorical_features=batch["static_categorical_features"],
|
||||
static_real_features=batch["static_real_features"],
|
||||
past_time_features=batch["past_time_features"],
|
||||
past_values=batch["past_values"],
|
||||
future_time_features=batch["future_time_features"],
|
||||
past_observed_mask=batch["past_observed_mask"],
|
||||
)
|
||||
expected_shape = torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length))
|
||||
self.assertEqual(outputs.sequences.shape, expected_shape)
|
||||
|
||||
expected_slice = torch.tensor([2289.5203, 2778.3054, 4648.1313], device=torch_device)
|
||||
mean_prediction = outputs.sequences.mean(dim=1)
|
||||
self.assertTrue(torch.allclose(mean_prediction[0, -3:], expected_slice, rtol=1e-1))
|
||||
@@ -46,6 +46,8 @@ PRIVATE_MODELS = [
|
||||
# Being in this list is an exception and should **not** be the rule.
|
||||
IGNORE_NON_TESTED = PRIVATE_MODELS.copy() + [
|
||||
# models to ignore for not tested
|
||||
"TimeSeriesTransformerEncoder", # Building part of bigger (tested) model.
|
||||
"TimeSeriesTransformerDecoder", # Building part of bigger (tested) model.
|
||||
"DeformableDetrEncoder", # Building part of bigger (tested) model.
|
||||
"DeformableDetrDecoder", # Building part of bigger (tested) model.
|
||||
"OPTDecoder", # Building part of bigger (tested) model.
|
||||
@@ -132,6 +134,7 @@ TEST_FILES_WITH_NO_COMMON_TESTS = [
|
||||
# should **not** be the rule.
|
||||
IGNORE_NON_AUTO_CONFIGURED = PRIVATE_MODELS.copy() + [
|
||||
# models to ignore for model xxx mapping
|
||||
"TimeSeriesTransformerForPrediction",
|
||||
"PegasusXEncoder",
|
||||
"PegasusXDecoder",
|
||||
"PegasusXDecoderWrapper",
|
||||
|
||||
Reference in New Issue
Block a user