-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
75 lines (52 loc) · 1.93 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import torchvision.models as models
class FcNet(nn.Module):
"""
Fully connected network for MNIST classification
"""
def __init__(self, input_dim, hidden_dims, output_dim, dropout_p=0.0):
super().__init__()
self.input_dim = input_dim
self.hidden_dims = hidden_dims
self.output_dim = output_dim
self.dropout_p = dropout_p
self.dims = [self.input_dim]
self.dims.extend(hidden_dims)
self.dims.append(self.output_dim)
self.layers = nn.ModuleList([])
for i in range(len(self.dims) - 1):
ip_dim = self.dims[i]
op_dim = self.dims[i + 1]
self.layers.append(
nn.Linear(ip_dim, op_dim, bias=True)
)
self.__init_net_weights__()
def __init_net_weights__(self):
for m in self.layers:
m.weight.data.normal_(0.0, 0.1)
m.bias.data.fill_(0.1)
def forward(self, x):
x = x.view(-1, self.input_dim)
for i, layer in enumerate(self.layers):
x = layer(x)
# Do not apply ReLU on the final layer
if i < (len(self.layers) - 1):
x = F.relu(x)
if i < (len(self.layers) - 1): # No dropout on output layer
x = F.dropout(x, p=self.dropout_p, training=self.training)
return x
def feature_extractor(self, x):
x = x.view(-1, self.input_dim)
for i, layer in enumerate(self.layers):
if i == (len(self.layers) - 1):
return x
x = layer(x)
# Do not apply ReLU on the final layer
if i < (len(self.layers) - 1):
x = F.relu(x)
if i < (len(self.layers) - 1): # No dropout on output layer
x = F.dropout(x, p=self.dropout_p, training=self.training)
return x