-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.py
64 lines (53 loc) · 2.22 KB
/
models.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
import torch.nn as nn
class MLP(nn.Module):
def __init__(self, m=100, dim_x=1):
super().__init__()
self.encoder = nn.Sequential(nn.Linear(m + dim_x, 100),
nn.ReLU(),
nn.Linear(100, 100),
nn.ReLU())
self.regression_layer = nn.Linear(100, 1)
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, std=0.001)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def forward(self, x):
features = self.encoder(x)
pred = self.regression_layer(features)
return pred, features
class MLP_classification(nn.Module):
def __init__(self, m=100, dim_x=1, bins=100):
super().__init__()
self.encoder = nn.Sequential(nn.Linear(m + dim_x, 100),
nn.ReLU(),
nn.Linear(100, 100),
nn.ReLU())
self.classification_layer = nn.Linear(100, bins)
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, std=0.001)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def forward(self, x):
features = self.encoder(x)
pred = self.classification_layer(features)
return pred, features