-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.py
69 lines (55 loc) · 1.69 KB
/
modules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from typing import Tuple
from torch import Tensor, nn
class GatedAttention(nn.Module):
def __init__(
self,
in_dim: int,
intermediate_dim: int,
out_dim: int = 1,
dropout: bool = False
) -> None:
super().__init__()
self.in_dim = in_dim
self.intermediate_dim = intermediate_dim
self.out_dim = out_dim
self.attn_a = [
nn.Linear(in_dim, intermediate_dim),
nn.Tanh(),
]
self.attn_b = [
nn.Linear(in_dim, intermediate_dim),
nn.Sigmoid()
]
if dropout:
self.attn_a.append(nn.Dropout(0.25))
self.attn_b.append(nn.Dropout(0.25))
self.attn_a = nn.Sequential(*self.attn_a)
self.attn_b = nn.Sequential(*self.attn_b)
self.final_attn = nn.Linear(intermediate_dim, out_dim)
def forward(self,
x: Tensor,
) -> Tuple[Tensor, Tensor]:
attn_a = self.attn_a(x)
attn_b = self.attn_b(x)
A = attn_a.mul(attn_b)
A = self.final_attn(A)
return A, x
class FinalClassifier(nn.Module):
def __init__(
self,
in_channels: int,
num_classes: int,
dropout_rate: float = 0.
) -> None:
super().__init__()
layers = [
nn.GroupNorm(num_groups=int(in_channels/16),
num_channels=in_channels),
nn.Linear(in_channels, num_classes),
]
if dropout_rate != 0.0:
layers.append(nn.Dropout(p=dropout_rate))
layers.append(nn.Mish())
self.final_fc = nn.Sequential(*layers)
def forward(self, x):
return self.final_fc(x)