-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path5.5_bp.py
72 lines (64 loc) · 2.25 KB
/
5.5_bp.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
import numpy as np
import math
import pandas as pd
import operator
df = pd.read_csv('watermelon_5_3.csv')
data = df.values[:, 1:-1].tolist()
labels = df.values[:,-1].tolist()
# print data
# print labels
input_layer = np.array([[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0]])
hidden_layer = np.array([[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0],[0.0]])
output_layer = np.array([[0.0]])
hidden_weight = np.random.rand(len(input_layer), len(hidden_layer))
# print hidden_weight.shape
hidden_threshold = np.random.rand(len(hidden_layer),1)
output_weight = np.random.rand(len(hidden_layer), len(output_layer))
output_threshold = np.random.rand(len(output_layer),1)
old_loss = 0.0
step = 0
same_time = 0
lr = 1
while (1):
cur_loss = 0.0
for column in range(len(data)):
for i in range(len(input_layer)):
input_layer[i][0] = data[column][i]
alpha = np.dot(hidden_weight.T, input_layer) - hidden_threshold
for i in range(len(hidden_layer)):
hidden_layer[i] = 1 / (1 + math.exp(-alpha[i]))
belta = np.dot(output_weight.T, hidden_layer) - output_threshold
output_layer = 1 / (1 + math.exp(-belta))
cur_loss += (labels[column] - output_layer) * (labels[column] - output_layer) / 2.0
g = output_layer * (1 - output_layer) * (labels[column] - output_layer)
delta_output_weight = lr * np.dot(g, hidden_layer)
delta_output_threshold = -lr * g
output_weight += delta_output_weight
output_threshold += delta_output_threshold
e = hidden_layer * (1 - hidden_layer) * np.dot(g, output_weight)
delta_hidden_weight = lr * np.dot(input_layer, e.T)
delta_hidden_threshold = -lr * e
hidden_weight += delta_hidden_weight
hidden_threshold += delta_hidden_threshold
if abs(cur_loss - old_loss) < 0.0001:
same_time += 1
if same_time == 100:
break
else:
old_loss = cur_loss
same_time = 0
step += 1
print('delta time : %d' % step)
#test
predict = []
for column in range(len(data)):
for i in range(len(input_layer)):
input_layer[i][0] = data[column][i]
alpha = np.dot(hidden_weight.T, input_layer) - hidden_threshold
for i in range(len(hidden_layer)):
hidden_layer[i] = 1 / (1 + math.exp(-alpha[i]))
belta = np.dot(output_weight.T, hidden_layer) - output_threshold
predicti = 1 / (1 + math.exp(-belta))
predict.append(predicti)
print predict
print labels