-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnetwork.py
More file actions
103 lines (85 loc) · 3.12 KB
/
network.py
File metadata and controls
103 lines (85 loc) · 3.12 KB
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
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm
from tqdm import tqdm
class HopfieldNetwork(object):
def train_weights(self, train_data):
print("Start to train weights...")
num_data = len(train_data)
self.num_neuron = train_data[0].shape[0]
# initialize weights
W = np.zeros((self.num_neuron, self.num_neuron))
rho = np.sum([np.sum(t) for t in train_data]) / (num_data*self.num_neuron)
# Hebb rule
for i in tqdm(range(num_data)):
t = train_data[i] - rho
W += np.outer(t, t)
# Make diagonal element of W into 0
diagW = np.diag(np.diag(W))
W = W - diagW
W /= num_data
self.W = W
def predict(self, data, num_iter=20, threshold=0, asyn=False):
print("Start to predict...")
self.num_iter = num_iter
self.threshold = threshold
self.asyn = asyn
# Copy to avoid call by reference
copied_data = np.copy(data)
# Define predict list
predicted = []
for i in tqdm(range(len(data))):
predicted.append(self._run(copied_data[i]))
return predicted
def _run(self, init_s):
if self.asyn==False:
"""
Synchronous update
"""
# Compute initial state energy
s = init_s
e = self.energy(s)
# Iteration
for i in range(self.num_iter):
# Update s
s = np.sign(self.W @ s - self.threshold)
# Compute new state energy
e_new = self.energy(s)
# s is converged
if e == e_new:
return s
# Update energy
e = e_new
return s
else:
"""
Asynchronous update
"""
# Compute initial state energy
s = init_s
e = self.energy(s)
# Iteration
for i in range(self.num_iter):
for j in range(100):
# Select random neuron
idx = np.random.randint(0, self.num_neuron)
# Update s
s[idx] = np.sign(self.W[idx].T @ s - self.threshold)
# Compute new state energy
e_new = self.energy(s)
# s is converged
if e == e_new:
return s
# Update energy
e = e_new
return s
def energy(self, s):
return -0.5 * s @ self.W @ s + np.sum(s * self.threshold)
def plot_weights(self, path):
plt.figure(figsize=(6, 5))
w_mat = plt.imshow(self.W, cmap=cm.coolwarm)
plt.colorbar(w_mat)
plt.title("Network Weights")
plt.tight_layout()
plt.savefig(path+"/weights.png")
plt.show()