-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSBP_utils.py
435 lines (354 loc) · 14.8 KB
/
SBP_utils.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import torch
from torch.distributions.normal import Normal
from scipy import special
import numpy as np
from torch.distributions.uniform import Uniform
import torch.nn.functional as F
import torch.nn as nn
import math
def where(cond, x_1, x_2):
return (cond * x_1) + ((1-cond) * x_2)
def erfcx(x):
"""M. M. Shepherd and J. G. Laframboise,
MATHEMATICS OF COMPUTATION 36, 249 (1981)
"""
x = x.cpu()
K = 3.75
y = (torch.abs(x)-K) / (torch.abs(x)+K)
y2 = 2.0*y
(d, dd) = (-0.4e-20, 0.0)
(d, dd) = (y2 * d - dd + 0.3e-20, d)
(d, dd) = (y2 * d - dd + 0.97e-19, d)
(d, dd) = (y2 * d - dd + 0.27e-19, d)
(d, dd) = (y2 * d - dd + -0.2187e-17, d)
(d, dd) = (y2 * d - dd + -0.2237e-17, d)
(d, dd) = (y2 * d - dd + 0.50681e-16, d)
(d, dd) = (y2 * d - dd + 0.74182e-16, d)
(d, dd) = (y2 * d - dd + -0.1250795e-14, d)
(d, dd) = (y2 * d - dd + -0.1864563e-14, d)
(d, dd) = (y2 * d - dd + 0.33478119e-13, d)
(d, dd) = (y2 * d - dd + 0.32525481e-13, d)
(d, dd) = (y2 * d - dd + -0.965469675e-12, d)
(d, dd) = (y2 * d - dd + 0.194558685e-12, d)
(d, dd) = (y2 * d - dd + 0.28687950109e-10, d)
(d, dd) = (y2 * d - dd + -0.63180883409e-10, d)
(d, dd) = (y2 * d - dd + -0.775440020883e-09, d)
(d, dd) = (y2 * d - dd + 0.4521959811218e-08, d)
(d, dd) = (y2 * d - dd + 0.10764999465671e-07, d)
(d, dd) = (y2 * d - dd + -0.218864010492344e-06, d)
(d, dd) = (y2 * d - dd + 0.774038306619849e-06, d)
(d, dd) = (y2 * d - dd + 0.4139027986073010e-05, d)
(d, dd) = (y2 * d - dd + -0.69169733025012064e-04, d)
(d, dd) = (y2 * d - dd + 0.490775836525808632e-03, d)
(d, dd) = (y2 * d - dd + -0.2413163540417608191e-02, d)
(d, dd) = (y2 * d - dd + 0.9074997670705265094e-02, d)
(d, dd) = (y2 * d - dd + -0.26658668435305752277e-01, d)
(d, dd) = (y2 * d - dd + 0.59209939998191890498e-01, d)
(d, dd) = (y2 * d - dd + -0.84249133366517915584e-01, d)
(d, dd) = (y2 * d - dd + -0.4590054580646477331e-02, d)
d = y * d - dd + 0.1177578934567401754080e+01
result = d/(1.0+2.0*torch.abs(x))
result[result!=result] = 1.0
result[result == float("Inf")] = 1.0
negative_mask = torch.zeros(x.size())
negative_mask[x<=0] = 1.0
positive_mask = torch.zeros(x.size())
positive_mask[x>0] = 1.0
negative_result = 2.0*torch.exp(x*x)-result
negative_result[negative_result!=negative_result] = 1.0
negative_result[negative_result == float("Inf")] = 1.0
result = negative_mask * negative_result + positive_mask * result
result = result.cuda()
return result
def phi(x):
normal = Normal(loc=torch.cuda.FloatTensor([0.0]), scale=torch.cuda.FloatTensor([1.0]))
return normal.cdf(x)
def phi_inv(x):
normal = Normal(loc=torch.cuda.FloatTensor([0.0]), scale=torch.cuda.FloatTensor([1.0]))
return normal.icdf(x)
def mean_truncated_log_normal_straight(mu, sigma, a, b):
alpha = (a - mu)/sigma
beta = (b - mu)/sigma
z = phi(beta) - phi(alpha)
mean = torch.exp(mu+sigma*sigma/2.0)/z*(phi(sigma-alpha) - phi(sigma-beta))
return mean
def mean_truncated_log_normal_reduced(mu, sigma, a, b):
alpha = (a - mu)/sigma
beta = (b - mu)/sigma
z = phi(beta) - phi(alpha)
mean = erfcx((sigma-beta)/(2 ** 0.5))*torch.exp(b-beta*beta/2)
mean = mean - erfcx((sigma-alpha)/(2 ** 0.5))*torch.exp(a-alpha*alpha/2)
mean = mean/(2*z)
return mean
def sample_truncated_normal(mu, sigma, a, b):
alpha = (a - mu)/sigma
beta = (b - mu)/sigma
uniform = Uniform(low=0.0,high=1.0)
sampled_uniform = uniform.sample(mu.size())
sampled_uniform = sampled_uniform.cuda()
gamma = phi(alpha)+sampled_uniform*(phi(beta)-phi(alpha))
return torch.clamp(phi_inv(torch.clamp(gamma, min=1e-5, max=1.0-1e-5))*sigma+mu, min=a, max=b)
def snr_truncated_log_normal(mu, sigma, a, b):
alpha = (a - mu)/sigma
beta = (b - mu)/sigma
z = phi(beta) - phi(alpha)
ratio = erfcx((sigma-beta)/(2 ** 0.5))*torch.exp((b-mu)-beta**2/2.0)
ratio = ratio - erfcx((sigma-alpha)/2 ** 0.5)*torch.exp((a-mu)-alpha**2/2.0)
denominator = 2*z*erfcx((2.0*sigma-beta)/2 ** 0.5)*torch.exp(2.0*(b-mu)-beta**2/2.0)
denominator = denominator - 2*z*erfcx((2.0*sigma-alpha)/(2 ** 0.5))*torch.exp(2.0*(a-mu)-alpha**2/2.0)
denominator = denominator - ratio**2
ratio = ratio/torch.sqrt(1e-8 + denominator)
return ratio
def test():
relative_error = 0
for i in range(100):
x = -1 + i * (10 - (-1)) / 100
my_erfcx = erfcx(torch.FloatTensor([x]))
relative_error = relative_error + np.abs(my_erfcx.item() - special.erfcx(x)) / special.erfcx(x)
average_error = relative_error / 100
print(average_error)
normal = Normal(loc=torch.Tensor([0.0]), scale=torch.Tensor([1.0]))
# cdf from 0 to x
print(normal.cdf(1.6449))
print(normal.icdf(torch.Tensor([0.95])))
def multi_dimension_expand(x,w):
x = x.unsqueeze(1)
x = x.unsqueeze(2)
x = x.expand(x.size(0),w.size(2),w.size(3))
return x
def score_predict(model, x):
score = model(x)
score = F.softmax(score, dim=1)
_, prediction = score.max(1)
return prediction
def accuracy(iter, model):
total = 0.0
correct = 0.0
with torch.no_grad():
for images, labels in iter:
images = images.cuda()
preds = score_predict(model, images)
total += labels.size(0)
correct += (preds.cpu().data == labels).sum().item()
return correct / total
class Conv2d_SBP(nn.Module):
"""
Conv2d layer with a SBP layer
This module is the same as
stack nn.Conv2d and SBP_layer together
"""
def __init__(self, input_channel = 3, output_channel = 6, kernel_size = 3, stride=1, padding=0, init_logsigma=-5):
super(Conv2d_SBP, self).__init__()
self.stride = stride
self.padding = padding
sigma = init_logsigma * torch.ones(output_channel)
self.log_sigma = nn.Parameter(sigma)
mu = (torch.zeros(output_channel))
self.mu = nn.Parameter(mu)
w = torch.zeros(output_channel,input_channel,kernel_size,kernel_size)
w = nn.init.xavier_normal_(w)
self.weight = nn.Parameter(w)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
def forward(self, x):
"""
Sample noise e ~ N(1, alpha)
Multiply noise h = h_ * e
"""
def pdf(x):
normal = Normal(loc=torch.cuda.FloatTensor([0.0]),scale=torch.cuda.FloatTensor([1.0]))
return torch.exp(normal.log_prob(x))
min_log = -20.0
max_log = 0.0
log_sigma = torch.clamp(self.log_sigma, min=-20,max=5.0)
mu = torch.clamp(self.mu,min=-20,max=5.0)
sigma = torch.exp(log_sigma)
alpha = (min_log-mu)/sigma
beta = (max_log-mu)/sigma
if self.training:
z = phi(beta) - phi(alpha)
kl = -log_sigma - torch.log(z) - (alpha * pdf(alpha) - beta * pdf(beta)) / (2.0 * z)
kl = kl + np.log(max_log - min_log) - np.log(2.0 * np.pi * np.e) / 2.0
kl = kl.mean()
multiplicator = torch.exp(sample_truncated_normal(mu, sigma, min_log, max_log))
output = F.conv2d(x, self.weight, stride=self.stride, padding=self.padding)
multiplicator = multi_dimension_expand(multiplicator, output)
output = multiplicator*output
# print(weight.size())
# print(x.size())
return output,kl
else:
multiplicator = mean_truncated_log_normal_reduced(mu.detach(), sigma.detach(), min_log, max_log)
snr = snr_truncated_log_normal(mu.detach(), sigma.detach(), min_log, max_log)
mask = snr
mask[snr <= 1.0] = 0.0
mask[snr > 1.0] = 1.0
multiplicator = multiplicator * mask
output = F.conv2d(x, self.weight, stride=self.stride, padding=self.padding)
multiplicator = multi_dimension_expand(multiplicator, output)
output = multiplicator * output
return output
def layer_sparsity(self):
min_log = -20.0
max_log = 0.0
mu = self.mu
sigma = torch.exp(self.log_sigma)
snr = snr_truncated_log_normal(mu.detach(), sigma.detach(), min_log, max_log)
mask = snr
mask[snr <= 1.0] = 0.0
mask[snr > 1.0] = 1.0
#print(mask)
s_ratio = torch.sum(mask.view(-1)==0.0).item() / mask.view(-1).size(0)
return s_ratio
def display_snr(self):
log_sigma = self.log_sigma.detach()
mu = self.mu.detach()
snr = snr_truncated_log_normal(mu, torch.exp(log_sigma), -20.0, 0.0)
mean = snr.mean()
return mean
class Linear_SBP(nn.Module):
"""
linear layer with a SBP layer
This module is the same as
stack nn.Linear and SBP_layer together
"""
def __init__(self, in_features, out_features, init_logsigma=-5,bias=True):
super(Linear_SBP, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = nn.Parameter(torch.Tensor(out_features, in_features))
if bias:
self.bias = nn.Parameter(torch.Tensor(out_features))
else:
self.register_parameter('bias', None)
self.reset_parameters()
sigma = init_logsigma * torch.ones(out_features)
self.log_sigma = nn.Parameter(sigma)
mu = (torch.zeros(out_features))
self.mu = nn.Parameter(mu)
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input):
def pdf(x):
normal = Normal(loc=torch.cuda.FloatTensor([0.0]),scale=torch.cuda.FloatTensor([1.0]))
return torch.exp(normal.log_prob(x))
min_log = -20.0
max_log = 0.0
log_sigma = torch.clamp(self.log_sigma, min=-20,max=5.0)
mu = torch.clamp(self.mu,min=-20,max=5.0)
sigma = torch.exp(log_sigma)
alpha = (min_log-mu)/sigma
beta = (max_log-mu)/sigma
if self.training:
z = phi(beta) - phi(alpha)
kl = -log_sigma - torch.log(z) - (alpha * pdf(alpha) - beta * pdf(beta)) / (2.0 * z)
kl = kl + np.log(max_log - min_log) - np.log(2.0 * np.pi * np.e) / 2.0
kl = kl.mean()
multiplicator = torch.exp(sample_truncated_normal(mu, sigma, min_log, max_log))
output = F.linear(input, self.weight, self.bias)
output = multiplicator*output
return output,kl
else:
multiplicator = mean_truncated_log_normal_reduced(mu.detach(), sigma.detach(), min_log, max_log)
snr = snr_truncated_log_normal(mu.detach(), sigma.detach(), min_log, max_log)
mask = snr
mask[snr <= 1.0] = 0.0
mask[snr > 1.0] = 1.0
multiplicator = multiplicator * mask
output = F.linear(input, self.weight, self.bias)
output = multiplicator * output
return output
def layer_sparsity(self):
min_log = -20.0
max_log = 0.0
mu = self.mu
sigma = torch.exp(self.log_sigma)
snr = snr_truncated_log_normal(mu.detach(), sigma.detach(), min_log, max_log)
mask = snr
mask[snr <= 1.0] = 0.0
mask[snr > 1.0] = 1.0
#print(mask)
s_ratio = torch.sum(mask.view(-1)==0.0).item() / mask.view(-1).size(0)
return s_ratio
def display_snr(self):
log_sigma = self.log_sigma.detach()
mu = self.mu.detach()
snr = snr_truncated_log_normal(mu, torch.exp(log_sigma), -20.0, 0.0)
mean = snr.mean()
return mean
class SBP_layer(nn.Module):
"""
Structured Bayesian Pruning layer
Mathmatichs: y_i = x_i*theta_i, where p(theta_i) ~ Log_uniform[a,b]
Approximate posterior of theta_i is given by: q(theta_i | mu_i, sigma_i^2) ~ Log_norm[a,b](theta_i | mu_i, sigma_i^2)
The target is to optimize KL divergence between p(theta_i) and q(theta_i | mu_i, sigma_i^2): KL(p||q)
Sample usage:
from SBP_utils import SBP_layer
#for CNN layer, input_dim is number of channels
#for linear layer, input_dim is number of neurons
sbp_layer = SBP_layer(input_dim)
#don't forget add kl to loss
y, kl = sbp_layer(x)
loss = loss + kl
"""
def __init__(self, input_dim, init_logsigma=-5):
super(SBP_layer, self).__init__()
sigma = init_logsigma * torch.ones(input_dim)
self.log_sigma = nn.Parameter(sigma)
mu = (torch.zeros(input_dim))
self.mu = nn.Parameter(mu)
def forward(self, input):
def pdf(x):
normal = Normal(loc=torch.cuda.FloatTensor([0.0]),scale=torch.cuda.FloatTensor([1.0]))
return torch.exp(normal.log_prob(x))
min_log = -20.0
max_log = 0.0
log_sigma = torch.clamp(self.log_sigma, min=-20,max=5.0)
mu = torch.clamp(self.mu,min=-20,max=5.0)
sigma = torch.exp(log_sigma)
alpha = (min_log-mu)/sigma
beta = (max_log-mu)/sigma
if self.training:
z = phi(beta) - phi(alpha)
kl = -log_sigma - torch.log(z) - (alpha * pdf(alpha) - beta * pdf(beta)) / (2.0 * z)
kl = kl + np.log(max_log - min_log) - np.log(2.0 * np.pi * np.e) / 2.0
kl = kl.mean()
multiplicator = torch.exp(sample_truncated_normal(mu, sigma, min_log, max_log))
if (input.size().__len__() == 4):
multiplicator = multi_dimension_expand(multiplicator, input)
output = multiplicator*input
return output,kl
else:
multiplicator = mean_truncated_log_normal_reduced(mu.detach(), sigma.detach(), min_log, max_log)
snr = snr_truncated_log_normal(mu.detach(), sigma.detach(), min_log, max_log)
mask = snr
mask[snr <= 1.0] = 0.0
mask[snr > 1.0] = 1.0
multiplicator = multiplicator * mask
if (input.size().__len__() == 4):
multiplicator = multi_dimension_expand(multiplicator, input)
output = multiplicator * input
return output
def layer_sparsity(self):
min_log = -20.0
max_log = 0.0
mu = self.mu
sigma = torch.exp(self.log_sigma)
snr = snr_truncated_log_normal(mu.detach(), sigma.detach(), min_log, max_log)
mask = snr
mask[snr <= 1.0] = 0.0
mask[snr > 1.0] = 1.0
#print(mask)
s_ratio = torch.sum(mask.view(-1)==0.0).item() / mask.view(-1).size(0)
return s_ratio
def display_snr(self):
log_sigma = self.log_sigma.detach()
mu = self.mu.detach()
snr = snr_truncated_log_normal(mu, torch.exp(log_sigma), -20.0, 0.0)
mean = snr.mean()
return mean