Spaces:
Build error
Build error
Commit ·
204251b
1
Parent(s): 4e6140d
computing loss history over time
Browse files- main.py +0 -1
- neural_network/backprop.py +12 -2
- neural_network/main.py +5 -2
main.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
from opts import options
|
| 2 |
import numpy as np
|
| 3 |
-
from pprint import pprint
|
| 4 |
|
| 5 |
|
| 6 |
def random_dataset(rows: int, features: int):
|
|
|
|
| 1 |
from opts import options
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def random_dataset(rows: int, features: int):
|
neural_network/backprop.py
CHANGED
|
@@ -4,7 +4,7 @@ from tqdm import tqdm
|
|
| 4 |
from neural_network.opts import activation
|
| 5 |
|
| 6 |
|
| 7 |
-
def bp(X_train: np.array, y_train: np.array, wb: dict, args: dict):
|
| 8 |
epochs = args["epochs"]
|
| 9 |
func = activation[args["activation_func"]]["main"]
|
| 10 |
func_prime = activation[args["activation_func"]]["prime"]
|
|
@@ -13,11 +13,14 @@ def bp(X_train: np.array, y_train: np.array, wb: dict, args: dict):
|
|
| 13 |
lr = args["learning_rate"]
|
| 14 |
|
| 15 |
r = {}
|
|
|
|
| 16 |
for e in tqdm(range(epochs)):
|
| 17 |
# forward prop
|
| 18 |
node1 = compute_node(arr=X_train, w=w1, b=b1, func=func)
|
| 19 |
y_hat = compute_node(arr=node1, w=w2, b=b2, func=func)
|
| 20 |
error = y_hat - y_train
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# backprop
|
| 23 |
dw1 = np.dot(
|
|
@@ -39,6 +42,7 @@ def bp(X_train: np.array, y_train: np.array, wb: dict, args: dict):
|
|
| 39 |
b1 -= (lr * db1)
|
| 40 |
b2 -= (lr * db2)
|
| 41 |
|
|
|
|
| 42 |
r[e] = {
|
| 43 |
"W1": w1,
|
| 44 |
"W2": w2,
|
|
@@ -48,8 +52,10 @@ def bp(X_train: np.array, y_train: np.array, wb: dict, args: dict):
|
|
| 48 |
"dw2": dw2,
|
| 49 |
"db1": db1,
|
| 50 |
"db2": db2,
|
|
|
|
|
|
|
| 51 |
}
|
| 52 |
-
return r
|
| 53 |
|
| 54 |
|
| 55 |
def compute_node(arr, w, b, func):
|
|
@@ -57,3 +63,7 @@ def compute_node(arr, w, b, func):
|
|
| 57 |
Computes nodes during forward prop
|
| 58 |
"""
|
| 59 |
return func(np.dot(arr, w) + b)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from neural_network.opts import activation
|
| 5 |
|
| 6 |
|
| 7 |
+
def bp(X_train: np.array, y_train: np.array, wb: dict, args: dict) -> (dict, np.array):
|
| 8 |
epochs = args["epochs"]
|
| 9 |
func = activation[args["activation_func"]]["main"]
|
| 10 |
func_prime = activation[args["activation_func"]]["prime"]
|
|
|
|
| 13 |
lr = args["learning_rate"]
|
| 14 |
|
| 15 |
r = {}
|
| 16 |
+
loss_history = np.array([])
|
| 17 |
for e in tqdm(range(epochs)):
|
| 18 |
# forward prop
|
| 19 |
node1 = compute_node(arr=X_train, w=w1, b=b1, func=func)
|
| 20 |
y_hat = compute_node(arr=node1, w=w2, b=b2, func=func)
|
| 21 |
error = y_hat - y_train
|
| 22 |
+
mean_squared_error = mse(y_train, y_hat)
|
| 23 |
+
loss_history = np.append(loss_history, mean_squared_error)
|
| 24 |
|
| 25 |
# backprop
|
| 26 |
dw1 = np.dot(
|
|
|
|
| 42 |
b1 -= (lr * db1)
|
| 43 |
b2 -= (lr * db2)
|
| 44 |
|
| 45 |
+
# keeping track of each epochs' numbers
|
| 46 |
r[e] = {
|
| 47 |
"W1": w1,
|
| 48 |
"W2": w2,
|
|
|
|
| 52 |
"dw2": dw2,
|
| 53 |
"db1": db1,
|
| 54 |
"db2": db2,
|
| 55 |
+
"error": error,
|
| 56 |
+
"mse": mean_squared_error,
|
| 57 |
}
|
| 58 |
+
return r, loss_history
|
| 59 |
|
| 60 |
|
| 61 |
def compute_node(arr, w, b, func):
|
|
|
|
| 63 |
Computes nodes during forward prop
|
| 64 |
"""
|
| 65 |
return func(np.dot(arr, w) + b)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def mse(y: np.array, y_hat: np.array):
|
| 69 |
+
return np.mean((y - y_hat) ** 2)
|
neural_network/main.py
CHANGED
|
@@ -48,8 +48,8 @@ def main(
|
|
| 48 |
|
| 49 |
# once we have these results we should test it against
|
| 50 |
# the y_test data
|
| 51 |
-
results = bp(X_train, y_train, wb, args)
|
| 52 |
-
final = results[args["epochs"]-1]
|
| 53 |
func = activation[args["activation_func"]]["main"]
|
| 54 |
fm = Model(final_wb=final, activation_func=func)
|
| 55 |
|
|
@@ -57,3 +57,6 @@ def main(
|
|
| 57 |
pred = fm.predict(X_test)
|
| 58 |
mse = np.mean((pred - y_test) ** 2)
|
| 59 |
print(f"mean squared error: {mse}")
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# once we have these results we should test it against
|
| 50 |
# the y_test data
|
| 51 |
+
results, loss_history = bp(X_train, y_train, wb, args)
|
| 52 |
+
final = results[args["epochs"] - 1]
|
| 53 |
func = activation[args["activation_func"]]["main"]
|
| 54 |
fm = Model(final_wb=final, activation_func=func)
|
| 55 |
|
|
|
|
| 57 |
pred = fm.predict(X_test)
|
| 58 |
mse = np.mean((pred - y_test) ** 2)
|
| 59 |
print(f"mean squared error: {mse}")
|
| 60 |
+
|
| 61 |
+
# plot predicted versus actual
|
| 62 |
+
# also plot the training loss over epochs
|