forked from tinkoff-ai/etna
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
38 changed files
with
5,104 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from etna.libs.ts2vec.ts2vec import TS2Vec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.