-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path5.7.py
60 lines (49 loc) · 1.32 KB
/
5.7.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
import numpy as np
import math
import pandas as pd
import operator
x = np.array([[0,0],[0,1],[1,0],[1,1]])
y = np.array([[0],[1],[1],[0]])
hidden_num = 10
p = np.random.rand(10,1)
output = np.random.rand(1,1)
weight = np.random.rand(hidden_num,1)
belta = np.random.rand(hidden_num,1)
lr = 0.5
c = np.random.rand(hidden_num,2)
step = 0
same_time = 0
old_loss = 0.0
while (1):
step += 1
cur_loss = 0.0
dw = np.zeros([hidden_num,1])
db = np.zeros([hidden_num,1])
for i in range(len(x)):
for j in range(hidden_num):
p[j,0] = math.exp(-belta[j,0] * sum((x[i,:] - c[j,:]) * (x[i,:] - c[j,:])))
output[0,0] = np.dot(p.T, weight)
for j in range(hidden_num):
dw[j,0] += (output[0,0] - y[i,0]) * p[j,0]
db[j,0] -= (output[0,0] - y[i,0]) * weight[j,0] * sum((x[i,:] - c[j,:]) * (x[i,:] - c[j,:])) * p[j,0]
cur_loss += (output[0,0] - y[i]) * (output[0,0] - y[i])
cur_loss = cur_loss / len(x)
weight -= lr * dw
belta -= lr * db
if abs(old_loss - cur_loss) < 0.0001:
same_time += 1
if same_time == 10:
break
else:
old_loss = cur_loss
same_time = 0
print('delta time : %d' % step)
#test
predict = []
for i in range(len(x)):
for j in range(hidden_num):
p[j,0] = math.exp(-belta[j,0] * sum((x[i,:] - c[j,:]) * (x[i,:] - c[j,:])))
predicti = np.dot(p.T, weight)
predict.append(predicti)
print predict
print y.T