-
Notifications
You must be signed in to change notification settings - Fork 0
/
informationContent.py
executable file
·46 lines (33 loc) · 1.64 KB
/
informationContent.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
# Copyright (C) 2021 by RWTH Aachen University #
# License: #
# #
# This software is dual-licensed under: #
# • Commercial license (please contact: lfb@lfb.rwth-aachen.de) #
# • AGPL (GNU Affero General Public License) open source license #
################################################################################
# Author: Leon Weninger #
################################################################################
################################################################################
import numpy as np
from sklearn.neighbors import KernelDensity
def fit_ic(activations):
models = []
for i in range(activations.shape[0]):
ts = activations[i, :]
#%% calc bandwidth: Silverman, B.W.(1986).Density Estimation for Statistics and
# Data Analysis. London: Chapman & Hall/CRC. p4. ISBN 978-0-412-24620-3.
n = ts.shape[0]
std = ts.std()
h = 1.06*std*np.power(n,-1/5)
model = KernelDensity(bandwidth=h, kernel='gaussian')
model.fit(ts.reshape(-1, 1))
models.append(model)
return tuple(models)
def predict_ic(activations, modelz):
ic = np.zeros(activations.shape)
for i in range(activations.shape[0]):
model = modelz[i]
ts = activations[i, :]
log_p = model.score_samples(ts.reshape(-1, 1))
ic[i, :] = -log_p
return ic