-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregularizer.py
340 lines (288 loc) · 11.3 KB
/
regularizer.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
from __future__ import division
import torch
import torch.nn as nn
import torch.autograd as autograd
import hessianflow as hf
import hessianflow.optimizer.optm_utils as hf_optm_utils
import hessianflow.optimizer.progressbar as hf_optm_pgb
def zero_gradients(i):
for t in iter_gradients(i):
t.zero_()
class JacobianReg(nn.Module):
'''
Loss criterion that computes the trace of the square of the Jacobian.
Arguments:
n (int, optional): determines the number of random projections.
If n=-1, then it is set to the dimension of the output
space and projection is non-random and orthonormal, yielding
the exact result. For any reasonable batch size, the default
(n=1) should be sufficient.
'''
def __init__(self, n=1):
assert n == -1 or n > 0
self.n = n
super(JacobianReg, self).__init__()
def forward(self, x, y):
'''
computes (1/2) tr |dy/dx|^2
'''
B,C = y.shape
if self.n == -1:
num_proj = C
else:
num_proj = self.n
J2 = 0
for ii in range(num_proj):
if self.n == -1:
# orthonormal vector, sequentially spanned
v=torch.zeros(B,C)
v[:,ii]=1
else:
# random properly-normalized vector for each sample
v = self._random_vector(C=C,B=B)
if x.is_cuda:
v = v.cuda()
Jv = self._jacobian_vector_product(y, x, v, create_graph=True)
J2 += C*torch.norm(Jv)**2 / (num_proj*B)
R = (1/2)*J2
return R
def _random_vector(self, C, B):
'''
creates a random vector of dimension C with a norm of C^(1/2)
(as needed for the projection formula to work)
'''
if C == 1:
return torch.ones(B)
v=torch.randn(B,C)
arxilirary_zero=torch.zeros(B,C)
vnorm=torch.norm(v, 2, 1,True)
v=torch.addcdiv(arxilirary_zero, 1.0, v, vnorm)
return v
def _jacobian_vector_product(self, y, x, v, create_graph=False):
'''
Produce jacobian-vector product dy/dx dot v.
Note that if you want to differentiate it,
you need to make create_graph=True
'''
flat_y = y.reshape(-1)
flat_v = v.reshape(-1)
grad_x, = torch.autograd.grad(flat_y, x, flat_v,
retain_graph=True,
create_graph=create_graph)
return grad_x
class PJacobiNormReg(nn.Module):
'''
Loss criterion that computes the trace of the square of the Jacobian.
Arguments:
n (int, optional): determines the number of random projections.
If n=-1, then it is set to the dimension of the output
space and projection is non-random and orthonormal, yielding
the exact result. For any reasonable batch size, the default
(n=1) should be sufficient.
'''
def __init__(self, n=2, p=1):
assert n == -1 or n > 0
self.n = n
self.p = p
super(PJacobiNormReg, self).__init__()
def forward(self, x, y):
'''
computes (1/2) tr |dy/dx|^2
'''
B, C = y.shape
if self.n == -1:
num_iter = C
else:
num_iter = self.n
J2 = 0
index = torch.argsort(y, dim=1, descending=True)
v = torch.zeros(B,C)
for ii in range(num_iter):
v += torch.eye(C)[index[:,ii]]
Jv = self._jacobian_vector_product(y, x, v.cuda(), create_graph=True)
J2 += torch.norm(Jv, self.p)/(num_iter * B)
return J2
def _jacobian_vector_product(self, y, x, v, create_graph=False):
'''
Produce jacobian-vector product dy/dx dot v.
Note that if you want to differentiate it,
you need to make create_graph=True
'''
flat_y = y.reshape(-1)
flat_v = v.reshape(-1)
grad_x, = torch.autograd.grad(flat_y, x, flat_v,
retain_graph=True,
create_graph=create_graph)
return grad_x
class JacobiNormReg(nn.Module):
'''
Loss criterion that computes the trace of the square of the Jacobian.
Arguments:
n (int, optional): determines the number of random projections.
If n=-1, then it is set to the dimension of the output
space and projection is non-random and orthonormal, yielding
the exact result. For any reasonable batch size, the default
(n=1) should be sufficient.
'''
# def __init__(self, n=2, p=1):
def __init__(self, n=2, p='fro'):
assert n == -1 or n > 0
self.n = n
self.p = p
super(JacobiNormReg, self).__init__()
def forward(self, x, y):
'''
computes (1/2) tr |dy/dx|^2
'''
B, C = y.shape
if self.n == -1:
num_iter = C
else:
num_iter = self.n
J2 = 0
index = torch.argsort(y, dim=1, descending=True)
for ii in range(num_iter):
v = torch.eye(C)[index[:,ii]]
Jv = self._jacobian_vector_product(y, x, v.cuda(), create_graph=True)
J2 += torch.norm(Jv, self.p)/(num_iter * B)
return J2
def _jacobian_vector_product(self, y, x, v, create_graph=False):
'''
Produce jacobian-vector product dy/dx dot v.
Note that if you want to differentiate it,
you need to make create_graph=True
'''
flat_y = y.reshape(-1)
flat_v = v.reshape(-1)
grad_x, = torch.autograd.grad(flat_y, x, flat_v,
retain_graph=True,
create_graph=create_graph)
return grad_x
class JacobiLossNormReg(nn.Module):
'''
Loss criterion that computes the trace of the square of the Jacobian.
Arguments:
n (int, optional): determines the number of random projections.
If n=-1, then it is set to the dimension of the output
space and projection is non-random and orthonormal, yielding
the exact result. For any reasonable batch size, the default
(n=1) should be sufficient.
'''
def __init__(self, n=2, p=1):
assert n == -1 or n > 0
self.n = n
self.p = p
super(JacobiLossNormReg, self).__init__()
def forward(self, x, loss):
'''
computes (1/2) tr |dy/dx|^2
'''
B = x.shape[0]
Jv = self._jacobian_vector_product(loss, x, create_graph=True)
J2 = torch.norm(Jv, self.p)/B
return J2
def _jacobian_vector_product(self, loss, x, create_graph=False):
'''
Produce jacobian-vector product dy/dx dot v.
Note that if you want to differentiate it,
you need to make create_graph=True
'''
flat_loss = loss.reshape(-1)
grad_x, = torch.autograd.grad(flat_loss, x, retain_graph=True,
create_graph=create_graph)
return grad_x
class JacobiAngularReg(nn.Module):
'''
Loss criterion that computes the trace of the square of the Jacobian.
Arguments:
n (int, optional): determines the number of random projections.
If n=-1, then it is set to the dimension of the output
space and projection is non-random and orthonormal, yielding
the exact result. For any reasonable batch size, the default
(n=1) should be sufficient.
'''
def __init__(self, n=2, p=1):
assert n == -1 or n > 0
self.n = n
self.p = p
super(JacobiAngularReg, self).__init__()
def forward(self, x, y):
'''
computes (1/2) tr |dy/dx|^2
'''
B, C = y.shape
if self.n == -1:
num_iter = C
else:
num_iter = self.n
J2 = 0
index = torch.argsort(y, dim=1, descending=True)
for ii in range(num_iter):
v = torch.eye(C)[index[:,ii]]
Jv = self._jacobian_vector_product(y, x, v.cuda(), create_graph=True)
J2 += torch.norm(Jv, self.p)/B
return J2
def _jacobian_vector_product(self, y, x, v, create_graph=False):
'''
Produce jacobian-vector product dy/dx dot v.
Note that if you want to differentiate it,
you need to make create_graph=True
'''
flat_y = y.reshape(-1)
flat_v = v.reshape(-1)
grad_x, = torch.autograd.grad(flat_y, x, flat_v,
retain_graph=True,
create_graph=create_graph)
return grad_x
class loss_curv():
def __init__(self, net, criterion, lambda_, device='cuda'):
self.net = net
self.criterion = criterion
self.lambda_ = lambda_
self.device = device
def _find_z(self, inputs, alphas, targets, h):
inputs.requires_grad_()
outputs = self.net.eval()(inputs, alphas)
# print(targets.size()[0])
loss_z = self.criterion(outputs, targets) #self.net.eval()(inputs)
# loss_z = self.net.module.loss(inputs, alphas, targets)
loss_z.backward(torch.ones(targets.size(), dtype=torch.float)[0].to(self.device)) #torch.ones(targets.size(), dtype=torch.float).to(self.device)
grad = inputs.grad.data + 0.0
norm_grad = grad.norm().item()
z = torch.sign(grad).detach() + 0.
z = 1. * (h) * (z + 1e-7) / (z.reshape(z.size(0), -1).norm(dim=1)[:, None, None, None] + 1e-7)
inputs.grad.detach()
inputs.grad.zero_()
#zero_gradients(inputs)
self.net.zero_grad()
return z, norm_grad
def regularizer(self, inputs, alphas, targets, h=3., lambda_=4):
z, norm_grad = self._find_z(inputs, alphas, targets, h)
inputs.requires_grad_()
outputs_pos = self.net.eval()(inputs + z, alphas)
outputs_orig = self.net.eval()(inputs, alphas)
loss_pos = self.criterion(outputs_pos, targets)
loss_orig = self.criterion(outputs_orig, targets)
grad_diff = \
torch.autograd.grad((loss_pos - loss_orig), inputs, grad_outputs=torch.ones(targets.size())[0].to(self.device),
create_graph=True)[0]
reg = grad_diff.reshape(grad_diff.size(0), -1).norm(dim=1)
self.net.zero_grad()
return torch.sum(self.lambda_ * reg) / float(inputs.size(0)), norm_grad
class loss_eigen():
def __init__(self, net, test_loader, input, target, criterion, full_eigen, maxIter=10, tol=1e-2):
self.net = net
self.test_loader = test_loader
self.criterion = criterion
self.full_eigen = full_eigen
self.max_iter = maxIter
self.tol = tol
self.input = input
self.target = target
self.cuda = True
def regularizer(self):
if self.full_eigen:
eigenvalue, eigenvector = hf.get_eigen_full_dataset(self.net, self.test_loader, self.criterion, self.max_iter, self.tol)
else:
eigenvalue, eigenvector= hf.get_eigen(self.net, self.input, self.target, self.criterion, self.cuda, self.max_iter, self.tol)
return eigenvalue, eigenvector