-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhop_proof.py
318 lines (296 loc) · 11.1 KB
/
hop_proof.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
"""
A very verbose information about the next state of the network and basic
theory of the Hopfield network energy.
"""
import io
import sys
import numpy as np
EXAMPLE = np.array([1, -1, -1])
EXAMPLE_WEIGHTS = np.array(
[
[0, 0.25, -0.25, -0.25],
[0.25, 0, -0.25, -0.25],
[-0.25, -0.25, 0, 0.25],
[-0.25, -0.25, 0.25, 0],
]
)
subscript = {}
for i in range(300):
subscript[i + 1] = chr(8320 + i)
# decorator to print equations
def print_eq(func):
def wrapper(self, stored_pattern):
print("Calculating dot product:")
print(self.neurons)
# ensure stored_pattern is a flat array
stored_pattern = np.array(stored_pattern).flatten()
print(stored_pattern)
dot_product = 0
for neuron, pattern in zip(self.neurons, stored_pattern):
product = neuron * pattern
print(f"{neuron} * {pattern} = {product}")
dot_product += product
result = dot_product / self.N
print(f"Dot product of neurons and stored pattern: {dot_product}")
print(f"m = {dot_product} / {self.N} = {result}")
return func(self, stored_pattern)
return wrapper
def part1(neurons=EXAMPLE, weights=EXAMPLE_WEIGHTS):
"""
This function is a verbose explanation of the energy of the Hopfield network.
E = -1/2 * ΣᵢΣⱼ Jᵢⱼ * sᵢ * sⱼ
"""
print()
for i in range(neurons.shape[0]):
if i != 0:
print(" + ", end="")
else:
print("E = {", end="")
for j in range(neurons.shape[0]):
print(
f"e{subscript[i+1]}{subscript[j+1]} = -(1/2)•(J{subscript[i+1]}{subscript[j+1]}•s{subscript[i+1]}•s{subscript[j+1]})",
end=" + " if j != neurons.shape[0] - 1 else "",
)
if i == neurons.shape[0] - 1:
print("}")
else:
print()
def part2(neurons=EXAMPLE, weights=EXAMPLE_WEIGHTS):
"""
This part only arranges the energy equation in a more readable way.
"""
print("\nWe can extract -1/2")
print(" -1/2 * {", end="")
for i in range(neurons.shape[0]):
if i != 0:
print(" ", end="")
print(" + ", end="")
for j in range(neurons.shape[0]):
print(
f"j{subscript[i+1]}{subscript[j+1]}•s{subscript[i+1]}•s{subscript[j+1]}",
end=" + " if j != neurons.shape[0] - 1 else "\n",
)
print("}")
print("The diagonal is 0 so we can write the energy as:")
print(" -1/2 * {", end="")
for i in range(neurons.shape[0]):
if i != 0:
print(" ", end="")
print(" + ", end="")
for j in range(neurons.shape[0]):
if i == j:
print(" 0 ", end=" + ")
else:
print(
f"j{subscript[i+1]}{subscript[j+1]}•s{subscript[i+1]}•s{subscript[j+1]}",
end=" + " if j != neurons.shape[0] - 1 else "\n",
)
print("}")
def part3(neurons=EXAMPLE, weights=EXAMPLE_WEIGHTS):
"""
In this part we substitute the new energy from the energy equation.
"""
print(
"Note that if sᵢ updated, we can subtract the energy of the network to get the energy difference"
)
print("E(t+1) - E(t) = -1/2 * (hᵢ(t+1) - hᵢ(t))")
print(
"lets say noiron 2 is the only one updated. Well mark s(t+1) as s' and s(t) as s"
)
print("{")
for i in range(neurons.shape[0]):
if i != 0:
print(" + ", end="")
else:
print(" ", end="")
for j in range(neurons.shape[0]):
if i == j:
print(" ", end=" ")
else:
si = "s" if i != 1 else "s'"
sj = "s" if j != 1 else "s'"
print(
f"j{subscript[i+1]}{subscript[j+1]}{si}{subscript[i+1]}{sj}{subscript[j+1]} - ",
end="",
)
print(
f"j{subscript[i+1]}{subscript[j+1]}s{subscript[i+1]}s{subscript[j+1]}",
end=" + " if j != neurons.shape[0] - 1 else "\n",
)
print()
print("}")
print(
"If its the only neuron updated, all the other terms that do not contain s2 will be 0: so E(t+1) - E(t) ="
)
print("-1/2 * {")
for i in range(neurons.shape[0]):
if i != 0:
print(" + ", end="")
else:
print(" ", end="")
for j in range(neurons.shape[0]):
if i == j or i != 1 and j != 1:
print(" ", end=" ")
else:
si = "s" if i != 1 else "s'"
sj = "s" if j != 1 else "s'"
print(
f"j{subscript[i+1]}{subscript[j+1]}{si}{subscript[i+1]}{sj}{subscript[j+1]} - ",
end="",
)
print(
f"j{subscript[i+1]}{subscript[j+1]}s{subscript[i+1]}s{subscript[j+1]}",
end=" + " if j != neurons.shape[0] - 1 else "\n",
)
print()
print("}")
print("-1/2 * {")
for i in range(neurons.shape[0]):
print(" ", end="")
if i != 0:
print(" + ", end="")
else:
print(" ", end="")
for j in range(neurons.shape[0]):
if i == j or i != 1 and j != 1:
print(" ", end=" ")
else:
si = "s" if i != 1 else "s'"
sj = "s" if j != 1 else "s'"
print(
f"j{subscript[i+1]}{subscript[j+1]}{si}{subscript[i+1]}{sj}{subscript[j+1]} - ",
end="",
)
print(
f"j{subscript[i+1]}{subscript[j+1]}s{subscript[i+1]}s{subscript[j+1]}",
end=" + " if j != neurons.shape[0] - 1 else "\n",
)
print()
print("}")
def proof_concept(neurons=EXAMPLE, weights=EXAMPLE_WEIGHTS):
"""
This function is a verbose explanation of the energy of the Hopfield network.
It explains one of the basic ideas behind the convergence of the network and
the energy of the network: why the energy of the network is converging to a local minimum.
This assume symmetric weights, and unsynchronized update of the neurons.
"""
neurons = EXAMPLE
weights = EXAMPLE_WEIGHTS
# Create a StringIO object and redirect stdout to it
old_stdout = sys.stdout
sys.stdout = buffer = io.StringIO()
part1(neurons, weights)
part2(neurons, weights)
part3(neurons, weights)
print("We can extract the weights:")
print("-1/2 * {")
for i in range(neurons.shape[0]):
print(" ", end="")
if i != 0:
print(" + ", end="")
else:
print(" ", end="")
for j in range(neurons.shape[0]):
if i == j or i != 1 and j != 1:
print(" ", end=" ")
else:
si = "s" if i != 1 else "s'"
sj = "s" if j != 1 else "s'"
print(
f"j{subscript[i+1]}{subscript[j+1]}({si}{subscript[i+1]}{sj}{subscript[j+1]} - s{subscript[i+1]}s{subscript[j+1]})",
end=" + " if j != neurons.shape[0] - 1 else "\n",
)
print()
print("}")
print("-1/2 * {")
for i in range(neurons.shape[0]):
print(" ", end="")
if i != 0:
print(" + ", end="")
else:
print(" ", end="")
for j in range(neurons.shape[0]):
if i == j or i != 1 and j != 1:
print(" ", end=" ")
else:
si = "s" if i != 1 else "s'"
sj = "s" if j != 1 else "s'"
print(
f"j{subscript[i+1]}{subscript[j+1]}s{subscript[j+1]}(s'{subscript[2]} - s{subscript[2]})",
end=" + " if j != neurons.shape[0] - 1 else "\n",
)
print()
print("}")
print("since the matrix is symmetric, we can write the energy difference as:")
print("{")
for i in range(neurons.shape[0]):
print(" ", end="")
for j in range(neurons.shape[0]):
if i == j or i != 1 and j != 1 or i > j:
print(" ", end=" ")
else:
si = "s" if i != 1 else "s'"
sj = "s" if j != 1 else "s'"
print(
f"j{subscript[i+1]}{subscript[j+1]}s{subscript[j+1]}(s'{subscript[2]} - s{subscript[2]})",
end=" + " if j != neurons.shape[0] - 1 else "\n",
)
print()
print("}")
print("We can extract the weights and the local field of the neuron 2:")
str_weights = ""
for i in range(neurons.shape[0]):
for j in range(neurons.shape[0]):
if i < j and i == 1 or (j == 1 and i == 0):
str_weights += f"j{subscript[i+1]}{subscript[j+1]}s{subscript[j+1]} + "
str_weights = str_weights[:-2]
print(f"[h{subscript[2]}=({str_weights})]*(s'{subscript[2]} - s{subscript[2]})\n\n")
for i in range(neurons.shape[0]):
print("| ", end="")
for j in range(neurons.shape[0]):
print(f"e{subscript[i+1]}{subscript[j+1]} = {weights[i][j]}", end=" | ")
print()
energy = np.zeros((neurons.shape[0], neurons.shape[0]))
for i in range(neurons.shape[0]):
for j in range(neurons.shape[0]):
energy[i][j] = weights[i][j]
print(energy)
# Reset stdout to its original value
sys.stdout = old_stdout
# Get the string value from the StringIO object
output = buffer.getvalue()
return output
def generate_equation(neurons, new_state, weights, t):
"""
Generate the equation for the next state.
sum_{j=1}^{N} Jᵢⱼ * sⱼ(t)
(sometimes markes with hᵢ(t) = \sum_{j=1}^{N} J_{ᵢⱼ} * sⱼ(t))
"""
neurons_str = ", ".join(
[f"s{subscript[i+1]}({t}) = {neurons[i]}" for i in range(len(neurons))]
)
cur_str = f"Current state: S({t}) = {{ {neurons_str} }}\n"
equation = ""
updated_neurons_str = ", ".join(
[f"s{subscript[i+1]}({t+1}) = {new_state[i]}" for i in range(len(new_state))]
)
signs_str = ", ".join(
[
f"s{subscript[i+1]}({t+1}) = sgn(Σ J{subscript[i+1]}{subscript[ord('J')]}•s{subscript[ord('J')]}({t}))"
for i in range(len(neurons))
]
)
equation += f"Updating States... S({t}+1) = {{ {signs_str} }}\n"
neurons_str = ""
for i in range(neurons.shape[0]):
neurons_str += (
f" s{subscript[i+1]}({t+1}) = "
+ f"{' + '.join([f'{weights[i][j]}•{neurons[j]}' for j in range(neurons.shape[0])])}"
)
weights_neurons_sum = sum(
[weights[i][j] * neurons[j] for j in range(neurons.shape[0])]
)
neurons_str += f" = sgn({weights_neurons_sum}) = {new_state[i]}\n"
equation += neurons_str
equation += cur_str + f"Updated state: S({t}+1) = {{ {updated_neurons_str} }}"
print(equation)