-
Notifications
You must be signed in to change notification settings - Fork 81
/
net-hopfield.py
44 lines (38 loc) · 1.09 KB
/
net-hopfield.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
import pycxsimulator
from pylab import *
import networkx as nx
memorized_letters = [
# Letter 'C'
[-1, 1, 1, 1,
1, -1, -1, -1,
1, -1, -1, -1,
-1, 1, 1, 1],
# Letter 'P'
[1, 1, 1, -1,
1, -1, -1, 1,
1, 1, 1, -1,
1, -1, -1, -1]
]
def initialize():
global g
g = nx.complete_graph(4 * 4)
g.pos = {}
for x in range(4):
for y in range(4):
g.pos[y * 4 + x] = (x, -y)
for i, j in g.edges:
g.edges[i, j]['weight'] = sum([letter[i] * letter[j] for letter in memorized_letters])
for i in g.nodes:
g.nodes[i]['state'] = choice([-1, 1])
def observe():
global g
cla()
nx.draw(g, pos = g.pos, cmap = cm.winter, vmin = -1, vmax = 1,
node_color = [g.nodes[i]['state'] for i in g.nodes])
axis('image')
def update():
global g
i = choice(list(g.nodes))
s = sum([g.edges[i, j]['weight'] * g.nodes[j]['state'] for j in g.neighbors(i)])
g.nodes[i]['state'] = 1 if s > 0 else -1 if s < 0 else g.nodes[i]['state']
pycxsimulator.GUI().start(func=[initialize, observe, update])