generated from itsluketwist/python-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transformer model and more options to data loader.
- Loading branch information
1 parent
982576d
commit bad5440
Showing
7 changed files
with
111 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
from src.models.rnn import GRU, LSTM, RNN | ||
from src.models.tcn import TCN | ||
from src.models.te import TE | ||
|
||
|
||
__all__ = [ | ||
"GRU", | ||
"LSTM", | ||
"RNN", | ||
"TCN", | ||
"TE", | ||
] |
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,40 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class TE(nn.Module): | ||
"""Class for TE (transformer encoder) model to predict sequential NBA data.""" | ||
|
||
def __init__( | ||
self, | ||
input_size: int = 116, | ||
sequence_len: int = 8, | ||
hidden_size: int = 512, | ||
**te_kwargs, | ||
): | ||
super(TE, self).__init__() | ||
|
||
self._input_size = input_size * sequence_len | ||
self._sequence_len = sequence_len | ||
self._output_size = 1 | ||
self._num_layers = 1 | ||
|
||
self.te = nn.TransformerEncoderLayer( | ||
d_model=self._input_size, | ||
nhead=self._sequence_len, | ||
dim_feedforward=hidden_size, | ||
batch_first=True, | ||
**te_kwargs, | ||
) | ||
|
||
self.linear = nn.Linear(self._input_size, self._output_size) | ||
self.sigmoid = nn.Sigmoid() | ||
|
||
def forward(self, input: torch.Tensor) -> torch.Tensor: | ||
output = self.te(input) | ||
output = self.linear(output) | ||
output = self.sigmoid(output) | ||
return output | ||
|
||
def __repr__(self) -> str: | ||
return type(self).__name__ |
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