-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlp.py
59 lines (54 loc) · 1.88 KB
/
mlp.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
import torch.nn as nn
import torch.nn.functional as F
class MERT_MLP(nn.Module):
def __init__(
self,
num_features,
hidden_layer_sizes,
num_outputs,
dropout_input=True,
dropout_p=0.5,
):
super().__init__()
d = num_features
self.aggregator = nn.Conv1d(in_channels=25, out_channels=1, kernel_size=1)
self.num_layers = len(hidden_layer_sizes)
for i, ld in enumerate(hidden_layer_sizes):
setattr(self, f"hidden_{i}", nn.Linear(d, ld))
d = ld
self.output = nn.Linear(d, num_outputs)
self.dropout = nn.Dropout(p=dropout_p)
def forward(self, x, y):
x = self.aggregator(x).squeeze()
x = self.dropout(x)
for i in range(self.num_layers):
x = getattr(self, f"hidden_{i}")(x)
x = F.relu(x)
x = self.dropout(x)
logits = self.output(x)
return logits, F.binary_cross_entropy_with_logits(logits, y.float(), reduction="mean")
class SimpleMLP(nn.Module):
def __init__(
self,
num_features,
hidden_layer_sizes,
num_outputs,
dropout_input=True,
dropout_p=0.5,
):
super().__init__()
d = num_features
self.num_layers = len(hidden_layer_sizes)
for i, ld in enumerate(hidden_layer_sizes):
setattr(self, f"hidden_{i}", nn.Linear(d, ld))
d = ld
self.output = nn.Linear(d, num_outputs)
self.dropout = nn.Dropout(p=dropout_p)
def forward(self, x, y):
x = self.dropout(x)
for i in range(self.num_layers):
x = getattr(self, f"hidden_{i}")(x)
x = F.relu(x)
x = self.dropout(x)
logits = self.output(x)
return logits, F.binary_cross_entropy_with_logits(logits, y.float(), reduction="mean")