-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlp_model.py
48 lines (36 loc) · 1.3 KB
/
mlp_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
import torch.nn as nn
class BasicBlock(nn.Module):
def __init__(self, in_size, out_size):
super(BasicBlock, self).__init__()
self.fc = nn.Linear(in_size, out_size)
self.dropout = nn.Dropout(0.3)
self.relu = nn.ReLU()
def forward(self, x):
x = self.fc(x)
x = self.dropout(x)
x = self.relu(x)
return x
class SEMLP(nn.Module):
def __init__(self, in_size, out_size, hidden_size, num_layer):
super(SEMLP, self).__init__()
layers = []
if num_layer == 1:
self.pipe_line = nn.Sequential(nn.Linear(in_size, out_size))
else:
layers.append(BasicBlock(in_size, hidden_size))
for i in range(num_layer - 1):
layers.append(BasicBlock(hidden_size, hidden_size))
layers.append(nn.Linear(hidden_size, out_size))
layers.append(nn.LogSigmoid())
self.pipe_line = nn.Sequential(*layers)
self.init_weights()
def forward(self, x):
x = self.pipe_line(x)
return x
def init_weights(self):
"""
Initialize weights for convolution layers using Xavier initialization.
"""
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)