dl-backtrace 0.0.12__py3-none-any.whl → 0.0.16__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of dl-backtrace might be problematic. Click here for more details.

Files changed (27) hide show
  1. dl_backtrace/pytorch_backtrace/backtrace/backtrace.py +173 -44
  2. dl_backtrace/pytorch_backtrace/backtrace/utils/__init__.py +3 -0
  3. dl_backtrace/pytorch_backtrace/backtrace/utils/encoder.py +183 -0
  4. dl_backtrace/pytorch_backtrace/backtrace/utils/encoder_decoder.py +489 -0
  5. dl_backtrace/pytorch_backtrace/backtrace/utils/helper.py +95 -0
  6. dl_backtrace/pytorch_backtrace/backtrace/utils/prop.py +481 -0
  7. dl_backtrace/tf_backtrace/backtrace/__init__.py +1 -2
  8. dl_backtrace/tf_backtrace/backtrace/activation_info.py +33 -0
  9. dl_backtrace/tf_backtrace/backtrace/backtrace.py +506 -279
  10. dl_backtrace/tf_backtrace/backtrace/models.py +25 -0
  11. dl_backtrace/tf_backtrace/backtrace/server.py +27 -0
  12. dl_backtrace/tf_backtrace/backtrace/utils/__init__.py +5 -2
  13. dl_backtrace/tf_backtrace/backtrace/utils/encoder.py +206 -0
  14. dl_backtrace/tf_backtrace/backtrace/utils/encoder_decoder.py +501 -0
  15. dl_backtrace/tf_backtrace/backtrace/utils/helper.py +99 -0
  16. dl_backtrace/tf_backtrace/backtrace/utils/utils_contrast.py +1132 -0
  17. dl_backtrace/tf_backtrace/backtrace/utils/utils_prop.py +1582 -0
  18. dl_backtrace/version.py +2 -2
  19. {dl_backtrace-0.0.12.dist-info → dl_backtrace-0.0.16.dist-info}/METADATA +3 -2
  20. dl_backtrace-0.0.16.dist-info/RECORD +29 -0
  21. {dl_backtrace-0.0.12.dist-info → dl_backtrace-0.0.16.dist-info}/WHEEL +1 -1
  22. dl_backtrace/tf_backtrace/backtrace/config.py +0 -41
  23. dl_backtrace/tf_backtrace/backtrace/utils/contrast.py +0 -834
  24. dl_backtrace/tf_backtrace/backtrace/utils/prop.py +0 -725
  25. dl_backtrace-0.0.12.dist-info/RECORD +0 -21
  26. {dl_backtrace-0.0.12.dist-info → dl_backtrace-0.0.16.dist-info}/LICENSE +0 -0
  27. {dl_backtrace-0.0.12.dist-info → dl_backtrace-0.0.16.dist-info}/top_level.txt +0 -0
