-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodels.py
120 lines (95 loc) · 4.21 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import torch.nn as nn
from torch.autograd import Function
# HDA Feature Projector
class Projector(nn.Module):
def __init__(self, d_input, d_common, layer):
super(Projector, self).__init__()
if layer.lower() == "single":
layer = nn.Linear(d_input, d_common)
leaky_relu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
# init weight and bias
nn.init.normal_(layer.weight, std=0.01)
nn.init.normal_(layer.bias, std=0.01)
projector = nn.Sequential(layer, leaky_relu)
elif layer.lower() == "double":
d_intermediate = int((d_input + d_common) / 2)
layer1 = nn.Linear(d_input, d_intermediate)
leaky_relu1 = nn.LeakyReLU(negative_slope=0.2, inplace=True)
layer2 = nn.Linear(d_intermediate, d_common)
leaky_relu2 = nn.LeakyReLU(negative_slope=0.2, inplace=True)
# init weight and bias
nn.init.normal_(layer1.weight, std=0.01)
nn.init.normal_(layer1.bias, std=0.01)
nn.init.normal_(layer2.weight, std=0.01)
nn.init.normal_(layer2.bias, std=0.01)
projector = nn.Sequential(layer1, leaky_relu1, layer2, leaky_relu2)
else:
raise Exception("Input layer invalid! ")
self.projector = projector
def forward(self, x):
return nn.functional.normalize(self.projector(x), dim=1, p=2)
# Label Classifier
class Classifier(nn.Module):
def __init__(self, d_common, class_number):
super(Classifier, self).__init__()
layer = nn.Linear(d_common, class_number)
leakey_relu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
# init weight and bias
nn.init.normal_(layer.weight, std=0.01)
nn.init.normal_(layer.bias, std=0.01)
self.class_classifier = nn.Sequential(layer, leakey_relu)
def forward(self, x):
return self.class_classifier(x)
class ReverseLayerF(Function):
r"""Gradient Reverse Layer(Unsupervised Domain Adaptation by Backpropagation)
Definition: During the forward propagation, GRL acts as an identity transform. During the back propagation though,
GRL takes the gradient from the subsequent level, multiplies it by -alpha and pass it to the preceding layer.
Args:
x (Tensor): the input tensor
alpha (float): \alpha = \frac{2}{1+\exp^{-\gamma \cdot p}}-1 (\gamma =10)
out (Tensor): the same output tensor as x
"""
@staticmethod
def forward(ctx, x, alpha):
ctx.alpha = alpha
return x.view_as(x)
@staticmethod
def backward(ctx, grad_output):
output = grad_output.neg() * ctx.alpha
return output, None
# Domain Discriminator
class Discriminator(nn.Module):
def __init__(self, d_common):
super(Discriminator, self).__init__()
layer = nn.Linear(d_common, 1)
sigmod = nn.Sigmoid()
# init weight and bias
nn.init.normal_(layer.weight, std=0.01)
nn.init.normal_(layer.bias, std=0.01)
self.discriminator = nn.Sequential(layer, sigmod)
def forward(self, x, alpha):
x = ReverseLayerF.apply(x, alpha)
x = self.discriminator(x)
return x
# Prototypical Network
class Prototypical(nn.Module):
def __init__(self, d_source, d_target, d_common, class_number, layer):
super(Prototypical, self).__init__()
self.d_common = d_common
self.d_source = d_source
self.d_target = d_target
self.class_number = class_number
self.layer = layer
self.projector_source = Projector(self.d_source, self.d_common, self.layer)
self.projector_target = Projector(self.d_target, self.d_common, self.layer)
self.classifier = Classifier(self.d_common, self.class_number)
def forward(self, input_feature):
if input_feature.shape[1] == self.d_source:
feature = self.projector_source(input_feature)
elif input_feature.shape[1] == self.d_target:
feature = self.projector_target(input_feature)
else:
raise Exception("Input data wrong dimension! ")
feature = feature.view(-1, self.d_common)
classifier_output = self.classifier(feature)
return classifier_output, feature