-
Notifications
You must be signed in to change notification settings - Fork 0
/
test3.py
57 lines (44 loc) · 1.15 KB
/
test3.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
import numpy as np
def f(x):
return 2/(1 + np.exp(-x)) - 1
def fd(x):
return 0.5 * (1 + x) * (1 - x)
W1 = np.array([[-0.2, 0.3, -0.4], [0.1, -0.3, 0.4]])
W2 = np.array([0.2, 0.3])
def go_forward(inp):
sum = np.dot(W1, inp)
print(sum.shape)
out = np.array([f(x) for x in sum])
print(out.shape)
sum = np.dot(W2, out)
print(sum.shape)
y = f(sum)
return (y, out)
def train(epoch):
global W1, W2
lmb = 0.01
N = 1
count = len(epoch)
for k in range(N):
x = epoch[np.random.randint(0, count)]
y, out = go_forward((x[0:3]))
e = y - x[-1]
delta = e * fd(y)
print(delta)
W2[0] = W2[0] - lmb * delta * out[0]
W2[1] = W2[1] - lmb * delta * out[1]
delta2 = W2*delta*fd(out)
print(delta2)
W1[0,:] = W1[0,:] - np.array(x[0:3]) * delta2[0] * lmb
W1[1,:] = W1[1,:] - np.array(x[0:3]) * delta2[1] * lmb
epoch =[(-1,-1,-1,-1),
(-1,-1,1,1),
(-1,1,-1,-1),
(-1,1,1,1),
(1,-1,-1,-1),
(1,-1,1,1),
(1,1,-1,-1),
(1,1,1,-1)]
train(epoch)
for x in epoch:
y, out = go_forward(x[0:3])