-
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.
- Loading branch information
1 parent
88f9df2
commit 3c8a293
Showing
8 changed files
with
94 additions
and
148 deletions.
There are no files selected for viewing
File renamed without changes.
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 was deleted.
Oops, something went wrong.
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,36 @@ | ||
from functools import partial | ||
from typing import Any, Dict, Tuple | ||
|
||
import lightning as L | ||
import torch | ||
import torch.nn as nn | ||
import torchmetrics | ||
from lightning.pytorch.utilities.types import STEP_OUTPUT | ||
from sklearn.metrics import f1_score | ||
from torch import optim | ||
from torchmetrics import MetricCollection | ||
|
||
class AttentionLayer(nn.Module): | ||
def __init__(self, in_channels, out_channels, key_channels): | ||
super(AttentionLayer, self).__init__() | ||
self.conv_Q = nn.Conv1d(in_channels, key_channels, kernel_size=1, bias=False) | ||
self.conv_K = nn.Conv1d(in_channels, key_channels, kernel_size=1, bias=False) | ||
self.conv_V = nn.Conv1d(in_channels, out_channels, kernel_size=1, bias=False) | ||
|
||
def forward(self, x): | ||
Q = self.conv_Q(x) | ||
K = self.conv_K(x) | ||
V = self.conv_V(x) | ||
A = Q.permute(0, 2, 1).matmul(K).softmax(2) | ||
x = A.matmul(V.permute(0, 2, 1)).permute(0, 2, 1) | ||
return x | ||
|
||
def __repr__(self): | ||
return ( | ||
self._get_name() | ||
+ "(in_channels={}, out_channels={}, key_channels={})".format( | ||
self.conv_Q.in_channels, | ||
self.conv_V.out_channels, | ||
self.conv_K.out_channels, | ||
) | ||
) |
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,51 @@ | ||
from functools import partial | ||
from typing import Any, Dict, Tuple | ||
|
||
import lightning as L | ||
import torch | ||
import torch.nn as nn | ||
import torchmetrics | ||
from lightning.pytorch.utilities.types import STEP_OUTPUT | ||
from sklearn.metrics import f1_score | ||
from torch import optim | ||
from torchmetrics import MetricCollection | ||
from hungarian_net.torch_modules.attention_layer import AttentionLayer | ||
|
||
class HNetGRU(nn.Module): | ||
def __init__(self, max_len=4, hidden_size=128): | ||
super().__init__() | ||
self.nb_gru_layers = 1 | ||
self.max_len = max_len | ||
self.gru = nn.GRU(max_len, hidden_size, self.nb_gru_layers, batch_first=True) | ||
self.attn = AttentionLayer(hidden_size, hidden_size, hidden_size) | ||
self.fc1 = nn.Linear(hidden_size, max_len) | ||
|
||
def forward(self, query): | ||
# query - batch x seq x feature | ||
|
||
out, _ = self.gru(query) | ||
# out - batch x seq x hidden | ||
|
||
out = out.permute((0, 2, 1)) | ||
# out - batch x hidden x seq | ||
|
||
out = self.attn.forward(out) | ||
# out - batch x hidden x seq | ||
|
||
out = out.permute((0, 2, 1)) | ||
out = torch.tanh(out) | ||
# out - batch x seq x hidden | ||
|
||
out = self.fc1(out) | ||
# out - batch x seq x feature | ||
|
||
out1 = out.view(out.shape[0], -1) | ||
# out1 - batch x (seq x feature) | ||
|
||
out2, _ = torch.max(out, dim=-1) | ||
# out2 - batch x seq x 1 | ||
|
||
out3, _ = torch.max(out, dim=-2) | ||
# out3 - batch x 1 x feature | ||
|
||
return out1.squeeze(), out2.squeeze(), out3.squeeze() |
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