-
Notifications
You must be signed in to change notification settings - Fork 0
/
information_test.py
131 lines (104 loc) · 3.31 KB
/
information_test.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
import numpy as np
import matplotlib.pyplot as plt
from load_binary_files import training_ims, training_labels
from prob_functions import joint, p_independent
from information_functions import entropy, joint_entropy, mutual_information
from information_functions import mutual_information2
# Select quantity of data to use
N_data_total = len(training_labels)
percentage = 0.1
N_to_use = int(percentage * N_data_total)
# Decide how much data to use
X = training_ims[0:N_to_use]
Y = training_labels[0:N_to_use]
N_hypercolumns = X.shape[1]
units_per_hypercolumn = 2
distribution = {0: 0, 1: 1, 'no': 3}
# Normalize
normalize = True
low_noise = 10e-10
# Coolective
collective = True
N1 = 28 * 1
N2 = N1 + 28 * 3
N1 = 0
N2 = N_hypercolumns
# Single example
i = 0
j = 0
p = p_independent(N_hypercolumns, units_per_hypercolumn, X)
p_i = p[i, :]
p_j = p[j, :]
p_joint = joint(i, j, X, distribution, units_per_hypercolumn)
if normalize:
# Joint
p_joint[p_joint < low_noise] = low_noise
p_joint = p_joint / p_joint.sum()
p_i = p_joint.sum(axis=1)
p_j = p_joint.sum(axis=0)
# Calculate the entropies
x1 = entropy(p_i)
x2 = entropy(p_j)
x3 = joint_entropy(p_joint)
MI = mutual_information(p_i, p_j, p_joint)
MI2 = mutual_information2(p_i, p_j, p_joint)
MI_alt = x1 + x2 - x3
D1 = 1 - MI / x3
D2 = 1 - MI2 / x3
print 'MI', MI
print 'MI2', MI2
print 'MI_alt', MI_alt
print np.isclose(MI, MI2)
print 'distances 1, 2', D1, D2
if collective:
# Values fo the map
d_matrix = np.zeros((N_hypercolumns, N_hypercolumns))
d_matrix2 = np.zeros((N_hypercolumns, N_hypercolumns))
d_matrix3 = np.zeros((N_hypercolumns, N_hypercolumns))
works = np.zeros((N_hypercolumns, N_hypercolumns))
je = np.zeros((N_hypercolumns, N_hypercolumns))
domain = range(N1, N2)
for i in domain:
print 'i', i
for j in domain:
p = p_independent(N_hypercolumns, units_per_hypercolumn, X)
p_i = p[i, :]
p_j = p[j, :]
p_joint = joint(i, j, X, distribution, units_per_hypercolumn)
if np.any(p_joint == 0):
# Joint
p_joint[p_joint < low_noise] = low_noise
p_joint = p_joint / p_joint.sum()
p_i = p_joint.sum(axis=1)
p_j = p_joint.sum(axis=0)
x1 = entropy(p_i)
x2 = entropy(p_j)
x3 = joint_entropy(p_joint)
MI = mutual_information(p_i, p_j, p_joint)
MI2 = mutual_information2(p_i, p_j, p_joint)
D1 = 1 - MI / x3
D2 = 1 - MI2 / x3
D3 = x3 - MI
d_matrix[i, j] = D1
d_matrix2[i, j] = D2
d_matrix3[i, j] = D3
works[i, j] = np.isclose(MI, MI2)
je[i, j] = x3
plt.subplot(1, 3, 1)
plt.imshow(d_matrix[N1:N2, N1:N2], interpolation='nearest')
plt.title('Distance 1')
plt.colorbar()
plt.subplot(1, 3, 2)
plt.imshow(d_matrix2[N1:N2, N1:N2], interpolation='nearest')
plt.colorbar()
plt.title('Distance 2')
plt.subplot(1, 3, 3)
plt.imshow(d_matrix3[N1:N2, N1:N2], interpolation='nearest')
plt.colorbar()
plt.title('Alternative Distance')
plt.show()
# Here we save the data
folder = './data/'
name = 'information_distances'
file_name = folder + name + str(percentage)
np.save(file_name, d_matrix3)