-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
44 lines (38 loc) · 1.38 KB
/
model.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
'''
Date: 2021-07-08 18:39:56
LastEditors: yuhhong
LastEditTime: 2021-07-09 13:33:52
'''
import torch
class MLP(torch.nn.Module):
def __init__(self, num_mlp_layers = 5, in_dim=1024, emb_dim = 300, out_dim = 2000, drop_ratio = 0):
super(MLP, self).__init__()
self.num_mlp_layers = num_mlp_layers
self.in_dim = in_dim
self.emb_dim = emb_dim
self.out_dim = out_dim
self.drop_ratio = drop_ratio
# mlp
module_list = [
torch.nn.Linear(self.in_dim, self.emb_dim),
torch.nn.BatchNorm1d(self.emb_dim),
torch.nn.ReLU(),
torch.nn.Dropout(p = self.drop_ratio),
]
for i in range(self.num_mlp_layers - 1):
module_list += [torch.nn.Linear(self.emb_dim, self.emb_dim),
torch.nn.BatchNorm1d(self.emb_dim),
torch.nn.ReLU(),
torch.nn.Dropout(p = self.drop_ratio)]
# relu is applied in the last layer to ensure positivity
module_list += [torch.nn.Linear(self.emb_dim, self.out_dim)]
self.mlp = torch.nn.Sequential(
*module_list
)
def forward(self, x):
output = self.mlp(x)
if self.training:
return output
else:
# At inference time, relu is applied to output to ensure positivity
return torch.clamp(output, min=0, max=1)