-
Notifications
You must be signed in to change notification settings - Fork 0
/
ans.py
205 lines (176 loc) · 7.07 KB
/
ans.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
# ans.py
# Autoencoder node saliency
#
# Input:
# A - Activation values
# L - Classification labels, 0 or 1
#
# Output:
# NEDAll - NED curve
# NED0All - NED0 curve
# NED1All - NED1 curve
# sns_incr - SNS with increasing probability distribution
# sns_bi - SNS with binary distribution
# g0Count - Histogram counts for label 0
# g1Count - Histogram counts for label 1
import numpy as np
def ans(A,L,numBins=10):
[n, numNodes] = A.shape
#numBins = 20
NEDAll = np.zeros((numNodes))
NED0All = np.zeros((numNodes))
NED1All = np.zeros((numNodes))
sns_incr = np.zeros((numNodes))
sns_bi = np.zeros((numNodes))
histCount = np.zeros((numNodes,numBins))
g1Count = np.zeros((numNodes,numBins))
g0Count = np.zeros((numNodes,numBins))
label1 = 1
label0 = 0
g1 = [i for [i,val] in enumerate(L) if val==label1]
g0 = [i for [i,val] in enumerate(L) if val==label0]
g1Total = len(g1)
g0Total = len(g0)
#nodeIter = 1
for nodeIter in range(numNodes):
#for nodeIter in range(1):
# Histogram
binRange = float(1.0/numBins)
binMids = np.zeros((numBins))
binMids[0] = binRange/2.0
for iter in range(1,numBins):
binMids[iter] = binRange*(iter+1) - binMids[0]
#print binMids
#sumBinMids = np.sum(binMids)
#print sumBinMids
for iter1 in range(n):
for iter2 in range(numBins):
if iter2 == 0 and A[iter1,nodeIter] >= binRange*iter2 and A[iter1,nodeIter] <= binRange*(iter2+1):
histCount[nodeIter,iter2] = histCount[nodeIter,iter2] + 1.0
#if abs(L[iter1]-label1) < 0.001:
if L[iter1]==label1:
g1Count[nodeIter,iter2] = g1Count[nodeIter,iter2] + 1.0
#if abs(L[iter1]-label0) < 0.001:
if L[iter1]==label0:
g0Count[nodeIter,iter2] = g0Count[nodeIter,iter2] + 1.0
elif A[iter1,nodeIter] > binRange*iter2 and A[iter1,nodeIter] <= binRange*(iter2+1):
histCount[nodeIter,iter2] = histCount[nodeIter,iter2] + 1.0
#if abs(L[iter1]-label1) < 0.001:
if L[iter1]==label1:
g1Count[nodeIter,iter2] = g1Count[nodeIter,iter2] + 1.0
#if abs(L[iter1]-label0) < 0.001:
if L[iter1]==label0:
g0Count[nodeIter,iter2] = g0Count[nodeIter,iter2] + 1.0
#print histCount
#print g1Count[nodeIter,]
#print g0Count[nodeIter,]
#----------------------------------
# Normalized entropy difference
#----------------------------------
entropy = 0.0
numOccupiedBins = 0.0
for iter2 in range(numBins):
currentP = float(histCount[nodeIter,iter2]/n)
if currentP != 0.0:
numOccupiedBins = numOccupiedBins + 1.0
temp = - currentP*np.log2(currentP)
entropy = entropy + temp
if numOccupiedBins > 1:
NED = 1.0 - entropy/np.log2(numOccupiedBins)
else:
NED = 1.0
#------------------------------------
# Class NED
#------------------------------------
entropy0 = 0.0
numOccupiedBins0 = 0.0
for iter2 in range(numBins):
currentP0 = float(g0Count[nodeIter,iter2]/g0Total)
if currentP0 != 0.0:
numOccupiedBins0 = numOccupiedBins0 + 1.0
temp = - currentP0*np.log2(currentP0)
entropy0 = entropy0 + temp
if numOccupiedBins0 > 1:
NED0 = 1.0 - entropy0/np.log2(numOccupiedBins0)
else:
NED0 = 1.0
entropy1 = 0.0
numOccupiedBins1 = 0.0
for iter2 in range(numBins):
currentP1 = float(g1Count[nodeIter,iter2]/g1Total)
if currentP1 != 0.0:
numOccupiedBins1 = numOccupiedBins1 + 1.0
temp = - currentP1*np.log2(currentP1)
entropy1 = entropy1 + temp
if numOccupiedBins1 > 1:
NED1 = 1.0 - entropy1/np.log2(numOccupiedBins1)
else:
NED1 = 1.0
'''
#-----------------------------------
# Cross entropy
#-----------------------------------
ce1 = 0.0
ce2 = 0.0
currentP = np.zeros((numBins,1))
for iter2 in range(numBins):
groupTotal = g1Count[nodeIter,iter2]+g0Count[nodeIter,iter2]
currentP = float(histCount[nodeIter,iter2]/n)
if currentP != 0.0:
if g1Count[nodeIter,iter2] != 0:
currentQ1 = float(g1Count[nodeIter,iter2]/groupTotal)
ce1 = ce1 - currentP*(currentQ1*np.log2(binMids[iter2])-(1.0-currentQ1)*np.log2(1.0-binMids[iter2]))
if g0Count[nodeIter,iter2] != 0:
currentQ2 = float(g0Count[nodeIter,iter2]/groupTotal)
ce2 = ce2 - currentP*(currentQ2*np.log2(binMids[iter2])-(1.0-currentQ2)*np.log2(1.0-binMids[iter2]))
'''
#-----------------------------------
# Cross entropy - increasing
#-----------------------------------
ce1 = 0.0
ce2 = 0.0
currentP = np.zeros((numBins,1))
for iter2 in range(numBins):
groupTotal = g1Count[nodeIter,iter2]+g0Count[nodeIter,iter2]
currentP = float(histCount[nodeIter,iter2]/n)
if g1Count[nodeIter,iter2] != 0:
currentQ1 = float(g1Count[nodeIter,iter2]/groupTotal)
if 1-currentQ1 != 0:
ce1 = ce1 - currentP*(binMids[iter2]*np.log2(currentQ1)- (1-binMids[iter2])*np.log2(1-currentQ1))
if g0Count[nodeIter,iter2] != 0:
currentQ2 = float(g0Count[nodeIter,iter2]/groupTotal)
if 1-currentQ2 != 0:
ce2 = ce2 - currentP*(binMids[iter2]*np.log2(currentQ2)- (1-binMids[iter2])*np.log2(1-currentQ2))
#-----------------------------------
# Cross entropy - binary
#-----------------------------------
ceo1 = 0.0
ceo2 = 0.0
currentP = np.zeros((numBins,1))
for iter2 in range(numBins):
groupTotal = g1Count[nodeIter,iter2]+g0Count[nodeIter,iter2]
currentP = float(histCount[nodeIter,iter2]/n)
if g1Count[nodeIter,iter2] != 0:
currentQ1 = float(g1Count[nodeIter,iter2]/groupTotal)
if 1-currentQ1 != 0:
#ceo1 = ceo1 - currentP*(binMids[iter2]*np.log2(currentQ1)- (1-binMids[iter2])*np.log2(1-currentQ1))
if iter2 > numBins/2:
ceo1 = ceo1 - currentP*(np.log2(currentQ1))
else:
ceo1 = ceo1 - currentP*(np.log2(1-currentQ1))
if g0Count[nodeIter,iter2] != 0:
currentQ2 = float(g0Count[nodeIter,iter2]/groupTotal)
if 1-currentQ2 != 0:
#ceo2 = ceo2 - currentP*(binMids[iter2]*np.log2(currentQ2)- (1-binMids[iter2])*np.log2(1-currentQ2))
if iter2 > numBins/2:
ceo2 = ceo2 - currentP*(np.log2(currentQ2))
else:
ceo2 = ceo2 - currentP*(np.log2(1-currentQ2))
NEDAll[nodeIter] = NED
NED0All[nodeIter] = NED0
NED1All[nodeIter] = NED1
# Supervised node saliency - increasing probability
sns_incr[nodeIter] = min([ce1,ce2])
# Supervised node saliency - binary
sns_bi[nodeIter] = min([ceo1,ceo2])
return NEDAll, NED0All, NED1All, sns_incr, sns_bi, g0Count, g1Count