-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_bcpnn.py
171 lines (135 loc) · 4.25 KB
/
fast_bcpnn.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
import matplotlib.pyplot as plt
from load_binary_files import training_ims, training_labels
from corrupt_functions import distort_binnary
from copy import deepcopy
# Select quantity of data to use
N_data_total = len(training_labels)
percentage = 0.01
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
directory = './data/'
name_matrix = 'saved_connectivity_matrix.npy'
name_prob = 'saved_connectivity_p.npy'
w = np.load(directory + name_matrix)
p = np.load(directory + name_prob)
log_beta = np.log(p)
# Now let's distort a pattern
percentage = 0.1
index = 1
pattern = deepcopy(X[index])
dis_pattern = distort_binnary(index, percentage, X)
save_dis = deepcopy(dis_pattern)
aux = np.vstack((dis_pattern, dis_pattern))
aux[1, :] = 1 - aux[1, :] # Make the 0 1's
o = aux.T
# We will make o a probability
o = o / np.sum(o, axis=1)[:, np.newaxis]
s = np.zeros(o.shape, dtype=float)
noise = 10e-10
double_bias = np.log(noise)
iterations = 100
to_store = np.zeros((6, N_hypercolumns, iterations))
G = 0.0001
counter_aux_1 = 0
counter_aux_2 = 0
counter = 0
statistics1 = []
statistics2 = []
statistics3 = []
for iter in xrange(iterations):
print 'iter', iter
print '----------------'
for h_index in xrange(N_hypercolumns):
quantity_0 = 0
quantity_1 = 0
for h_out in xrange(N_hypercolumns):
aux_1 = (w[h_index, h_out, 0, 0] * o[h_out, 0] +
w[h_index, h_out, 0, 1] * o[h_out, 1])
aux_2 = (w[h_index, h_out, 1, 0] * o[h_out, 0] +
w[h_index, h_out, 1, 1] * o[h_out, 1])
if (aux_1 > 0):
quantity_0 += np.log(aux_1)
counter_aux_1 = 0
else:
quantity_0 += double_bias
counter_aux_1 = 1
if (aux_2 > 0):
quantity_1 += np.log(aux_2)
counter_aux_2 = 0
else:
quantity_1 += double_bias
counter_aux_2 = 1
counter += counter_aux_1 * counter_aux_2
if False:
print '1 log , quant', log_beta[h_index, 0], quantity_0
print '2 log, quant', log_beta[h_index, 1], quantity_1
x_1 = log_beta[h_index, 0] + quantity_0
x_2 = log_beta[h_index, 1] + quantity_1
statistics1.append(quantity_0)
statistics1.append(quantity_1)
s[h_index, 0] = x_1
s[h_index, 1] = x_2
statistics2.append(x_1)
statistics2.append(x_2)
statistics3.append(np.abs(x_2 - x_1))
plt.plot(s)
plt.savefig('./figures/figure' + str(iter) + '.png')
plt.hold(False)
o = np.exp(G * s)
to_store[0, :, iter] = o[:, 0]
to_store[1, :, iter] = o[:, 1]
to_store[4, :, iter] = o[:, 0]
to_store[5, :, iter] = o[:, 1]
o = o / np.sum(o, axis=1)[:, np.newaxis]
to_store[2, :, iter] = o[:, 0]
to_store[3, :, iter] = o[:, 1]
# Plot
if iterations * 2 < N_hypercolumns:
plot_to_N = iterations * 2
else:
plot_to_N = N_hypercolumns
plt.subplot(3, 2, 1)
plt.imshow(to_store[0, 0:plot_to_N, ...])
plt.title('0 before normalization and noise')
plt.colorbar()
plt.subplot(3, 2, 2)
plt.imshow(to_store[1, 0:plot_to_N, ...])
plt.title('1 before normalization and noise')
plt.colorbar()
plt.subplot(3, 2, 3)
plt.imshow(to_store[2, 0:plot_to_N, ...])
plt.title('0 after normalization and noise')
c = plt.colorbar()
c.set_clim(0, 1)
plt.subplot(3, 2, 4)
plt.imshow(to_store[3, 0:plot_to_N, ...])
plt.title('1 after normalization and noise')
c = plt.colorbar()
c.set_clim(0, 1)
plt.subplot(3, 2, 5)
plt.imshow(to_store[4, 0:plot_to_N, ...])
plt.title('0 after noise')
plt.colorbar()
plt.subplot(3, 2, 6)
plt.imshow(to_store[5, 0:plot_to_N, ...])
plt.title('1 after noise')
plt.colorbar()
plt.show()
output = 1 - np.argmax(o, axis=1)
output = 1 - np.argmax(o, axis=1)
plt.subplot(1, 3, 1)
plt.imshow(pattern.reshape((28, 28)))
plt.title('Original')
plt.subplot(1, 3, 2)
plt.imshow(output.reshape((28, 28)))
plt.title('Output')
plt.subplot(1, 3, 3)
plt.imshow(save_dis.reshape((28, 28)))
plt.title('Distorted')
plt.show()
print 'The thing did move?', np.sum(save_dis - output)