-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_bcpnn_01.py
170 lines (128 loc) · 3.96 KB
/
fast_bcpnn_01.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
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
# Load the data
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 = 1
index = 10
pattern = deepcopy(X[index])
dis_pattern = distort_binnary(index, percentage, X)
save_dis = deepcopy(dis_pattern)
aux = np.vstack((dis_pattern, dis_pattern))
aux = np.vstack((pattern, pattern))
aux[1, :] = 1 - aux[1, :] # Make the 0 1's
o = aux.T
o = np.random.rand(784, 2)
save_dis = np.argmax(o, axis=1)
# We will make 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 = 10
to_store = np.zeros((6, N_hypercolumns, iterations))
G = 5.0
plot = True
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])
x_1 = log_beta[h_index, 0] + np.log(aux_1)
x_2 = log_beta[h_index, 1] + np.log(aux_2)
s[h_index, 0] = x_1
s[h_index, 1] = x_2
statistics1.append(x_1)
statistics1.append(x_1)
statistics2.append(np.abs(x_2 - x_1))
o = np.exp(G * s)
plt.subplot(1, 3, 1)
plt.plot(s)
plt.ylim([-10, 1])
plt.subplot(1, 3, 2)
plt.plot(o)
plt.ylim([-0.1, 1.1])
to_store[0, :, iter] = o[:, 0]
to_store[1, :, iter] = o[:, 1]
to_store[4, :, iter] = o[:, 0]
to_store[5, :, iter] = o[:, 1]
denominator = np.sum(o, axis=1)
statistics3.append(denominator)
o = o / denominator[:, np.newaxis]
plt.subplot(1, 3, 3)
plt.plot(o)
to_store[2, :, iter] = o[:, 0]
to_store[3, :, iter] = o[:, 1]
plt.savefig('./figures/figure' + str(iter) + '.png')
plt.hold(False)
# Plot
if iterations * 2 < N_hypercolumns:
plot_to_N = iterations * 2
else:
plot_to_N = N_hypercolumns
output = 1 - np.argmax(o, axis=1)
if plot:
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()
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)