-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_layers.py
304 lines (261 loc) · 11.5 KB
/
model_layers.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""
This file includes the basic layers of the models
"""
import numpy as np
import torch
import math
import scipy.io as iso
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data as Data
import matplotlib.pyplot as plt
from sklearn import preprocessing
class SourceProbFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input, nn_output, prediction, p_t):
ctx.save_for_backward(input, nn_output, prediction, p_t)
return input
@staticmethod
def backward(ctx, grad_output):
#print "Source back called"
input, nn_output, prediction, p_t = ctx.saved_tensors
grad_input = grad_out = grad_pred = grad_p_t = None
if ctx.needs_input_grad[0]:
grad_input = torch.sum(nn_output.mul(prediction), dim=1).reshape(-1,1)/p_t
if ctx.needs_input_grad[1]:
grad_out = None
if ctx.needs_input_grad[2]:
grad_pred = None
if ctx.needs_input_grad[3]:
grad_p_t = None
return grad_input, grad_out, grad_pred, grad_p_t
class SourceProbLayer(nn.Module):
def __init__(self):
super(SourceProbLayer, self).__init__()
def forward(self, input, nn_output, prediction, p_t):
return SourceProbFunction.apply(input, nn_output, prediction, p_t)
def extra_repr(self):
return "The Layer After Source Density Estimation"
class TargetProbFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input, nn_output, prediction, p_t, p_s):
ctx.save_for_backward(input, nn_output, prediction, p_t, p_s)
return input
@staticmethod
def backward(ctx, grad_output):
#print "Target back called"
input, nn_output, prediction, p_t, p_s = ctx.saved_tensors
grad_input = grad_out = grad_pred = grad_p_t = grad_p_s = None
if ctx.needs_input_grad[0]:
grad_input = -p_s*torch.sum(nn_output.mul(prediction), dim=1).reshape(-1, 1)/(p_t*p_t)
if ctx.needs_input_grad[1]:
grad_out = None
if ctx.needs_input_grad[2]:
grad_pred = None
if ctx.needs_input_grad[3]:
grad_p_t = None
if ctx.needs_input_grad[4]:
grad_p_s = None
return grad_input, grad_out, grad_pred, grad_p_t, grad_p_s
class TargetProbLayer(nn.Module):
def __init__(self):
super(TargetProbLayer, self).__init__()
def forward(self, input, nn_output, prediction, p_t, p_s):
return TargetProbFunction.apply(input, nn_output, prediction, p_t, p_s)
def extra_repr(self):
return "The Layer After Target Density Estimation"
class ClassificationFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input, weight, Y, r_st, bias=True):
exp_temp = input.mm(weight.t()).mul(r_st)
if bias is not None:
exp_temp += bias.unsqueeze(0).expand_as(exp_temp)
output = F.softmax(exp_temp, dim=1)
ctx.save_for_backward(input, weight, bias, output, Y)
return exp_temp
@staticmethod
def backward(ctx, grad_output):
input, weight, bias, output, Y = ctx.saved_tensors
grad_input = grad_weight = grad_bias = grad_Y = grad_r = None
if ctx.needs_input_grad[0]:
grad_input = (output - Y).mm(weight)#/(output.shape[0]*output.shape[1])
if ctx.needs_input_grad[1]:
grad_weight = ((output.t() - Y.t()).mm(input))#/(output.shape[0]*output.shape[1])
if ctx.needs_input_grad[2]:
grad_Y = None
if ctx.needs_input_grad[3]:
grad_r = None
if bias is not None and ctx.needs_input_grad[4]:
grad_bias = grad_output.sum(0).squeeze(0)
return grad_input, grad_weight, grad_Y, grad_r, grad_bias
class ClassifierLayer(nn.Module):
"""
The last layer for C
"""
def __init__(self, input_features, output_features, bias=True):
super(ClassifierLayer, self).__init__()
self.input_features = input_features
self.output_features = output_features
self.weight = nn.Parameter(torch.Tensor(output_features, input_features))
if bias:
self.bias = nn.Parameter(torch.Tensor(output_features))
else:
self.register_parameter("bias", None)
# Weight initialization
self.weight.data.uniform_(-1./math.sqrt(input_features), 1./math.sqrt(input_features))
if bias is not None:
self.bias.data.uniform_(-1./math.sqrt(input_features), 1./math.sqrt(input_features))
def forward(self, input, Y, r):
return ClassificationFunction.apply(input, self.weight, Y, r, self.bias)
def extra_repr(self):
return "in_features={}, output_features={}, bias={}".format(
self.input_features, self.output_features, self.bias is not None
)
class RatioEstimationFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input, nn_output, prediction, pass_sign):
"""
input: The density ratio
nn_output: The output (a new feature) of the classification network, with shape (batch_size, n_classes)
prediction: The probability, with shape (batch_size, n_classes)
"""
ctx.save_for_backward(input, nn_output, prediction, pass_sign)
return input
@staticmethod
def backward(ctx, grad_output):
input, nn_output, prediction, pass_sign = ctx.saved_tensors
grad_input = grad_out = grad_pred = grad_pass = None
if ctx.needs_input_grad[0]:
if pass_sign is None:
grad_input = grad_output.clone()
#print grad_input
else:
grad_input = torch.sum(nn_output.mul(prediction), dim=1).reshape(-1, 1)
#print grad_input
if ctx.needs_input_grad[1]:
grad_out = None
if ctx.needs_input_grad[2]:
grad_pred = None
if ctx.needs_input_grad[3]:
grad_pass = None
return grad_input, grad_out, grad_pred, grad_pass
class RatioEstimationLayer(nn.Module):
"""
The last layer for D
"""
def __init__(self):
super(RatioEstimationLayer, self).__init__()
def forward(self, input, nn_output, prediction, pass_sign):
return RatioEstimationFunction.apply(input, nn_output, prediction, pass_sign)
def extra_repr(self):
return "Ratio Estimation Layer"
class Flatten(torch.nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
class SourceGradFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input, nn_output, prediction, p_t, sign_variable):
ctx.save_for_backward(input, nn_output, prediction, p_t, sign_variable)
return input
@staticmethod
def backward(ctx, grad_output):
input, nn_output, prediction, p_t, sign_variable = ctx.saved_tensors
#print "Source back called", sign_variable
grad_input = grad_out = grad_pred = grad_p_t = grad_sign = None
if ctx.needs_input_grad[0]:
if sign_variable is None:
grad_input = grad_output
else:
grad_input = 1e3*torch.sum(nn_output.mul(prediction), dim=1).reshape(-1,1)/p_t
if ctx.needs_input_grad[1]:
grad_out = None
if ctx.needs_input_grad[2]:
grad_pred = None
if ctx.needs_input_grad[3]:
grad_p_t = None
return grad_input, grad_out, grad_pred, grad_p_t, grad_sign
class SourceGradLayer(nn.Module):
def __init__(self):
super(SourceGradLayer, self).__init__()
def forward(self, input, nn_output, prediction, p_t, sign_variable):
return SourceGradFunction.apply(input, nn_output, prediction, p_t, sign_variable)
def extra_repr(self):
return "The Layer After Source Density Estimation, but different from SourceProbLayer"
class GradFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input, nn_output, prediction, p_t, sign_variable):
ctx.save_for_backward(input, nn_output, prediction, p_t, sign_variable)
return input
@staticmethod
def backward(ctx, grad_output):
input, nn_output, prediction, p_t, sign_variable = ctx.saved_tensors
grad_input = grad_out = grad_pred = grad_p_t = grad_sign = None
if ctx.needs_input_grad[0]:
# The parameters here controls the uncertainty measurement entropy of the results
if sign_variable is None:
grad_input = grad_output * 1e2
else:
grad_source = torch.sum(nn_output.mul(prediction), dim=1).reshape(-1,1)/p_t
grad_target = torch.sum(nn_output.mul(prediction), dim=1).reshape(-1,1) * (-(1-p_t)/p_t**2)
grad_source /= prediction.shape[0]
grad_target /= prediction.shape[0]
grad_input = 1e-1 * torch.cat((grad_source, grad_target), dim=1)/p_t.shape[0]
grad_input = 1e1 * grad_input
if ctx.needs_input_grad[1]:
grad_out = None
if ctx.needs_input_grad[2]:
grad_pred = None
if ctx.needs_input_grad[3]:
grad_p_t = None
return grad_input, grad_out, grad_pred, grad_p_t, grad_sign
class GradLayer(nn.Module):
def __init__(self):
super(GradLayer, self).__init__()
def forward(self, input, nn_output, prediction, p_t, sign_variable):
return GradFunction.apply(input, nn_output, prediction, p_t, sign_variable)
def extra_repr(self):
return "The Layer After Source Density Estimation"
class IWFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input, weight, Y, r_ts, bias=True):
exp_temp = input.mm(weight.t())
if bias is not None:
exp_temp += bias.unsqueeze(0).expand_as(exp_temp)
output = F.softmax(exp_temp, dim=1)
ctx.save_for_backward(input, weight, Y, r_ts, output, bias)
return exp_temp
@staticmethod
def backward(ctx, grad_output):
input, weight, Y, r_ts, output, bias = ctx.saved_tensors
grad_input = grad_weight = grad_Y = grad_bias = grad_r = None
if ctx.needs_input_grad[0]:
grad_input = (output - Y).mm(weight)/(input.shape[0]*Y.shape[1])
if ctx.needs_input_grad[1]:
grad_weight = (output.t() - Y.t()).mm(input*r_ts)/(input.shape[0]*Y.shape[1])
if ctx.needs_input_grad[2]:
grad_Y = None
if ctx.needs_input_grad[3]:
grad_r = None
if bias is not None and ctx.needs_input_grad[4]:
grad_bias = grad_output.sum(0).squeeze(0)
return grad_input, grad_weight, grad_Y, grad_r, grad_bias
class IWLayer(nn.Module):
def __init__(self, input_features, output_features, bias=True):
super(IWLayer, self).__init__()
self.input_features = input_features
self.output_features = output_features
self.weight = nn.Parameter(torch.Tensor(output_features, input_features))
if bias:
self.bias = nn.Parameter(torch.Tensor(output_features))
else:
self.register_parameter("bias", None)
# Weight initialization
self.weight.data.uniform_(-1./math.sqrt(input_features), 1./math.sqrt(input_features))
if bias is not None:
self.bias.data.uniform_(-1./math.sqrt(input_features), 1./math.sqrt(input_features))
def forward(self, input, Y, r):
return IWFunction.apply(input, self.weight, Y, r, self.bias)
def extra_repr(self):
return "in_features={}, output_features={}, bias={}".format(
self.input_features, self.output_features, self.bias is not None
)