Last active
May 8, 2019 20:04
-
-
Save tariqul-islam/628dd1cd8747263b9091a800b0eb647e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def compute_di_score(features,Y): | |
''' | |
Computes DI Score of a Neural Network Output | |
For convolution layer, the DI score of each channel is computed | |
For fully connected layer, the DI score of each node is computed | |
Input: | |
features: shape: convolution: Ne,H,W,Nf | |
fully connected: Ne,Nf | |
Ne = number of example | |
Nf = number of channels/filters | |
Y: shape: Ne,n_class | |
One hot encoded array. Each row contains one example. | |
Output: | |
dis: shape: (Nf,), di sore of each of the filters. | |
''' | |
fshape = len(features.shape) | |
#Taking the features to Appropriate Shape | |
if fshape==4: | |
Ne,H,W,Nf = features.shape | |
features = features.transpose(3,0,1,2) #Nf, Ne, H,W | |
features = features.reshape(Nf,Ne,-1) #Nf, Ne, HxW | |
features = features.transpose(0,2,1) # Nf, HxW, Ne | |
d = H*W | |
elif fshape==2: | |
Ne,Nf = features.shape | |
features = features.transpose(1,0) #Nf, Ne | |
features = np.expand_dims(features,axis=1) | |
d = 1 | |
#Normalizing the Y | |
Y = Y.T | |
N_number = np.sqrt(np.array([np.sum(y_one_hot,axis=1)]).T) | |
Y = Y/N_number | |
dis = np.zeros(Nf) | |
rho = 10**-8 | |
for i in range(Nf): | |
X = features[i,:,:] | |
dis[i] = np.trace(np.linalg.inv(X.dot(X.T)+rho*np.eye(d)).dot(X).dot(Y.T).dot(Y).dot(X.T)) | |
return dis |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment