Spaces:
Sleeping
Sleeping
Commit
·
0f9e8ef
1
Parent(s):
c777165
returning dictionary of computations after each epoch to try to measure accuracy
Browse files- cluster/opts.py +1 -2
- neural_network/backprop.py +23 -6
cluster/opts.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
-
|
3 |
clustering_methods = {
|
|
|
4 |
}
|
|
|
|
|
|
|
1 |
clustering_methods = {
|
2 |
+
|
3 |
}
|
neural_network/backprop.py
CHANGED
@@ -11,6 +11,7 @@ def bp(X_train: np.array, y_train: np.array, wb: dict, args: dict):
|
|
11 |
b1, b2 = wb["b1"], wb["b2"]
|
12 |
lr = args["learning_rate"]
|
13 |
|
|
|
14 |
for e in range(epochs):
|
15 |
# forward prop
|
16 |
node1 = compute_node(X_train, w1, b1, func)
|
@@ -18,8 +19,6 @@ def bp(X_train: np.array, y_train: np.array, wb: dict, args: dict):
|
|
18 |
error = y_hat - y_train
|
19 |
|
20 |
# backprop
|
21 |
-
# right now this is just the weights,
|
22 |
-
# we should also update the biases
|
23 |
dw2 = np.dot(
|
24 |
node1.T,
|
25 |
error * func_prime(y_hat),
|
@@ -28,10 +27,28 @@ def bp(X_train: np.array, y_train: np.array, wb: dict, args: dict):
|
|
28 |
X_train.T,
|
29 |
np.dot(error * func_prime(y_hat), w2.T) * func_prime(node1),
|
30 |
)
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
|
37 |
def compute_node(X, w, b, func):
|
|
|
11 |
b1, b2 = wb["b1"], wb["b2"]
|
12 |
lr = args["learning_rate"]
|
13 |
|
14 |
+
r = {}
|
15 |
for e in range(epochs):
|
16 |
# forward prop
|
17 |
node1 = compute_node(X_train, w1, b1, func)
|
|
|
19 |
error = y_hat - y_train
|
20 |
|
21 |
# backprop
|
|
|
|
|
22 |
dw2 = np.dot(
|
23 |
node1.T,
|
24 |
error * func_prime(y_hat),
|
|
|
27 |
X_train.T,
|
28 |
np.dot(error * func_prime(y_hat), w2.T) * func_prime(node1),
|
29 |
)
|
30 |
+
db2 = np.sum(error * func_prime(y_hat), axis=0)
|
31 |
+
db1 = np.sum(np.dot(error * func_prime(y_hat), w2.T) * func_prime(node1), axis=0)
|
32 |
+
|
33 |
+
# update weights & biases using gradient descent.
|
34 |
+
# this is -= and not += because if the gradient descent
|
35 |
+
# is positive, we want to go down.
|
36 |
+
w1 -= (lr * dw1)
|
37 |
+
w2 -= (lr * dw2)
|
38 |
+
b1 -= (lr * db1)
|
39 |
+
b2 -= (lr * db2)
|
40 |
+
|
41 |
+
r[e] = {
|
42 |
+
"W1": w1,
|
43 |
+
"W2": w2,
|
44 |
+
"b1": b1,
|
45 |
+
"b2": b2,
|
46 |
+
"dw1": dw1,
|
47 |
+
"dw2": dw2,
|
48 |
+
"db1": db1,
|
49 |
+
"db2": db2,
|
50 |
+
}
|
51 |
+
return r
|
52 |
|
53 |
|
54 |
def compute_node(X, w, b, func):
|