-
Notifications
You must be signed in to change notification settings - Fork 0
/
TNT.py
executable file
·171 lines (139 loc) · 4.67 KB
/
TNT.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
import numpy as np
from numpy.linalg import norm
def check_result(ternary, target):
'''
Calculating the cosine similarity between ternary and target
==============
Parameters
ternary: a numpy array and its elements are -1, 0 or 1
target: a numpy array whose elements are floating numbers.
Output
return the cosine similarity
'''
if norm(ternary, ord=2) == 0 or norm(target, ord=2) == 0:
return 0
else:
return np.dot(ternary, target) / (norm(ternary, ord=2) * norm(target, ord=2))
def order_vector(target_vector, num):
'''
Ordering a list whose elements are -1, 0, 1 follows
the order of target_vector's.
-----------
Parameter
===========
binary_vector: all elements are -1, 0, 1
target_vector: the targeterved floating type list
num: a int, indicating how many -1 and 1 the binary_vector should have
-----------
Return
===========
binary_vector: whose elements are all -1, 0 or 1 and have
the same ordering with target_vector.
'''
# sort the target_vector in a decreasing order,
# and return the index of each
# elements after sorting
binary_vector = signalization(target_vector)
x_sorted_index = np.argsort(np.abs(target_vector))[::-1]
for elem in x_sorted_index[(num + 2)::]:
binary_vector[elem] = 0
return binary_vector
def normalize_rows(x):
"""
function that normalizes each row of the matrix x to have unit length.
Args:
``x``: A numpy matrix of shape (n, m)
Returns:
``x``: The normalized (by row) numpy matrix.
"""
return x / norm(x, ord=2, keepdims=True)
def similar_cos(target_vector):
# norm_scalar = norm(target_vector)
# target_l2 = target_vector / norm_scalar
target_hat = normalize_rows(target_vector)
target_hat_sorted = sorted(np.abs(target_hat), reverse=True)
similar_value = []
temp = target_hat_sorted[0]
for i in range(1, len(target_hat_sorted)):
temp += target_hat_sorted[i]
similar_value.append(temp / np.sqrt(i + 1))
return similar_value, max(similar_value), np.argmax(similar_value)
def signalization(target_vector):
'''
Parameters
============
target_vector: the vector that will be converted to a ternary vector.
------------
Return
============
binary_vector: all elements convert to -1 or 1
'''
binary_vector = target_vector.copy()
binary_vector[binary_vector < 0] = -1
binary_vector[binary_vector >= 0] = 1
return binary_vector
def TNT_convert(weights, name=False):
'''
ternary_T_filter: the cluster results of the weights of a filter.
n_clusters_: The number of clusters, in Ternary it should be 3.
'''
kernel_flatten = weights.flatten()
# print(np.shape(kernel_flatten))
# binary_type = signalization(kernel_flatten)
similarValue_conTerv, maxValue_conv, maxInde_conv = similar_cos(kernel_flatten)
restultVector_conv = order_vector(kernel_flatten, maxInde_conv)
# result_conv = check_result(restultVector_conv, kernel_flatten)
'''
if name:
print('[INFO] the cosine similarity is {}'.
format(result_conv))
else:
print('[INFO] the similarity of the layer {} is {}'
.format(name, result_conv))
'''
ternary_weights = np.array(restultVector_conv).reshape(weights.shape)
return ternary_weights
def inner(a_, t_):
return np.dot(a_, t_.reshape(-1, 1)) / (norm(a_) * (norm(t_) + 0.00001))
def scaling(weights_, ternary_):
a = weights_.flatten()
t = ternary_.flatten()
ap = a.copy()
an = a.copy()
tp = t.copy()
tn = t.copy()
ap[a < 0.] = 0.
an[a > 0.] = 0.
tp[t < 0.] = 0.
tn[t > 0.] = 0.
rp = 0
rn = 0
if sum(ap) == 0:
rp = 0
else:
rp = (norm(ap) / (norm(tp) + 0.00001)) * inner(ap, tp)
if sum(an) == 0:
rn = 0
else:
rn = (norm(an) / (norm(tn) + 0.00001)) * inner(an, tn)
t_result = tp * rp + tn * rn
return t_result.reshape(weights_.shape)
def kernels_cluster(weights_):
if weights_.ndim == 4:
r, c, in_sample, out_sample = weights_.shape
kernels = np.zeros(weights_.shape)
for i in range(out_sample):
for j in range(in_sample):
xi = weights_[:, :, j, i]
temp_T = TNT_convert(xi)
t_ = scaling(xi, temp_T)
kernels[:, :, j, i] = t_
return kernels
elif weights_.ndim == 2:
temp_T = TNT_convert(weights_)
t_ = scaling(weights_, temp_T)
return t_
elif weights_.ndim == 1:
temp_T = TNT_convert(weights_)
t_ = scaling(weights_, temp_T)
return t_