@@ -1,725 +0,0 @@
1
- import gc
2
-
3
- import numpy as np
4
- import tensorflow as tf
5
- from numpy.lib.stride_tricks import as_strided
6
- from tensorflow.keras import backend as K
7
-
8
-
9
- def np_swish(x, beta=0.75):
10
- z = 1 / (1 + np.exp(-(beta * x)))
11
- return x * z
12
-
13
-
14
- def np_wave(x, alpha=1.0):
15
- return (alpha * x * np.exp(1.0)) / (np.exp(-x) + np.exp(x))
16
-
17
-
18
- def np_pulse(x, alpha=1.0):
19
- return alpha * (1 - np.tanh(x) * np.tanh(x))
20
-
21
-
22
- def np_absolute(x, alpha=1.0):
23
- return alpha * x * np.tanh(x)
24
-
25
-
26
- def np_hard_sigmoid(x):
27
- return np.clip(0.2 * x + 0.5, 0, 1)
28
-
29
-
30
- def np_sigmoid(x):
31
- z = 1 / (1 + np.exp(-x))
32
- return z
33
-
34
-
35
- def np_tanh(x):
36
- z = np.tanh(x)
37
- return z.astype(np.float32)
38
-
39
-
40
- class LSTM_forward(object):
41
- def __init__(
42
- self, num_cells, units, weights, return_sequence=False, go_backwards=False
43
- ):
44
- self.num_cells = num_cells
45
- self.units = units
46
- self.kernel = weights[0]
47
- self.recurrent_kernel = weights[1]
48
- self.bias = weights[2]
49
- self.return_sequence = return_sequence
50
- self.go_backwards = go_backwards
51
- self.recurrent_activation = tf.math.sigmoid
52
- self.activation = tf.math.tanh
53
-
54
- self.compute_log = {}
55
- for i in range(self.num_cells):
56
- self.compute_log[i] = {}
57
- self.compute_log[i]["inp"] = None
58
- self.compute_log[i]["x"] = None
59
- self.compute_log[i]["hstate"] = [None, None]
60
- self.compute_log[i]["cstate"] = [None, None]
61
- self.compute_log[i]["int_arrays"] = {}
62
-
63
- def compute_carry_and_output(self, x, h_tm1, c_tm1, cell_num):
64
- """Computes carry and output using split kernels."""
65
- x_i, x_f, x_c, x_o = x
66
- h_tm1_i, h_tm1_f, h_tm1_c, h_tm1_o = h_tm1
67
- i = self.recurrent_activation(
68
- x_i + K.dot(h_tm1_i, self.recurrent_kernel[:, : self.units])
69
- )
70
- f = self.recurrent_activation(
71
- x_f + K.dot(h_tm1_f, self.recurrent_kernel[:, self.units : self.units * 2])
72
- )
73
- c = f * c_tm1 + i * self.activation(
74
- x_c
75
- + K.dot(h_tm1_c, self.recurrent_kernel[:, self.units * 2 : self.units * 3])
76
- )
77
- o = self.recurrent_activation(
78
- x_o + K.dot(h_tm1_o, self.recurrent_kernel[:, self.units * 3 :])
79
- )
80
- self.compute_log[cell_num]["int_arrays"]["i"] = i
81
- self.compute_log[cell_num]["int_arrays"]["f"] = f
82
- self.compute_log[cell_num]["int_arrays"]["c"] = c
83
- self.compute_log[cell_num]["int_arrays"]["o"] = o
84
- return c, o
85
-
86
- def calculate_lstm_cell_wt(self, inputs, states, cell_num, training=None):
87
- h_tm1 = states[0] # previous memory state
88
- c_tm1 = states[1] # previous carry state
89
- self.compute_log[cell_num]["inp"] = inputs
90
- self.compute_log[cell_num]["hstate"][0] = h_tm1
91
- self.compute_log[cell_num]["cstate"][0] = c_tm1
92
- inputs_i = inputs
93
- inputs_f = inputs
94
- inputs_c = inputs
95
- inputs_o = inputs
96
- k_i, k_f, k_c, k_o = tf.split(self.kernel, num_or_size_splits=4, axis=1)
97
- x_i = K.dot(inputs_i, k_i)
98
- x_f = K.dot(inputs_f, k_f)
99
- x_c = K.dot(inputs_c, k_c)
100
- x_o = K.dot(inputs_o, k_o)
101
- b_i, b_f, b_c, b_o = tf.split(self.bias, num_or_size_splits=4, axis=0)
102
- x_i = tf.add(x_i, b_i)
103
- x_f = tf.add(x_f, b_f)
104
- x_c = tf.add(x_c, b_c)
105
- x_o = tf.add(x_o, b_o)
106
-
107
- h_tm1_i = h_tm1
108
- h_tm1_f = h_tm1
109
- h_tm1_c = h_tm1
110
- h_tm1_o = h_tm1
111
- x = (x_i, x_f, x_c, x_o)
112
- h_tm1 = (h_tm1_i, h_tm1_f, h_tm1_c, h_tm1_o)
113
- c, o = self.compute_carry_and_output(x, h_tm1, c_tm1, cell_num)
114
- h = o * self.activation(c)
115
- self.compute_log[cell_num]["x"] = x
116
- self.compute_log[cell_num]["hstate"][1] = h
117
- self.compute_log[cell_num]["cstate"][1] = c
118
- return h, [h, c]
119
-
120
- def calculate_lstm_wt(self, input_data):
121
- hstate = tf.convert_to_tensor(np.zeros((1, self.units)), dtype=tf.float32)
122
- cstate = tf.convert_to_tensor(np.zeros((1, self.units)), dtype=tf.float32)
123
- output = []
124
- for ind in range(input_data.shape[0]):
125
- inp = tf.convert_to_tensor(
126
- input_data[ind, :].reshape((1, input_data.shape[1])), dtype=tf.float32
127
- )
128
- h, s = self.calculate_lstm_cell_wt(inp, [hstate, cstate], ind)
129
- hstate = s[0]
130
- cstate = s[1]
131
- output.append(h)
132
- return output
133
-
134
-
135
- class LSTM_backtrace(object):
136
- def __init__(
137
- self, num_cells, units, weights, return_sequence=False, go_backwards=False
138
- ):
139
- self.num_cells = num_cells
140
- self.units = units
141
- self.kernel = weights[0]
142
- self.recurrent_kernel = weights[1]
143
- self.bias = weights[2]
144
- self.return_sequence = return_sequence
145
- self.go_backwards = go_backwards
146
- self.recurrent_activation = np_sigmoid
147
- self.activation = np_tanh
148
-
149
- self.compute_log = {}
150
-
151
- def calculate_wt_fc(self, wts, inp, w, b, act):
152
- mul_mat = np.einsum("ij,i->ij", w, inp).T
153
- wt_mat = np.zeros(mul_mat.shape)
154
- for i in range(mul_mat.shape[0]):
155
- l1_ind1 = mul_mat[i]
156
- wt_ind1 = wt_mat[i]
157
- wt = wts[i]
158
- p_ind = l1_ind1 > 0
159
- n_ind = l1_ind1 < 0
160
- p_sum = np.sum(l1_ind1[p_ind])
161
- n_sum = np.sum(l1_ind1[n_ind]) * -1
162
- if len(b) > 0:
163
- if b[i] > 0:
164
- pbias = b[i]
165
- nbias = 0
166
- else:
167
- pbias = 0
168
- nbias = b[i] * -1
169
- else:
170
- pbias = 0
171
- nbias = 0
172
- t_sum = p_sum + pbias - n_sum - nbias
173
- if act["type"] == "mono":
174
- if act["range"]["l"]:
175
- if t_sum < act["range"]["l"]:
176
- p_sum = 0
177
- if act["range"]["u"]:
178
- if t_sum > act["range"]["u"]:
179
- n_sum = 0
180
- elif act["type"] == "non_mono":
181
- t_act = act["func"](t_sum)
182
- p_act = act["func"](p_sum + pbias)
183
- n_act = act["func"](-1 * (n_sum + nbias))
184
- if act["range"]["l"]:
185
- if t_sum < act["range"]["l"]:
186
- p_sum = 0
187
- if act["range"]["u"]:
188
- if t_sum > act["range"]["u"]:
189
- n_sum = 0
190
- if p_sum > 0 and n_sum > 0:
191
- if t_act == p_act:
192
- n_sum = 0
193
- elif t_act == n_act:
194
- p_sum = 0
195
- if p_sum > 0:
196
- p_agg_wt = (p_sum + pbias) / (p_sum + n_sum + pbias + nbias)
197
- p_agg_wt = p_agg_wt * (p_sum / (p_sum + pbias))
198
- else:
199
- p_agg_wt = 0
200
- if n_sum > 0:
201
- n_agg_wt = (n_sum + nbias) / (p_sum + n_sum + pbias + nbias)
202
- n_agg_wt = n_agg_wt * (n_sum / (n_sum + nbias))
203
- else:
204
- n_agg_wt = 0
205
- if p_sum == 0:
206
- p_sum = 1
207
- if n_sum == 0:
208
- n_sum = 1
209
- wt_ind1[p_ind] = (l1_ind1[p_ind] / p_sum) * wt * p_agg_wt
210
- wt_ind1[n_ind] = (l1_ind1[n_ind] / n_sum) * wt * n_agg_wt * -1.0
211
- wt_mat = wt_mat.sum(axis=0)
212
- return wt_mat
213
-
214
- def calculate_wt_add(self, wts, inp=None):
215
- wt_mat = []
216
- inp_list = []
217
- for x in inp:
218
- wt_mat.append(np.zeros_like(x))
219
- wt_mat = np.array(wt_mat)
220
- inp_list = np.array(inp)
221
- for i in range(wt_mat.shape[1]):
222
- wt_ind1 = wt_mat[:, i]
223
- wt = wts[i]
224
- l1_ind1 = inp_list[:, i]
225
- p_ind = l1_ind1 > 0
226
- n_ind = l1_ind1 < 0
227
- p_sum = np.sum(l1_ind1[p_ind])
228
- n_sum = np.sum(l1_ind1[n_ind]) * -1
229
- t_sum = p_sum - n_sum
230
- p_agg_wt = 0
231
- n_agg_wt = 0
232
- if p_sum + n_sum > 0:
233
- p_agg_wt = p_sum / (p_sum + n_sum)
234
- n_agg_wt = n_sum / (p_sum + n_sum)
235
- if p_sum == 0:
236
- p_sum = 1
237
- if n_sum == 0:
238
- n_sum = 1
239
- wt_ind1[p_ind] = (l1_ind1[p_ind] / p_sum) * wt * p_agg_wt
240
- wt_ind1[n_ind] = (l1_ind1[n_ind] / n_sum) * wt * n_agg_wt * -1.0
241
- wt_mat[:, i] = wt_ind1
242
- wt_mat = [i.reshape(wts.shape) for i in list(wt_mat)]
243
- return wt_mat
244
-
245
- def calculate_wt_multiply(self, wts, inp=None):
246
- wt_mat = []
247
- inp_list = []
248
- for x in inp:
249
- wt_mat.append(np.zeros_like(x))
250
- wt_mat = np.array(wt_mat)
251
- inp_list = np.array(inp)
252
- inp_prod = inp[0] * inp[1]
253
- inp_diff1 = np.abs(inp_prod - inp[0])
254
- inp_diff2 = np.abs(inp_prod - inp[1])
255
- inp_diff_sum = inp_diff1 + inp_diff2
256
- inp_wt1 = (inp_diff1 / inp_diff_sum) * wts
257
- inp_wt2 = (inp_diff2 / inp_diff_sum) * wts
258
- return [inp_wt1, inp_wt2]
259
-
260
- def compute_carry_and_output(self, wt_o, wt_c, h_tm1, c_tm1, x, cell_num):
261
- """Computes carry and output using split kernels."""
262
- h_tm1_i, h_tm1_f, h_tm1_c, h_tm1_o = (h_tm1, h_tm1, h_tm1, h_tm1)
263
- x_i, x_f, x_c, x_o = x
264
- f = self.compute_log[cell_num]["int_arrays"]["f"].numpy()[0]
265
- i = self.compute_log[cell_num]["int_arrays"]["i"].numpy()[0]
266
- # o = self.recurrent_activation(
267
- # x_o + np.dot(h_tm1_o, self.recurrent_kernel[:, self.units * 3:])).astype(np.float32)
268
- temp1 = np.dot(h_tm1_o, self.recurrent_kernel[:, self.units * 3 :]).astype(
269
- np.float32
270
- )
271
- wt_x_o, wt_temp1 = self.calculate_wt_add(wt_o, [x_o, temp1])
272
- wt_h_tm1_o = self.calculate_wt_fc(
273
- wt_temp1,
274
- h_tm1_o,
275
- self.recurrent_kernel[:, self.units * 3 :],
276
- [],
277
- {"type": None},
278
- )
279
-
280
- # c = f * c_tm1 + i * self.activation(x_c + np.dot(
281
- # h_tm1_c, self.recurrent_kernel[:, self.units * 2:self.units * 3])).astype(np.float32)
282
- temp2 = f * c_tm1
283
- temp3_1 = np.dot(
284
- h_tm1_c, self.recurrent_kernel[:, self.units * 2 : self.units * 3]
285
- )
286
- temp3_2 = self.activation(x_c + temp3_1)
287
- temp3_3 = i * temp3_2
288
- wt_temp2, wt_temp3_3 = self.calculate_wt_add(wt_c, [temp2, temp3_3])
289
- wt_f, wt_c_tm1 = self.calculate_wt_multiply(wt_temp2, [f, c_tm1])
290
- wt_i, wt_temp3_2 = self.calculate_wt_multiply(wt_temp3_3, [i, temp3_2])
291
- wt_x_c, wt_temp3_1 = self.calculate_wt_add(wt_temp3_2, [x_c, temp3_1])
292
- wt_h_tm1_c = self.calculate_wt_fc(
293
- wt_temp3_1,
294
- h_tm1_c,
295
- self.recurrent_kernel[:, self.units * 2 : self.units * 3],
296
- [],
297
- {"type": None},
298
- )
299
-
300
- # f = self.recurrent_activation(x_f + np.dot(
301
- # h_tm1_f, self.recurrent_kernel[:, self.units:self.units * 2])).astype(np.float32)
302
- temp4 = np.dot(h_tm1_f, self.recurrent_kernel[:, self.units : self.units * 2])
303
- wt_x_f, wt_temp4 = self.calculate_wt_add(wt_f, [x_f, temp4])
304
- wt_h_tm1_f = self.calculate_wt_fc(
305
- wt_temp4,
306
- h_tm1_f,
307
- self.recurrent_kernel[:, self.units : self.units * 2],
308
- [],
309
- {"type": None},
310
- )
311
-
312
- # i = self.recurrent_activation(
313
- # x_i + np.dot(h_tm1_i, self.recurrent_kernel[:, :self.units])).astype(np.float32)
314
- temp5 = np.dot(h_tm1_i, self.recurrent_kernel[:, : self.units])
315
- wt_x_i, wt_temp5 = self.calculate_wt_add(wt_i, [x_i, temp5])
316
- wt_h_tm1_i = self.calculate_wt_fc(
317
- wt_temp5,
318
- h_tm1_i,
319
- self.recurrent_kernel[:, : self.units],
320
- [],
321
- {"type": None},
322
- )
323
-
324
- return (
325
- wt_x_i,
326
- wt_x_f,
327
- wt_x_c,
328
- wt_x_o,
329
- wt_h_tm1_i,
330
- wt_h_tm1_f,
331
- wt_h_tm1_c,
332
- wt_h_tm1_o,
333
- wt_c_tm1,
334
- )
335
-
336
- def calculate_lstm_cell_wt(self, cell_num, wts_hstate, wts_cstate):
337
- o = self.compute_log[cell_num]["int_arrays"]["o"].numpy()[0]
338
- c = self.compute_log[cell_num]["cstate"][1].numpy()[0]
339
- h_tm1 = self.compute_log[cell_num]["hstate"][0].numpy()[0]
340
- c_tm1 = self.compute_log[cell_num]["cstate"][0].numpy()[0]
341
- x = [i.numpy()[0] for i in self.compute_log[cell_num]["x"]]
342
- wt_o, wt_c = self.calculate_wt_multiply(
343
- wts_hstate, [o, self.activation(c)]
344
- ) # h = o * self.activation(c)
345
- wt_c = wt_c + wts_cstate
346
- (
347
- wt_x_i,
348
- wt_x_f,
349
- wt_x_c,
350
- wt_x_o,
351
- wt_h_tm1_i,
352
- wt_h_tm1_f,
353
- wt_h_tm1_c,
354
- wt_h_tm1_o,
355
- wt_c_tm1,
356
- ) = self.compute_carry_and_output(wt_o, wt_c, h_tm1, c_tm1, x, cell_num)
357
- wt_h_tm1 = wt_h_tm1_i + wt_h_tm1_f + wt_h_tm1_c + wt_h_tm1_o
358
- inputs = self.compute_log[cell_num]["inp"].numpy()[0]
359
- k_i, k_f, k_c, k_o = np.split(self.kernel, indices_or_sections=4, axis=1)
360
- b_i, b_f, b_c, b_o = np.split(self.bias, indices_or_sections=4, axis=0)
361
-
362
- wt_inputs_i = self.calculate_wt_fc(wt_x_i, inputs, k_i, b_i, {"type": None})
363
- wt_inputs_f = self.calculate_wt_fc(wt_x_f, inputs, k_f, b_f, {"type": None})
364
- wt_inputs_c = self.calculate_wt_fc(wt_x_c, inputs, k_c, b_c, {"type": None})
365
- wt_inputs_o = self.calculate_wt_fc(wt_x_o, inputs, k_o, b_o, {"type": None})
366
-
367
- wt_inputs = wt_inputs_i + wt_inputs_f + wt_inputs_c + wt_inputs_o
368
-
369
- return wt_inputs, wt_h_tm1, wt_c_tm1
370
-
371
- def calculate_lstm_wt(self, wts, compute_log):
372
- self.compute_log = compute_log
373
- output = []
374
- if self.return_sequence:
375
- temp_wts_hstate = wts[-1, :]
376
- else:
377
- temp_wts_hstate = wts
378
- temp_wts_cstate = np.zeros_like(self.compute_log[0]["cstate"][1].numpy()[0])
379
- for ind in range(len(self.compute_log) - 1, -1, -1):
380
- temp_wt_inp, temp_wts_hstate, temp_wts_cstate = self.calculate_lstm_cell_wt(
381
- ind, temp_wts_hstate, temp_wts_cstate
382
- )
383
- output.append(temp_wt_inp)
384
- if self.return_sequence and ind > 0:
385
- temp_wts_hstate = temp_wts_hstate + wts[ind - 1, :]
386
- output.reverse()
387
- return np.array(output)
388
-
389
-
390
- def dummy_wt(wts, inp, *args):
391
- test_wt = np.zeros_like(inp)
392
- return test_wt
393
-
394
-
395
- def calculate_wt_fc(wts, inp, w, b, act):
396
- mul_mat = np.einsum("ij,i->ij", w.numpy(), inp).T
397
- wt_mat = np.zeros(mul_mat.shape)
398
- for i in range(mul_mat.shape[0]):
399
- l1_ind1 = mul_mat[i]
400
- wt_ind1 = wt_mat[i]
401
- wt = wts[i]
402
- p_ind = l1_ind1 > 0
403
- n_ind = l1_ind1 < 0
404
- p_sum = np.sum(l1_ind1[p_ind])
405
- n_sum = np.sum(l1_ind1[n_ind]) * -1
406
- if b.numpy()[i] > 0:
407
- pbias = b.numpy()[i]
408
- nbias = 0
409
- else:
410
- pbias = 0
411
- nbias = b.numpy()[i] * -1
412
- t_sum = p_sum + pbias - n_sum - nbias
413
- if act["type"] == "mono":
414
- if act["range"]["l"]:
415
- if t_sum < act["range"]["l"]:
416
- p_sum = 0
417
- if act["range"]["u"]:
418
- if t_sum > act["range"]["u"]:
419
- n_sum = 0
420
- elif act["type"] == "non_mono":
421
- t_act = act["func"](t_sum)
422
- p_act = act["func"](p_sum + pbias)
423
- n_act = act["func"](-1 * (n_sum + nbias))
424
- if act["range"]["l"]:
425
- if t_sum < act["range"]["l"]:
426
- p_sum = 0
427
- if act["range"]["u"]:
428
- if t_sum > act["range"]["u"]:
429
- n_sum = 0
430
- if p_sum > 0 and n_sum > 0:
431
- if t_act == p_act:
432
- n_sum = 0
433
- elif t_act == n_act:
434
- p_sum = 0
435
- if p_sum > 0:
436
- p_agg_wt = (p_sum + pbias) / (p_sum + n_sum + pbias + nbias)
437
- p_agg_wt = p_agg_wt * (p_sum / (p_sum + pbias))
438
- else:
439
- p_agg_wt = 0
440
- if n_sum > 0:
441
- n_agg_wt = (n_sum + nbias) / (p_sum + n_sum + pbias + nbias)
442
- n_agg_wt = n_agg_wt * (n_sum / (n_sum + nbias))
443
- else:
444
- n_agg_wt = 0
445
- if p_sum == 0:
446
- p_sum = 1
447
- if n_sum == 0:
448
- n_sum = 1
449
- wt_ind1[p_ind] = (l1_ind1[p_ind] / p_sum) * wt * p_agg_wt
450
- wt_ind1[n_ind] = (l1_ind1[n_ind] / n_sum) * wt * n_agg_wt * -1.0
451
-
452
- wt_mat = wt_mat.sum(axis=0)
453
- return wt_mat
454
-
455
-
456
- def calculate_wt_rshp(wts, inp=None):
457
- x = np.reshape(wts, inp.shape)
458
- return x
459
-
460
-
461
- def calculate_wt_concat(wts, inp=None, axis=-1):
462
- splits = [i.shape[axis] for i in inp]
463
- splits = np.cumsum(splits)
464
- if axis > 0:
465
- axis = axis - 1
466
- x = np.split(wts, indices_or_sections=splits, axis=axis)
467
- return x
468
-
469
-
470
- def calculate_wt_add(wts, inp=None):
471
- wt_mat = []
472
- inp_list = []
473
- expanded_wts = as_strided(
474
- wts,
475
- shape=(np.prod(wts.shape),),
476
- strides=(wts.strides[-1],),
477
- writeable=False, # totally use this to avoid writing to memory in weird places
478
- )
479
-
480
- for x in inp:
481
- expanded_input = as_strided(
482
- x,
483
- shape=(np.prod(x.shape),),
484
- strides=(x.strides[-1],),
485
- writeable=False, # totally use this to avoid writing to memory in weird places
486
- )
487
- inp_list.append(expanded_input)
488
- wt_mat.append(np.zeros_like(expanded_input))
489
- wt_mat = np.array(wt_mat)
490
- inp_list = np.array(inp_list)
491
- for i in range(wt_mat.shape[1]):
492
- wt_ind1 = wt_mat[:, i]
493
- wt = expanded_wts[i]
494
- l1_ind1 = inp_list[:, i]
495
- p_ind = l1_ind1 > 0
496
- n_ind = l1_ind1 < 0
497
- p_sum = np.sum(l1_ind1[p_ind])
498
- n_sum = np.sum(l1_ind1[n_ind]) * -1
499
- t_sum = p_sum - n_sum
500
- p_agg_wt = 0
501
- n_agg_wt = 0
502
- if p_sum + n_sum > 0:
503
- p_agg_wt = p_sum / (p_sum + n_sum)
504
- n_agg_wt = n_sum / (p_sum + n_sum)
505
- if p_sum == 0:
506
- p_sum = 1
507
- if n_sum == 0:
508
- n_sum = 1
509
- wt_ind1[p_ind] = (l1_ind1[p_ind] / p_sum) * wt * p_agg_wt
510
- wt_ind1[n_ind] = (l1_ind1[n_ind] / n_sum) * wt * n_agg_wt * -1.0
511
- wt_mat[:, i] = wt_ind1
512
- wt_mat = [i.reshape(wts.shape) for i in list(wt_mat)]
513
- return wt_mat
514
-
515
-
516
- def calculate_start_wt(arg):
517
- x = np.argmax(arg[0])
518
- y = np.zeros(arg.shape)
519
- y[0][x] = 1
520
- return y[0]
521
-
522
-
523
- def calculate_wt_passthru(wts):
524
- return wts
525
-
526
-
527
- def calculate_wt_conv_unit(wt, p_mat, n_mat, t_sum, p_sum, n_sum, act):
528
- wt_mat = np.zeros_like(p_mat)
529
- if act["type"] == "mono":
530
- if act["range"]["l"]:
531
- if t_sum < act["range"]["l"]:
532
- p_sum = 0
533
- if act["range"]["u"]:
534
- if t_sum > act["range"]["u"]:
535
- n_sum = 0
536
- elif act["type"] == "non_mono":
537
- t_act = act["func"](t_sum)
538
- p_act = act["func"](p_sum)
539
- n_act = act["func"](n_sum)
540
- if act["range"]["l"]:
541
- if t_sum < act["range"]["l"]:
542
- p_sum = 0
543
- if act["range"]["u"]:
544
- if t_sum > act["range"]["u"]:
545
- n_sum = 0
546
- if p_sum > 0 and n_sum > 0:
547
- if t_act == p_act:
548
- n_sum = 0
549
- elif t_act == n_act:
550
- p_sum = 0
551
- p_agg_wt = 0.0
552
- n_agg_wt = 0.0
553
- if p_sum + n_sum > 0.0:
554
- p_agg_wt = p_sum / (p_sum + n_sum)
555
- n_agg_wt = n_sum / (p_sum + n_sum)
556
- if p_sum == 0.0:
557
- p_sum = 1.0
558
- if n_sum == 0.0:
559
- n_sum = 1.0
560
- wt_mat = wt_mat + ((p_mat / p_sum) * wt * p_agg_wt)
561
- wt_mat = wt_mat + ((n_mat / n_sum) * wt * n_agg_wt * -1.0)
562
- return wt_mat
563
-
564
-
565
- def calculate_wt_conv(wts, inp, w, b, act):
566
- expanded_input = as_strided(
567
- inp,
568
- shape=(
569
- inp.shape[0]
570
- - w.numpy().shape[0]
571
- + 1, # The feature map is a few pixels smaller than the input
572
- inp.shape[1] - w.numpy().shape[1] + 1,
573
- inp.shape[2],
574
- w.numpy().shape[0],
575
- w.numpy().shape[1],
576
- ),
577
- strides=(
578
- inp.strides[0],
579
- inp.strides[1],
580
- inp.strides[2],
581
- inp.strides[
582
- 0
583
- ], # When we move one step in the 3rd dimension, we should move one step in the original data too
584
- inp.strides[1],
585
- ),
586
- writeable=False, # totally use this to avoid writing to memory in weird places
587
- )
588
- test_wt = np.einsum("mnc->cmn", np.zeros_like(inp), order="C", optimize=True)
589
- for k in range(w.numpy().shape[-1]):
590
- kernel = w.numpy()[:, :, :, k]
591
- x = np.einsum(
592
- "abcmn,mnc->abcmn", expanded_input, kernel, order="C", optimize=True
593
- )
594
- x_pos = x.copy()
595
- x_neg = x.copy()
596
- x_pos[x < 0] = 0
597
- x_neg[x > 0] = 0
598
- x_sum = np.einsum("abcmn->ab", x, order="C", optimize=True)
599
- x_p_sum = np.einsum("abcmn->ab", x_pos, order="C", optimize=True)
600
- x_n_sum = np.einsum("abcmn->ab", x_neg, order="C", optimize=True) * -1.0
601
- # print(np.sum(x),np.sum(x_pos),np.sum(x_neg),np.sum(x_n_sum))
602
- for ind1 in range(expanded_input.shape[0]):
603
- for ind2 in range(expanded_input.shape[1]):
604
- temp_wt_mat = calculate_wt_conv_unit(
605
- wts[ind1, ind2, k],
606
- x_pos[ind1, ind2, :, :, :],
607
- x_neg[ind1, ind2, :, :, :],
608
- x_sum[ind1, ind2],
609
- x_p_sum[ind1, ind2],
610
- x_n_sum[ind1, ind2],
611
- act,
612
- )
613
- test_wt[
614
- :, ind1 : ind1 + kernel.shape[0], ind2 : ind2 + kernel.shape[1]
615
- ] += temp_wt_mat
616
- test_wt = np.einsum("cmn->mnc", test_wt, order="C", optimize=True)
617
- gc.collect()
618
- return test_wt
619
-
620
-
621
- def get_max_index(mat=None):
622
- max_ind = np.argmax(mat)
623
- ind = []
624
- rem = max_ind
625
- for i in mat.shape[:-1]:
626
- ind.append(rem // i)
627
- rem = rem % i
628
- ind.append(rem)
629
- return tuple(ind)
630
-
631
-
632
- def calculate_wt_maxpool(wts, inp, pool_size):
633
- pad1 = pool_size[0]
634
- pad2 = pool_size[1]
635
- test_samp_pad = np.pad(inp, ((0, pad1), (0, pad2), (0, 0)), "constant")
636
- dim1, dim2, _ = wts.shape
637
- test_wt = np.zeros_like(test_samp_pad)
638
- for k in range(inp.shape[2]):
639
- wt_mat = wts[:, :, k]
640
- for ind1 in range(dim1):
641
- for ind2 in range(dim2):
642
- temp_inp = test_samp_pad[
643
- ind1 * pool_size[0] : (ind1 + 1) * pool_size[0],
644
- ind2 * pool_size[1] : (ind2 + 1) * pool_size[1],
645
- k,
646
- ]
647
- max_index = get_max_index(temp_inp)
648
- test_wt[
649
- ind1 * pool_size[0] : (ind1 + 1) * pool_size[0],
650
- ind2 * pool_size[1] : (ind2 + 1) * pool_size[1],
651
- k,
652
- ][max_index] = wt_mat[ind1, ind2]
653
- test_wt = test_wt[0 : inp.shape[0], 0 : inp.shape[1], :]
654
- return test_wt
655
-
656
-
657
- def calculate_wt_avgpool(wts, inp, pool_size):
658
- pad1 = pool_size[0]
659
- pad2 = pool_size[1]
660
- test_samp_pad = np.pad(inp, ((0, pad1), (0, pad2), (0, 0)), "constant")
661
- dim1, dim2, _ = wts.shape
662
- test_wt = np.zeros_like(test_samp_pad)
663
- for k in range(inp.shape[2]):
664
- wt_mat = wts[:, :, k]
665
- for ind1 in range(dim1):
666
- for ind2 in range(dim2):
667
- temp_inp = test_samp_pad[
668
- ind1 * pool_size[0] : (ind1 + 1) * pool_size[0],
669
- ind2 * pool_size[1] : (ind2 + 1) * pool_size[1],
670
- k,
671
- ]
672
- wt_ind1 = test_wt[
673
- ind1 * pool_size[0] : (ind1 + 1) * pool_size[0],
674
- ind2 * pool_size[1] : (ind2 + 1) * pool_size[1],
675
- k,
676
- ]
677
- wt = wt_mat[ind1, ind2]
678
- p_ind = temp_inp > 0
679
- n_ind = temp_inp < 0
680
- p_sum = np.sum(temp_inp[p_ind])
681
- n_sum = np.sum(temp_inp[n_ind]) * -1
682
- if p_sum > 0:
683
- p_agg_wt = p_sum / (p_sum + n_sum)
684
- else:
685
- p_agg_wt = 0
686
- if n_sum > 0:
687
- n_agg_wt = n_sum / (p_sum + n_sum)
688
- else:
689
- n_agg_wt = 0
690
- if p_sum == 0:
691
- p_sum = 1
692
- if n_sum == 0:
693
- n_sum = 1
694
- wt_ind1[p_ind] += (temp_inp[p_ind] / p_sum) * wt * p_agg_wt
695
- wt_ind1[n_ind] += (temp_inp[n_ind] / n_sum) * wt * n_agg_wt * -1.0
696
- test_wt = test_wt[0 : inp.shape[0], 0 : inp.shape[1], :]
697
- return test_wt
698
-
699
-
700
- def calculate_wt_gavgpool(wts, inp):
701
- channels = wts.shape[0]
702
- wt_mat = np.zeros_like(inp)
703
- for c in range(channels):
704
- wt = wts[c]
705
- temp_wt = wt_mat[..., c]
706
- x = inp[..., c]
707
- p_mat = np.copy(x)
708
- n_mat = np.copy(x)
709
- p_mat[x < 0] = 0
710
- n_mat[x > 0] = 0
711
- p_sum = np.sum(p_mat)
712
- n_sum = np.sum(n_mat) * -1
713
- p_agg_wt = 0.0
714
- n_agg_wt = 0.0
715
- if p_sum + n_sum > 0.0:
716
- p_agg_wt = p_sum / (p_sum + n_sum)
717
- n_agg_wt = n_sum / (p_sum + n_sum)
718
- if p_sum == 0.0:
719
- p_sum = 1.0
720
- if n_sum == 0.0:
721
- n_sum = 1.0
722
- temp_wt = temp_wt + ((p_mat / p_sum) * wt * p_agg_wt)
723
- temp_wt = temp_wt + ((n_mat / n_sum) * wt * n_agg_wt * -1.0)
724
- wt_mat[..., c] = temp_wt
725
- return wt_mat