-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelta-learning-algorithm.py
79 lines (73 loc) · 2.51 KB
/
delta-learning-algorithm.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
# https://en.wikipedia.org/wiki/Delta_rule
import numpy as np
from tabulate import tabulate
# initial weights
w = [1, 0, 0]
# learning rate
n = 1
# iterations
iterations = 12
# dataset
X = [[1, 0, 2],
[1, 1, 2],
[1, 2, 1],
[1, -3, 1],
[1, -2, -1],
[1, -3, -2]]
# classes
t = [1, 1, 1, 0, 0, 0]
# https://en.wikipedia.org/wiki/Heaviside_step_function
def transfer_function(w, x):
wx = np.dot(w, x)
if (wx > 0):
return 1
elif (wx == 0):
return 0.5
else:
return 0
assert (transfer_function([0.1, -0.5, 0.4], [0.1, -0.5, 0.4]) == 1)
assert (transfer_function([0.1, -0.5, 0.4], [0.1, 0.5, 0.4]) == 0)
assert (transfer_function([0, 0, 0], [0, 0, 0]) == 0.5)
# sequential delta learning algorithm
result = []
for o in range(int(iterations / len(X))):
for i in range(len(X)):
if ((i + 1 + (len(X) * o)) > iterations): break
w_prev = w
x = X[i]
y = transfer_function(w, x)
# calculate update part
update = np.zeros(len(x))
for j in range(len(x)): update[j] = n * (t[i] - y) * x[j]
# add update part to w
w = np.add(w, update)
# append to result
result.append([
# iteration
i + 1 + (len(X) * o),
# w
list(w_prev),
# x
x,
# y = H(wx)
y,
# t
t[i],
# w_new
list(w)]
)
print(tabulate(result, headers=['i', 'w', 'x', 'y = H(wx)', 't', 'w_new'], tablefmt="simple"))
# i w x y = H(wx) t w_new
# --- ---------------- ----------- ----------- --- ----------------
# 1 [1, 0, 0] [1, 0, 2] 1 1 [1.0, 0.0, 0.0]
# 2 [1.0, 0.0, 0.0] [1, 1, 2] 1 1 [1.0, 0.0, 0.0]
# 3 [1.0, 0.0, 0.0] [1, 2, 1] 1 1 [1.0, 0.0, 0.0]
# 4 [1.0, 0.0, 0.0] [1, -3, 1] 1 0 [0.0, 3.0, -1.0]
# 5 [0.0, 3.0, -1.0] [1, -2, -1] 0 0 [0.0, 3.0, -1.0]
# 6 [0.0, 3.0, -1.0] [1, -3, -2] 0 0 [0.0, 3.0, -1.0]
# 7 [0.0, 3.0, -1.0] [1, 0, 2] 0 1 [1.0, 3.0, 1.0]
# 8 [1.0, 3.0, 1.0] [1, 1, 2] 1 1 [1.0, 3.0, 1.0]
# 9 [1.0, 3.0, 1.0] [1, 2, 1] 1 1 [1.0, 3.0, 1.0]
# 10 [1.0, 3.0, 1.0] [1, -3, 1] 0 0 [1.0, 3.0, 1.0]
# 11 [1.0, 3.0, 1.0] [1, -2, -1] 0 0 [1.0, 3.0, 1.0]
# 12 [1.0, 3.0, 1.0] [1, -3, -2] 0 0 [1.0, 3.0, 1.0]