-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodules.py
75 lines (65 loc) · 2.18 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
70
71
72
73
74
75
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
class mygru(nn.Module):
'''
classifier decoder implemented with mlp
'''
def __init__(self, n_layer, input_dim, hidden_dim):
super().__init__()
this_layer = n_layer
self.g_ir = funcsgru(this_layer, input_dim, hidden_dim, 0)
self.g_iz = funcsgru(this_layer, input_dim, hidden_dim, 0)
self.g_in = funcsgru(this_layer, input_dim, hidden_dim, 0)
self.g_hr = funcsgru(this_layer, hidden_dim, hidden_dim, 0)
self.g_hz = funcsgru(this_layer, hidden_dim, hidden_dim, 0)
self.g_hn = funcsgru(this_layer, hidden_dim, hidden_dim, 0)
self.sigmoid = torch.nn.Sigmoid()
self.tanh = torch.nn.Tanh()
def forward(self, x, h):
r_t = self.sigmoid(
self.g_ir(x) + self.g_hr(h)
)
z_t = self.sigmoid(
self.g_iz(x) + self.g_hz(h)
)
n_t = self.tanh(
self.g_in(x) + self.g_hn(h).mul(r_t)
)
h_t = (1 - z_t) * n_t + z_t * h
return h_t
class funcsgru(nn.Module):
'''
classifier decoder implemented with mlp
'''
def __init__(self, n_layer, hidden_dim, output_dim, dpo):
super().__init__()
self.lins = nn.ModuleList([
nn.Linear(hidden_dim, hidden_dim)
for _ in range(n_layer)
])
self.dropout = nn.Dropout(p = dpo)
self.out = nn.Linear(hidden_dim, output_dim)
self.act = torch.nn.Sigmoid()
def forward(self, x):
for lin in self.lins:
x = F.relu(lin(x))
return self.out(self.dropout(x))
class funcs(nn.Module):
'''
classifier decoder implemented with mlp
'''
def __init__(self, n_layer, hidden_dim, output_dim, dpo):
super().__init__()
self.lins = nn.ModuleList([
nn.Linear(hidden_dim, hidden_dim)
for _ in range(n_layer)
])
self.dropout = nn.Dropout(p = dpo)
self.out = nn.Linear(hidden_dim, output_dim)
self.act = torch.nn.Sigmoid()
def forward(self, x):
for lin in self.lins:
x = F.relu(lin(x))
return self.out(self.dropout(x))