Skip to content

Commit

Permalink
Merge embeddings into master (#306)
Browse files Browse the repository at this point in the history
* Implement TS2VecModel (#253)

* add ts2vec model

* delete unnecessary utils

* add multiscale mode

* revert to common encode in model class

* lints

* reformat save method, add _is_fitted attr

* fix embeddings shapes

* fix

* one more fix

* pass numpy array to fit

* add tests checking nans in embeddings

* update changelog

---------

Co-authored-by: Egor Baturin <egoriyaa@github.com>

* Implement EmbeddingSegmentTransform and EmbeddingWindowTransform (#265)

* add transforms

* update changelog

* fix ts2vec tests

* fix

* update rst, encoding_params

* fix

* fix

* fix

* fix docstring

* add training_params

* add freeze method

* fix inference tests

* lints

* fix lisence

* fix lisence, fix docs

* fix quotes

---------

Co-authored-by: Egor Baturin <egoriyaa@github.com>

* Implement TSTCC (#294)

* add tstcc

* add einops package

* remove pd.testing in inference tests

* fix

* add verbose param, refactor logging, fix warning

* fix logging loss

* add changelog

* catch torch warning

* fix

* catch nn.Conv1d warning

---------

Co-authored-by: Egor Baturin <egoriyaa@github.com>

* lints

* fix

* Add tutorial how to work with embedding models (#304)

* fix tstcc

* move lr param from __init__ to fit

* add tutorial

* fix notebook

* update changelog

* fix changelog

* lints

* fix notebook

* update readme

* fix readme

* fix readme

* write comment in libs/ts2vec/ts2vec.py

* fix notebook

* remove multiscale option in ts2vec

* lints

* fix notebook

---------

Co-authored-by: Egor Baturin <egoriyaa@github.com>

* fix atol in inference tests

* downgrade poetry

---------

Co-authored-by: Egor Baturin <egoriyaa@github.com>
  • Loading branch information
egoriyaa and Egor Baturin committed May 3, 2024
1 parent 755cdab commit d604429
Show file tree
Hide file tree
Showing 38 changed files with 5,104 additions and 95 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased
### Added
-
-
-
-
- Add `TS2VecEmbeddingModel` model ([#253](https://github.com/etna-team/etna/pull/253))
- Add `EmbeddingSegmentTransform` ([#265](https://github.com/etna-team/etna/pull/265))
- Add `EmbeddingWindowTransform` ([#265](https://github.com/etna-team/etna/pull/265))
- Add `TSTCCEmbeddingModel` ([#294](https://github.com/etna-team/etna/pull/294))
- Add `210-embedding_models` example notebook ([#304](https://github.com/etna-team/etna/pull/304))
-
-
-
Expand Down
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,27 +175,28 @@ To set up a configuration for your project you should create a `.etna` file at t

We have also prepared a set of tutorials for an easy introduction:

| Notebook | Interactive launch |
|:----------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------:|
| [Get started](https://github.com/etna-team/etna/tree/master/examples/101-get_started.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/101-get_started.ipynb) |
| [Backtest](https://github.com/etna-team/etna/tree/master/examples/102-backtest.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/102-backtest.ipynb) |
| [EDA](https://github.com/etna-team/etna/tree/master/examples/103-EDA.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/103-EDA.ipynb) |
| [Regressors and exogenous data](https://github.com/etna-team/etna/tree/master/examples/201-exogenous_data.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/201-exogenous_data.ipynb) |
| [Deep learning models](https://github.com/etna-team/etna/tree/master/examples/202-NN_examples.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/202-NN_examples.ipynb) |
| [Ensembles](https://github.com/etna-team/etna/tree/master/examples/303-ensembles.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/203-ensembles.ipynb) |
| [Outliers](https://github.com/etna-team/etna/tree/master/examples/204-outliers.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/204-outliers.ipynb) |
| [AutoML](https://github.com/etna-team/etna/tree/master/examples/205-automl.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/205-automl.ipynb) |
| [Clustering](https://github.com/etna-team/etna/tree/master/examples/206-clustering.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/206-clustering.ipynb) |
| [Feature selection](https://github.com/etna-team/etna/blob/master/examples/207-feature_selection.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/207-feature_selection.ipynb) |
| [Forecasting strategies](https://github.com/etna-team/etna/tree/master/examples/208-forecasting_strategies.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/208-forecasting_strategies.ipynb) |
| [Mechanics of forecasting](https://github.com/etna-team/etna/blob/master/examples/209-mechanics_of_forecasting.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/209-mechanics_of_forecasting.ipynb) |
| [Custom model and transform](https://github.com/etna-team/etna/tree/master/examples/301-custom_transform_and_model.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/301-custom_transform_and_model.ipynb) |
| [Inference: using saved pipeline on a new data](https://github.com/etna-team/etna/tree/master/examples/302-inference.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/302-inference.ipynb) |
| [Hierarchical time series](https://github.com/etna-team/etna/blob/master/examples/303-hierarchical_pipeline.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/303-hierarchical_pipeline.ipynb) |
| [Forecast interpretation](https://github.com/etna-team/etna/tree/master/examples/304-forecasting_interpretation.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/304-forecasting_interpretation.ipynb) |
| [Classification](https://github.com/etna-team/etna/blob/master/examples/305-classification.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/305-classification.ipynb) |
| [Prediction intervals](https://github.com/etna-team/etna/tree/master/examples/306-prediction_intervals.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/306-prediction_intervals.ipynb) |
| [Working with misaligned data](https://github.com/etna-team/etna/tree/master/examples/307-working_with_misaligned_data.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/307-working_with_misaligned_data.ipynb) |
| Notebook | Interactive launch |
|:------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------:|
| [Get started](https://github.com/etna-team/etna/tree/master/examples/101-get_started.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/101-get_started.ipynb) |
| [Backtest](https://github.com/etna-team/etna/tree/master/examples/102-backtest.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/102-backtest.ipynb) |
| [EDA](https://github.com/etna-team/etna/tree/master/examples/103-EDA.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/103-EDA.ipynb) |
| [Regressors and exogenous data](https://github.com/etna-team/etna/tree/master/examples/201-exogenous_data.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/201-exogenous_data.ipynb) |
| [Deep learning models](https://github.com/etna-team/etna/tree/master/examples/202-NN_examples.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/202-NN_examples.ipynb) |
| [Ensembles](https://github.com/etna-team/etna/tree/master/examples/303-ensembles.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/203-ensembles.ipynb) |
| [Outliers](https://github.com/etna-team/etna/tree/master/examples/204-outliers.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/204-outliers.ipynb) |
| [AutoML](https://github.com/etna-team/etna/tree/master/examples/205-automl.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/205-automl.ipynb) |
| [Clustering](https://github.com/etna-team/etna/tree/master/examples/206-clustering.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/206-clustering.ipynb) |
| [Feature selection](https://github.com/etna-team/etna/blob/master/examples/207-feature_selection.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/207-feature_selection.ipynb) |
| [Forecasting strategies](https://github.com/etna-team/etna/tree/master/examples/208-forecasting_strategies.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/208-forecasting_strategies.ipynb) |
| [Mechanics of forecasting](https://github.com/etna-team/etna/blob/master/examples/209-mechanics_of_forecasting.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/209-mechanics_of_forecasting.ipynb) |
| [Embedding models](https://github.com/etna-team/etna/blob/master/examples/210-embedding_models.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/210-embedding_models.ipynb) |
| [Custom model and transform](https://github.com/etna-team/etna/tree/master/examples/301-custom_transform_and_model.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/301-custom_transform_and_model.ipynb) |
| [Inference: using saved pipeline on a new data](https://github.com/etna-team/etna/tree/master/examples/302-inference.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/302-inference.ipynb) |
| [Hierarchical time series](https://github.com/etna-team/etna/blob/master/examples/303-hierarchical_pipeline.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/303-hierarchical_pipeline.ipynb) |
| [Forecast interpretation](https://github.com/etna-team/etna/tree/master/examples/304-forecasting_interpretation.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/304-forecasting_interpretation.ipynb) |
| [Classification](https://github.com/etna-team/etna/blob/master/examples/305-classification.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/305-classification.ipynb) |
| [Prediction intervals](https://github.com/etna-team/etna/tree/master/examples/306-prediction_intervals.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/306-prediction_intervals.ipynb) |
| [Working with misaligned data](https://github.com/etna-team/etna/tree/master/examples/307-working_with_misaligned_data.ipynb) | [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/etna-team/etna/master?filepath=examples/307-working_with_misaligned_data.ipynb) |

## Documentation

Expand Down
12 changes: 12 additions & 0 deletions docs/source/api_reference/transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ Categorical encoding transforms:
LabelEncoderTransform
OneHotEncoderTransform

.. _embeddings:
Embedding transforms and their utilities:

.. autosummary::
:toctree: api/
:template: class.rst

EmbeddingSegmentTransform
EmbeddingWindowTransform
embeddings.models.TS2VecEmbeddingModel
embeddings.models.TSTCCEmbeddingModel

Feature selection transforms:

.. autosummary::
Expand Down
9 changes: 9 additions & 0 deletions docs/source/tutorials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ Intermediate
^^^
How pipelines are making forecasts under the hood

.. grid-item-card:: Embedding models
:text-align: center
:link: tutorials/210-embedding_models
:link-type: doc
:class-header: card-tutorial-intermediate

^^^
How to use embedding models

Advanced
--------

Expand Down
1 change: 1 addition & 0 deletions etna/libs/ts2vec/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from etna.libs.ts2vec.ts2vec import TS2Vec
83 changes: 83 additions & 0 deletions etna/libs/ts2vec/dilated_conv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
MIT License
Copyright (c) 2022 Zhihan Yue
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
# Note: Copied from ts2vec repository (https://github.com/yuezhihan/ts2vec/tree/main)

import torch
from torch import nn
import torch.nn.functional as F
import numpy as np


class SamePadConv(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, dilation=1, groups=1):
super().__init__()
self.receptive_field = (kernel_size - 1) * dilation + 1
padding = self.receptive_field // 2
self.conv = nn.Conv1d(
in_channels, out_channels, kernel_size,
padding=padding,
dilation=dilation,
groups=groups
)
self.remove = 1 if self.receptive_field % 2 == 0 else 0

def forward(self, x):
out = self.conv(x)
if self.remove > 0:
out = out[:, :, : -self.remove]
return out


class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, dilation, final=False):
super().__init__()
self.conv1 = SamePadConv(in_channels, out_channels, kernel_size, dilation=dilation)
self.conv2 = SamePadConv(out_channels, out_channels, kernel_size, dilation=dilation)
self.projector = nn.Conv1d(in_channels, out_channels, 1) if in_channels != out_channels or final else None

def forward(self, x):
residual = x if self.projector is None else self.projector(x)
x = F.gelu(x)
x = self.conv1(x)
x = F.gelu(x)
x = self.conv2(x)
return x + residual


class DilatedConvEncoder(nn.Module):
def __init__(self, in_channels, channels, kernel_size):
super().__init__()
self.net = nn.Sequential(*[
ConvBlock(
channels[i - 1] if i > 0 else in_channels,
channels[i],
kernel_size=kernel_size,
dilation=2 ** i,
final=(i == len(channels) - 1)
)
for i in range(len(channels))
])

def forward(self, x):
return self.net(x)

0 comments on commit d604429

Please sign in to comment.