-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_network.py
137 lines (126 loc) · 4.21 KB
/
neural_network.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def neuron():
# inputs: x x_reverse
# output: y y_reverse
# weights: w w_reverse
# intermediates: t1, t2 t1_reverse, t2_reverse
# forward sweep
for i in range(len(x)):
t1[i] = w[i]*x[i]
t2 = 0
for i in range(len(t1)):
t2 = t2+t1[i]
y = sigmoid(t2)
# initialize reverse variables
y_reverse = 0
t2_reverse = 0
for i in range(len(t1)):
t1_reverse[i] = 0
for i in range(len(w)):
w_reverse[i] = 0
for i in range(len(x)):
x_reverse[i] = 0
# reverse sweep
y_reverse += 1
t2_reverse += y_reverse*sigmoid_derivative(t2)
for i in range(len(t1)):
t1_reverse[i] += t2_reverse
for i in range(len(w)):
w_reverse[i] += t1_reverse[i]*x[i]
for i in range(len(x)):
x_reverse[i] += t1_reverse[i]*w[i]
def slp():
# inputs: x x_reverse
# output: y y_reverse
# weights: w w_reverse
# intermediates: t1, t2 t1_reverse, t2_reverse
# forward sweep
for j in range(len(y)):
for i in range(len(x)):
t1[j, i] = w[j, i]*x[i]
for j in range(len(y)):
t2[j] = 0
for j in range(len(y)):
for i in range(len(t1[j])):
t2[j] = t2[j]+t1[j, i]
for j in range(len(y)):
y[j] = sigmoid(t2[j])
# initialize reverse variables
for j in range(len(y)):
y_reverse[j] = 0
for j in range(len(y)):
t2_reverse[j] = 0
for j in range(len(y)):
for i in range(len(t1[j])):
t1_reverse[j, i] = 0
for j in range(len(y)):
for i in range(len(w[j])):
w_reverse[j, i] = 0
for i in range(len(x)):
x_reverse[i] = 0
# reverse sweep
for j in range(len(y)):
y_reverse[j] += 1
for j in range(len(y)):
t2_reverse[j] += y_reverse[j]*sigmoid_derivative(t2[j])
for j in range(len(y)):
for i in range(len(t1[j])):
t1_reverse[j, i] += t2_reverse[j]
for j in range(len(y)):
for i in range(len(w[j])):
w_reverse[j, i] += t1_reverse[j, i]*x[i]
for j in range(len(y)):
for i in range(len(x)):
x_reverse[i] += t1_reverse[j, i]*w[j, i]
def mlp():
# inputs: x x_reverse
# output: y y_reverse
# weights: w w_reverse
# intermediates: t1, t2 t1_reverse, t2_reverse
# forward sweep
for k in range(layers):
if k>0:
for j in range(len(y[k])):
x[k, j] = y[k-1, j]
for j in range(len(y[k])):
for i in range(len(x[k])):
t1[k, j, i] = w[k, j, i]*x[k, i]
for j in range(len(y[k])):
t2[k, j] = 0
for j in range(len(y[k])):
for i in range(len(t1[k, j])):
t2[k, j] = t2[k, j]+t1[k, j, i]
for j in range(len(y[k])):
y[k, j] = sigmoid(t2[k, j])
# initialize reverse variables
for k in range(layers-1, -1, -1):
for j in range(len(y[k])):
y_reverse[k, j] = 0
for j in range(len(y[k])):
t2_reverse[k, j] = 0
for j in range(len(y[k])):
for i in range(len(t1[k, j])):
t1_reverse[k, j, i] = 0
for j in range(len(y[k])):
for i in range(len(w[k, j])):
w_reverse[k, j, i] = 0
for i in range(len(x[k])):
x_reverse[k, i] = 0
# reverse sweep
for k in range(layers-1, -1, -1):
if k<layers-1:
for j in range(len(y[k])):
y_reverse[k, j] += x_reverse[k+1, j]
else:
for j in range(len(y[k])):
y_reverse[k, j] += 1
for j in range(len(y[k])):
t2_reverse[k, j] += y_reverse[k, j]*sigmoid_derivative(t2[k, j])
for j in range(len(y[k])):
for i in range(len(t1[k, j])):
t1_reverse[k, j, i] += t2_reverse[k, j]
for j in range(len(y[k])):
for i in range(len(w[k, j])):
w_reverse[k, j, i] += t1_reverse[k, j, i]*x[k, i]
for j in range(len(y[k])):
for i in range(len(x[k])):
x_reverse[k, i] += t1_reverse[k, j, i]*w[k, j, i]