-
Notifications
You must be signed in to change notification settings - Fork 27
/
model.py
53 lines (43 loc) · 1.53 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
import torch.nn as nn
import torch.nn.functional as F
from utils import ReverseLayerF
class Extractor(nn.Module):
def __init__(self):
super(Extractor, self).__init__()
self.extractor = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5, padding=2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(in_channels=32, out_channels=48, kernel_size=5, padding=2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2)
)
def forward(self, x):
x = self.extractor(x)
x = x.view(-1, 3 * 28 * 28)
return x
class Classifier(nn.Module):
def __init__(self):
super(Classifier, self).__init__()
self.classifier = nn.Sequential(
nn.Linear(in_features=3 * 28 * 28, out_features=100),
nn.ReLU(),
nn.Linear(in_features=100, out_features=100),
nn.ReLU(),
nn.Linear(in_features=100, out_features=10)
)
def forward(self, x):
x = self.classifier(x)
return x
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.discriminator = nn.Sequential(
nn.Linear(in_features=3 * 28 * 28, out_features=100),
nn.ReLU(),
nn.Linear(in_features=100, out_features=2)
)
def forward(self, input_feature, alpha):
reversed_input = ReverseLayerF.apply(input_feature, alpha)
x = self.discriminator(reversed_input)
return x