-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpatterns.py
542 lines (446 loc) · 18.9 KB
/
patterns.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
"""Perform pattern computation."""
import torch
import numpy as np
DTYPE = torch.float32
# statistics computation
def compute_statistics(X, Y, Y_wb, device=torch.device("cpu")):
"""
Compute statistics of the given data e.g. mean over x and y, covariance of
x and y.
Statistics needed:
for a linear:
covariance of x and y for each neuron in y
variance of y for each neuron in y
for a+/a-:
mean_y+ for each neuron in y
mean_y- for each neuron in y
means_x+ for each neuron in y
means_x- for each neuron in y
means_yx+ for each neuron in y
means_yx- for each neuron in y
Parameters
----------
X: matrix with inputs to the layer
shape: sxn with n number of neurons, s number
samples
Y: matrix with outputs of the layer, before relu
or other functions applied
shape: sxm with m number of neurons, s number
of samples
Returns
-------
dict with statistics
"""
num_samples = X.size()[0]
num_neurons_x = X.size()[1]
num_neurons_y = Y.size()[1]
# create a dictionary with three subdictionaries
# one for the linear estimators and two for the two component estimator
lin_dict = {
"e_y": torch.zeros((1, num_neurons_y), device=device, dtype=DTYPE),
"e_yy": torch.zeros((1, num_neurons_y), device=device, dtype=DTYPE),
"e_x": torch.zeros(
(num_neurons_y, num_neurons_x), device=device, dtype=DTYPE
),
"e_xy": torch.zeros(
(num_neurons_y, num_neurons_x), device=device, dtype=DTYPE
),
"cnt" : X.shape[0]
}
pos_dict = {
"e_xy": torch.zeros(
(num_neurons_y, num_neurons_x), device=device, dtype=DTYPE
),
"e_x": torch.zeros(
(num_neurons_y, num_neurons_x), device=device, dtype=DTYPE
),
"e_y": torch.zeros((1, num_neurons_y), device=device, dtype=DTYPE),
"cnt" : torch.zeros(num_neurons_y, device=device, dtype=DTYPE)
}
stat_dict = {"linear": lin_dict, "positive": pos_dict}
e_x_linear = torch.mean(X, dim=0)
e_x_linear = e_x_linear.type(DTYPE)
stat_dict["linear"]["e_x"] = e_x_linear.expand(
num_neurons_y, num_neurons_x
)
for i in range(num_neurons_y):
# for each neuron and each estimator (component) compute the expected
# value of x and y and the expected values of x and y, respectively
# start with the linear ones:
_, e_y_linear, e_xy_linear = _mean_x_y_xy(X, Y[:, i])
yy = torch.mul(Y[:, i], Y[:, i])
e_yy = torch.mean(yy)
stat_dict["linear"]["e_yy"][0, i] = e_yy
stat_dict["linear"]["e_y"][0, i] = e_y_linear
stat_dict["linear"]["e_xy"][i, :] = e_xy_linear
# now the positive ones:
# first find the samples with positive y-value
ind_pos = torch.squeeze(torch.nonzero(Y_wb[:, i] > 0))
ind_pos_wob = torch.squeeze(torch.nonzero(Y[:, i] > 0))
# paper seems to use e_y over all y-values, not just the positive ones
stat_dict["positive"]["e_y"][0, i] = e_y_linear
if ind_pos.size() == torch.Size([0]):
# device needed here?
stat_dict["positive"]["e_xy"][i, :] = torch.zeros(
(1, num_neurons_x), device=device, dtype=DTYPE
)
stat_dict["positive"]["e_x"][i, :] = torch.zeros(
(1, num_neurons_x), device=device, dtype=DTYPE
)
# stat_dict["positive"]["e_y"][0, i] = 0
else:
# select only samples with positive y-value in x
x_plus = torch.index_select(X, 0, ind_pos)
# select only samples with positive y-value in y
y_plus = torch.index_select(Y[:, i], 0, ind_pos)
cnt = (
1
if ind_pos.shape == torch.Size([])
else ind_pos.shape[0]
)
stat_dict["positive"]["cnt"][i] = cnt
e_x_pos, e_y_pos, e_xy_pos = _mean_x_y_xy(x_plus, y_plus)
stat_dict["positive"]["e_xy"][i, :] = e_xy_pos
stat_dict["positive"]["e_x"][i, :] = e_x_pos
# stat_dict["positive"]["e_y"][0, i] = e_y_pos
return stat_dict
def update_statistics(
X, Y, Y_wb, stats_dict, device=torch.device("cpu")
):
""" Perform a linear update of the statistics in `stats_dict` with the new
data given in `X`, `Y` and `Y_wb`.
Parameters
----------
X: torch.FloatTensor
input to layer for which statistics should be computed
Y: torch.FloatTensor
output of layer for which statistics should be comptuted
Y_wb: torch.FloatTensor
output of layer for which statistics should be computed minus bias
of the layer
stats_dict: dict
Statistics dictionary as returned from `compute_statistics`. Needs
to have been computed on a layer with the same input and output
shapes as given by `X` and `Y`.
device: torch.device
device to use for the statistics update computation
Returns
-------
dict
dictionary with the updated statistics
"""
# first compute new statistics
new_stats = compute_statistics(X, Y, Y_wb, device)
# compute old and new sample factors for linear and positive statistics
factor_old_linear = stats_dict['linear']['cnt'] / (stats_dict['linear']['cnt'] \
+ new_stats['linear']['cnt'])
factor_new_linear = 1 - factor_old_linear
# for the positive statistics start with zeros to avoid having to divide by zero
factor_old_positive = torch.zeros(stats_dict['positive']['cnt'].shape)
inds_nonzero = torch.squeeze(stats_dict['positive']['cnt'].nonzero())
factor_old_positive[inds_nonzero] = stats_dict['positive']['cnt'][inds_nonzero] \
/ (stats_dict['positive']['cnt'][inds_nonzero] + \
new_stats['positive']['cnt'][inds_nonzero])
factor_new_positive = torch.ones(stats_dict['positive']['cnt'].shape) - \
factor_old_positive
for param_type in stats_dict:
for param in stats_dict[param_type]:
# cnt only needed for factors
if param == 'cnt':
continue
# use linear factors
if param_type == 'linear' or param == 'e_y':
new_stats[param_type][param] = factor_old_linear * stats_dict[param_type][param] \
+ factor_new_linear * new_stats[param_type][param]
# use factors for plus/minus-patterns
else:
old_stats_weighted = _rowwise_mul(stats_dict[param_type][param],
factor_old_positive)
new_stats_weighted = _rowwise_mul(new_stats[param_type][param],
factor_new_positive)
new_stats[param_type][param] = old_stats_weighted + new_stats_weighted
# before returning new_stats the cnt has to be updated as well
new_stats['linear']['cnt'] += stats_dict['linear']['cnt']
new_stats['positive']['cnt'] += stats_dict['positive']['cnt']
return new_stats
# pattern computation
def _compute_a(weights, stats):
""" Computes the linear and +/- patterns.
Parameters
----------
weights: torch.FloatTensor
layer weights. For conv layers must be transformed to
two-dimensional weight matrix
stats: dict
dictionary with the computed statistics
Returns
-------
dict
dictionary with the linear and +/- patterns
"""
# for the linear estimator
# the variance of y
weights = weights.type(DTYPE)
var_y = (
stats["linear"]["e_yy"]
- stats["linear"]["e_y"] * stats["linear"]["e_y"]
)
# the covariance between x and y
cov_xy = stats["linear"]["e_xy"] - _rowwise_mul(
stats["linear"]["e_x"], torch.squeeze(stats["linear"]["e_y"])
)
# the linear estimator is cov/var_y
a_linear = _rowwise_div(cov_xy, torch.squeeze(var_y))
# for the plus-minus estimator
# get Ex*Ey
ex_ey = _rowwise_mul(
stats["positive"]["e_x"], torch.squeeze(stats["positive"]["e_y"])
)
# get the nominator of the whole formula: Exy - Ex*Ey
nom_a_plus = stats["positive"]["e_xy"] - ex_ey
# now the denominator
w_exy = _rowwise_mul(weights, stats["positive"]["e_xy"])
w_ex_ey = _rowwise_mul(weights, ex_ey)
denom_a_plus = w_exy - w_ex_ey
# now divide the nominator by the denominator
a_plus = _rowwise_div(nom_a_plus, denom_a_plus)
a_dict = {"A_linear": a_linear, "A_plus": a_plus}
return a_dict
def compute_patterns_linear(stats, weights):
""" Computes patterns for dense layers.
Parameters
----------
stats: dict
dictionary with the computed statistics
weights: torch.FloatTensor
weight matrix of the dense layer
Returns
-------
dict
dictionary with the linear and +/- patterns of the layer
"""
a_dict = _compute_a(weights, stats)
return a_dict
def compute_patterns_conv(stats, kernel):
""" Computes patterns for conv2d layers.
Parameters
----------
stats: dict
dictionary with the computed statistics
kernel: torch.FloatTensor
kernel weights of the conv2d layer
Returns
-------
dict
dictionary with the linear and +/- patterns of the layer
"""
# convert kernel to weight matrix
k_as_w = _conv_kernel_to_dense(kernel)
# print("Conv kernel as dense weight matrix:")
# print(k_as_w.shape)
# print(k_as_w)
# compute patterns from weight matrix and statistics
a_dict = _compute_a(k_as_w, stats)
# get size of pattern kernel in right order st view works along the right
# dimensions
k_s = (kernel.shape[0], kernel.shape[2], kernel.shape[3], kernel.shape[1])
# k_s = (kernel.shape[1], kernel.shape[0], kernel.shape[2], kernel.shape[3])
# convert pattern matrix to pattern kernel
a_dict["A_linear"] = a_dict["A_linear"].contiguous().view(k_s)
a_dict["A_plus"] = a_dict["A_plus"].contiguous().view(k_s)
a_dict["A_linear"] = a_dict["A_linear"].permute(3, 0, 1, 2)
a_dict["A_plus"] = a_dict["A_plus"].permute(3, 0, 1, 2)
# the kernels have to be reversed along the height and width dimension
def revert_tensor(tensor, axis=0):
idx = [i for i in range(tensor.size(axis) - 1, -1, -1)]
idx = torch.LongTensor(idx)
return tensor.index_select(axis, idx)
a_dict["A_linear"] = revert_tensor(a_dict["A_linear"], 2)
a_dict["A_linear"] = revert_tensor(a_dict["A_linear"], 3)
a_dict["A_plus"] = revert_tensor(a_dict["A_plus"], 2)
a_dict["A_plus"] = revert_tensor(a_dict["A_plus"], 3)
return a_dict
###############################################################################
############ helper functions ###############################################
###############################################################################
def _mean_x_y_xy(x, y):
""" Compute the columnwise mean of x, the mean of y and the mean of x*y."""
# x = x.type(torch.float32)
# y = y.type(torch.float32)
# m_x = torch.sum(x, dim=0, dtype=torch.float64) / x.shape[0]
m_x = torch.mean(x, dim=0)
# m_y = torch.sum(y, dtype=torch.float64) / y.shape[0]
m_y = torch.mean(y)
xy = x * torch.t(y.expand_as(torch.t(x)))
m_xy = torch.mean(xy, dim=0)
# m_xy = torch.sum(xy, dim=0) / xy.shape[0]
return m_x, m_y, m_xy
def _rowwise_mul(matrix, other):
""" Compute the rowwise multiplication of `matrix` and `other`
`other` can be a scalar, a vector or a matrix
- if `other` is a scalar the matrix is multiplied by the scalar
- if `other` is a vector each row of `matrix` is multiplied with the
corresponding scalar value in the vector
- if `other` is a matrix the dot product of the corresponding rows of
the two matrices is computed
Parameters
----------
matrix: torch.FloatTensor
matrix which should be multiplied with sth else rowwise
other: torch.FloatTensor or float
scalar, vector or matrix with which `matrix` should be multiplied.
Returns
-------
torch.FloatTensor
result of the rowwise multiplication
"""
# compute the rowwise multiplication by seperately
# going throuth the rows
if other.dim() == 0:
result = matrix * other
elif other.dim() == 1:
result = torch.zeros(matrix.size(), dtype=DTYPE)
for i in range(matrix.size()[0]):
result[i, :] = matrix[i, :] * other[i]
else:
result = torch.zeros(matrix.size()[0], dtype=DTYPE)
for i in range(matrix.size()[0]):
result[i] = torch.dot(matrix[i, :], other[i, :])
return result
def _rowwise_div(matrix, vector):
if vector.dim() == 0:
return matrix * (1 / vector)
result = torch.zeros(matrix.size(), dtype=DTYPE)
for i in range(matrix.size()[0]):
# if there's a 0 in vector we just set the result
# to 0 or leave the values that are in matrix?
if vector[i] == 0:
result[i, :] = matrix[i, :]
else:
result[i, :] = matrix[i, :] * (1 / vector[i])
return result
""" reshapes the input map, kernels and output map of a convolutional
layer to a dense layer
converts a convolutional layer to a fully connected layer,
i.e. a layer where the output is a simple matrix
multiplication of the weight matrix and the input
Note that the input and output of the resulting fc layer
are two dimensional for one sample already which is one
dimension more than for a typical fully connected layer
PADDING NOT IMPLEMENTED YET!!!
input:
inp: input to the convolutional layer
shape: batch_size x inp_channels x height x width
kernels: kernels of the convolutional layer
shape: out_channels x inp_channels x kernel x kernel
stride: stride size
padding: how many padding values are added to each side
output:
inp_fc: input as fc layer
weights: kernels as weight matrix
out_fc: output of the layer as fc layer
"""
i_s = inp.size()
k_s = kernels.size()
# patch size
num_rows = k_s[1] * k_s[2] * k_s[3]
num_cols = int((i_s[2] - k_s[2]) / stride + 1) * int(
(i_s[3] - k_s[3]) / stride + 1
)
# print(num_rows,num_cols)
weight_matrix_height = k_s[0] # number of kernels
weight_matrix_width = k_s[1] * k_s[2] * k_s[3] # one kernel reshaped
# batch_size at first dimension
inp_fc = torch.zeros(i_s[0], num_rows, num_cols)
weights = torch.zeros(weight_matrix_height, weight_matrix_width)
# now insert all the patches into inp_fc
col_count = 0
for i in range(0, i_s[2] - k_s[2] + 1, stride):
for j in range(0, i_s[3] - k_s[3] + 1, stride):
# print(
# (
# (inp[:, :, i : i + k_s[2], j : j + k_s[3]])
# .contiguous()
# .view(i_s[0], num_rows, -1)
# .size()
# )
# )
# print(inp_fc[:, :, col_count].size())
inp_fc[:, :, [col_count]] = (
(inp[:, :, i : i + k_s[2], j : j + k_s[3]]).contiguous()
).view(i_s[0], num_rows, -1)
col_count += 1
# now flatten the kernels and save them into the weight matrix
for k in range(k_s[0]):
weights[k, :] = kernels[k, :, :, :].contiguous().view(1, -1)
# now the output can be computed as a simple matrix multiplication
out_fc = torch.matmul(weights, inp_fc)
return inp_fc, weights, out_fc
def _conv_maps_to_dense(
inp, out, kernel_size, stride=1, padding=None, device=torch.device("cpu")
):
"""
Transform convolutional input and output map to dense maps
Parameters
----------
inp: convolutional input map of a layer
out: convolutioanl output map of a layer
kernel_size: size of kernels used in this layer
padding: number of zeros used for padding, if None, no padding
Returns
-------
inp_dense: input map as dense map
out_dense: output map as dense map
"""
# device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# if theres padding there are more patches -> first convert input map to
# input map with zeros padded, then do the rest as before?
if padding is not None:
i_s = inp.size()
# zeros on the right and left of each map
zeros_rl = torch.zeros(
(i_s[0], i_s[1], i_s[2], padding), device=device
)
# zeros at the top and bottom of each map
zeros_tb = torch.zeros(
(i_s[0], i_s[1], padding, i_s[3] + 2 * padding), device=device
)
inp = torch.cat((zeros_rl, inp, zeros_rl), dim=3)
inp = torch.cat((zeros_tb, inp, zeros_tb), dim=2)
# conv map shape: samples x channels x height x width
i_s = inp.size()
o_s = out.size()
num_rows = i_s[1] * kernel_size[0] * kernel_size[1]
num_cols = int((i_s[2] - kernel_size[0]) / stride + 1) * int(
(i_s[3] - kernel_size[1]) / stride + 1
)
inp_dense = torch.zeros((i_s[0], num_rows, num_cols), device=device)
col_count = 0
for i in range(0, i_s[2] - kernel_size[0] + 1, stride):
for j in range(0, i_s[3] - kernel_size[1] + 1, stride):
patch = (
inp[:, :, i : i + kernel_size[0], j : j + kernel_size[1]]
).contiguous()
# INNVESTIGATE: next line added for innvestigate similarity
patch = patch.permute(0, 2, 3, 1).contiguous()
#############################################
patch_reshaped = patch.view(i_s[0], num_rows)
# patch_reshaped = patch.view(i_s[0], num_rows, -1).squeeze()
inp_dense[:, :, col_count] = patch_reshaped
col_count += 1
# out_dense = torch.zeros(o_s[0],o_s[1],o_s[2]*o_s[3])
out_dense = out.view(o_s[0], o_s[1], o_s[2] * o_s[3])
# reshaping so that the maps can directly be used for the statistics
# computation
inp_dense_t = torch.transpose(inp_dense, 1, 2).contiguous()
inp_stat = inp_dense_t.view(i_s[0] * num_cols, -1)
out_dense_t = torch.transpose(out_dense, 1, 2).contiguous()
out_stat = out_dense_t.view(o_s[0] * o_s[2] * o_s[3], -1)
return inp_stat, out_stat
def _conv_kernel_to_dense(kernels):
k_s = kernels.size()
kernels = kernels.contiguous()
# first permute the order, then reshape the kernel
kernels_permuted = kernels.permute(0, 2, 3, 1).contiguous()
weights = kernels_permuted.view(k_s[0], k_s[1] * k_s[2] * k_s[3])
return weights