-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
60 lines (53 loc) · 1.91 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from functools import reduce
from torch import nn
class MLP(nn.Module):
def __init__(self, input_size, output_size,
hidden_size=400,
hidden_layer_num=2,
hidden_dropout_prob=.5,
input_dropout_prob=.2,
lamda=40):
# Configurations.
super().__init__()
self.input_size = input_size
self.input_dropout_prob = input_dropout_prob
self.hidden_size = hidden_size
self.hidden_layer_num = hidden_layer_num
self.hidden_dropout_prob = hidden_dropout_prob
self.output_size = output_size
self.lamda = lamda
# Layers.
self.layers = nn.ModuleList([
# input
nn.Linear(self.input_size, self.hidden_size), nn.ReLU(),
nn.Dropout(self.input_dropout_prob),
# hidden
*((nn.Linear(self.hidden_size, self.hidden_size), nn.ReLU(),
nn.Dropout(self.hidden_dropout_prob)) * self.hidden_layer_num),
# output
nn.Linear(self.hidden_size, self.output_size)
])
@property
def name(self):
return (
'MLP'
'-lambda{lamda}'
'-in{input_size}-out{output_size}'
'-h{hidden_size}x{hidden_layer_num}'
'-dropout_in{input_dropout_prob}_hidden{hidden_dropout_prob}'
).format(
lamda=self.lamda,
input_size=self.input_size,
output_size=self.output_size,
hidden_size=self.hidden_size,
hidden_layer_num=self.hidden_layer_num,
input_dropout_prob=self.input_dropout_prob,
hidden_dropout_prob=self.hidden_dropout_prob,
)
def forward(self, x):
return reduce(lambda x, l: l(x), self.layers, x)
if __name__ == "__main__":
a = MLP(input_size=100, output_size=20)
print(a)
for name, _ in a.named_parameters():
print(name)