-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.py
More file actions
80 lines (70 loc) · 2.37 KB
/
Copy pathmodules.py
File metadata and controls
80 lines (70 loc) · 2.37 KB
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
76
77
78
79
80
import torch, torchvision
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Function
class GradReverse(Function):
def __init__(self):
super(GradReverse, self).__init__()
def _set_lambda(self, lbda):
self.lbda = lbda
def forward(self, x):
return x.view_as(x)
def backward(self, grad_output):
return grad_output.neg() * self.lbda
class GradNet(nn.Module):
def __init__(self, init_weight):
super(GradNet, self).__init__()
self.E = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=5, padding=2),
nn.ReLU(True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
nn.Conv2d(64, 64, kernel_size=5, padding=2),
nn.ReLU(True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
nn.Conv2d(64, 128, kernel_size=5, padding=2),
nn.ReLU(True),
nn.Dropout(0.5)
)
in_features = 128 * 7 * 7
self.grad_revers = GradReverse()
self.G_d = nn.Sequential(
nn.Linear(in_features, 1024),
nn.ReLU(True),
nn.Linear(1024, 1024),
nn.ReLU(True),
nn.Linear(1024, 2),
nn.LogSoftmax()
)
self.G_c = nn.Sequential(
nn.Linear(in_features, 3072),
nn.ReLU(True),
nn.Linear(3072, 2048),
nn.ReLU(True),
nn.Linear(2048, 10),
nn.LogSoftmax()
)
if init_weight:
self._init_weight()
def _set_lambda(self, lbda):
self.grad_revers._set_lambda(lbda)
def forward(self, x, d_classify=False, classify=False):
x = self.E(x)
x = x.view(x.size(0), -1)
if d_classify:
y = self.grad_revers(x)
y = self.G_d(y)
if classify:
return self.G_c(x), y
else:
return y
return self.G_c(x)
def _init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
nn.init.constant_(m.bias, 0)