-
Notifications
You must be signed in to change notification settings - Fork 908
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/apply-arima-to-new-ts
- Loading branch information
Showing
5 changed files
with
146 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
from darts.utils.torch import MonteCarloDropout | ||
|
||
|
||
class CustomFeedForwardEncoderLayer(nn.TransformerEncoderLayer): | ||
"""Overwrites the PyTorch TransformerEncoderLayer to use Darts' Position-wise Feed-Forward variants.""" | ||
|
||
def __init__(self, ffn: nn.Module, dropout: float, *args, **kwargs): | ||
""" | ||
Parameters | ||
---------- | ||
ffn | ||
One of Darts' Position-wise Feed-Forward Network variants from darts.models.components.glu_variants | ||
dropout | ||
Fraction of neurons affected by Dropout (default=0.1). | ||
args | ||
positional arguments from torch.nn.TransformerEncoderLayer. | ||
kwargs | ||
keyword arguments from torch.nn.TransformerEncoderLayer. `activation` will have no effect. | ||
""" | ||
super().__init__(*args, **kwargs) | ||
self.ffn = ffn | ||
self.dropout = MonteCarloDropout(dropout) | ||
|
||
# overwrite the feed forward block | ||
def _ff_block(self, x: torch.Tensor) -> torch.Tensor: | ||
x = self.ffn(x) | ||
return self.dropout(x) | ||
|
||
|
||
class CustomFeedForwardDecoderLayer(nn.TransformerDecoderLayer): | ||
"""Overwrites the PyTorch TransformerDecoderLayer to use Darts' custom Position Wise Feed Forward Layers.""" | ||
|
||
def __init__(self, ffn: nn.Module, dropout: float, *args, **kwargs): | ||
""" | ||
Parameters | ||
---------- | ||
ffn | ||
One of Darts' Position-wise Feed-Forward Network variants from darts.models.components.glu_variants | ||
dropout | ||
Fraction of neurons affected by Dropout (default=0.1). | ||
args | ||
positional arguments from torch.nn.TransformerEncoderLayer. | ||
kwargs | ||
keyword arguments from torch.nn.TransformerEncoderLayer. `activation` will have no effect. | ||
""" | ||
super().__init__(*args, **kwargs) | ||
self.ffn = ffn | ||
self.dropout = MonteCarloDropout(dropout) | ||
|
||
# overwrite the feed forward block | ||
def _ff_block(self, x: torch.Tensor) -> torch.Tensor: | ||
x = self.ffn(x) | ||
return self.dropout(x) |
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