-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackPropagation.py
184 lines (139 loc) · 6.54 KB
/
BackPropagation.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import numpy as np
import random
import matplotlib.pyplot as plt
from ANN_Project_Assets import Loading_Datasets as ld
train_set = ld.load_and_get_set(test_or_train="train",
feature_file_path="ANN_Project_Assets/Datasets/train_set_features.pkl",
label_file_path="ANN_Project_Assets/Datasets/train_set_labels.pkl")
layer_sizes = [len(train_set[0][0]), 150, 60, 4] # [102, 150, 60, 4]
# There are 4 layers, between each two consecutive layers, there needs to be a weight matrix
W = [
np.random.normal(size=(layer_sizes[1], layer_sizes[0])), # weights between layer 0 and 1, aka W[0]
np.random.normal(size=(layer_sizes[2], layer_sizes[1])), # weights between layer 1 and 2, aka W[1]
np.random.normal(size=(layer_sizes[3], layer_sizes[2])) # weights between layer 2 and 3, aka W[2]
]
# Initialize bias to 0, for every layer.
B = [
np.zeros((layer_sizes[1], 1)), # bias vector between layer 0 and 1, aka B[0]
np.zeros((layer_sizes[2], 1)), # bias vector between layer 1 and 2, aka B[1]
np.zeros((layer_sizes[3], 1)), # bias vector between layer 2 and 3, aka B[2]
]
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def check_accuracy(calculated_labels, correct_labels):
calculated_ans = np.where(calculated_labels == np.amax(calculated_labels))
correct_ans = np.where(correct_labels == np.amax(correct_labels))
return calculated_ans == correct_ans
def run_feed_forward():
data_size = 200 # number of train set elements taken from the train set
correct_ans_count = 0 # number of correct answers, initialized at 0
for td in train_set[:data_size]:
z = [
np.zeros((layer_sizes[0], 1)),
np.zeros((layer_sizes[1], 1)),
np.zeros((layer_sizes[2], 1)),
np.zeros((layer_sizes[3], 1))
]
# values of the first layer (0th), initialized as the train data
z[0] = td[0]
np.reshape(z[0], (102, 1))
for i in range(1, 4):
# for each next layer, z is calculated as discussed below
z[i] = sigmoid(W[i - 1] @ z[i - 1] + B[i - 1])
if check_accuracy(z[3], td[1]):
correct_ans_count += 1
return correct_ans_count / data_size
# ----------------------------------------
def d_sigmoid(x):
return sigmoid(x) * (1 - sigmoid(x))
# Hyper parameters
batch_size = 10
learning_rate = 0.6
epoch_number = 5
costs = []
def run_back_propagated():
data_size = 200
trimmed_train_set = train_set[:data_size]
for i in range(0, epoch_number):
# shuffle the train set
random.shuffle(trimmed_train_set)
batches = [train_set[x:x + batch_size] for x in range(0, data_size, batch_size)]
for batch in batches:
grad_W = [
np.random.normal(size=(layer_sizes[1], layer_sizes[0])),
np.random.normal(size=(layer_sizes[2], layer_sizes[1])),
np.random.normal(size=(layer_sizes[3], layer_sizes[2]))
]
grad_B = [
np.zeros((layer_sizes[1], 1)),
np.zeros((layer_sizes[2], 1)),
np.zeros((layer_sizes[3], 1))
]
for td in batch:
z = [
np.zeros((layer_sizes[0], 1)),
np.zeros((layer_sizes[1], 1)),
np.zeros((layer_sizes[2], 1)),
np.zeros((layer_sizes[3], 1))
]
# values of the first layer (0th), initialized as the train data
z[0] = td[0]
np.reshape(z[0], (102, 1))
for j in range(1, 4):
# for each next layer, z is calculated as discussed below
z[j] = sigmoid(W[j - 1] @ z[j - 1] + B[j - 1])
# ** layer 4
for j in range(layer_sizes[3]):
grad_B[2][j, 0] += 2 * (z[3][j, 0] - td[1][j, 0]) * d_sigmoid(z[3][j, 0]) # bias layer 4
for k in range(layer_sizes[2]): # weight layer 4
grad_W[2][j, k] += 2 * (z[3][j, 0] - td[1][j, 0]) * d_sigmoid(z[3][j, 0]) * z[2][k, 0]
delta_2 = np.zeros((layer_sizes[2], 1))
for k in range(layer_sizes[2]):
for j in range(layer_sizes[3]):
delta_2[k, 0] += 2 * (z[3][j, 0] - td[1][j, 0]) * d_sigmoid(z[3][j, 0]) * W[2][j, k]
# ** layer 3
for j in range(layer_sizes[2]):
grad_B[1][j, 0] += delta_2[j, 0] * d_sigmoid(z[2][j, 0]) # bias layer 3
for k in range(layer_sizes[1]): # weight layer 3
grad_W[1][j, k] += delta_2[j, 0] * d_sigmoid(z[2][j, 0]) * z[1][k, 0]
delta_1 = np.zeros((layer_sizes[1], 1))
for k in range(layer_sizes[1]):
for j in range(layer_sizes[2]):
delta_1[k, 0] += delta_2[j, 0] * d_sigmoid(z[2][j, 0]) * W[1][j, k]
# ** layer 2
for j in range(layer_sizes[1]):
grad_B[0][j, 0] += delta_1[j, 0] * d_sigmoid(z[1][j, 0]) # bias layer 2
for k in range(layer_sizes[0]): # weight layer 2
grad_W[0][j, k] += delta_1[j, 0] * d_sigmoid(z[1][j, 0]) * z[0][k, 0]
# update, using the gradient
for ind in range(0, 3):
W[ind] -= learning_rate * (grad_W[ind] / batch_size)
B[ind] -= learning_rate * (grad_B[ind] / batch_size)
cost = 0
correct_ans_count = 0
for td in trimmed_train_set:
z = [
np.zeros((layer_sizes[0], 1)),
np.zeros((layer_sizes[1], 1)),
np.zeros((layer_sizes[2], 1)),
np.zeros((layer_sizes[3], 1))
]
# values of the first layer (0th), initialized as the train data
z[0] = td[0]
np.reshape(z[0], (102, 1))
for it in range(1, 4):
# for each next layer, z is calculated as discussed below
z[it] = sigmoid(W[it - 1] @ z[it - 1] + B[it - 1])
for j in range(layer_sizes[3]):
cost += np.power((z[3][j, 0] - td[1][j, 0]), 2)
if check_accuracy(z[3], td[1]):
correct_ans_count += 1
print(correct_ans_count)
print(data_size)
cost /= data_size
costs.append(cost)
run_back_propagated()
epoch_size = [x for x in range(epoch_number)]
plt.plot(epoch_size, costs)
plt.show()
print("Back Propagation Accuracy: ", run_feed_forward())