Spaces:
Build error
Build error
Commit
·
303df47
1
Parent(s):
8ea5c07
adding accuracy score tracking
Browse files- app.py +4 -1
- nn/nn.py +6 -0
- nn/train.py +7 -3
app.py
CHANGED
|
@@ -45,4 +45,7 @@ def neural_net():
|
|
| 45 |
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
-
app.run(
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
+
app.run(
|
| 49 |
+
port=4000,
|
| 50 |
+
debug=True,
|
| 51 |
+
)
|
nn/nn.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from typing import Callable
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
|
|
@@ -41,6 +42,11 @@ class NN:
|
|
| 41 |
self.input_size = len(self.X.columns)
|
| 42 |
self.output_size = len(self.y_dummy.columns)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def set_func(self, f: Callable) -> None:
|
| 45 |
assert isinstance(f, Callable)
|
| 46 |
self.func = f
|
|
|
|
| 1 |
from typing import Callable
|
| 2 |
+
from sklearn.preprocessing import StandardScaler
|
| 3 |
import pandas as pd
|
| 4 |
|
| 5 |
|
|
|
|
| 42 |
self.input_size = len(self.X.columns)
|
| 43 |
self.output_size = len(self.y_dummy.columns)
|
| 44 |
|
| 45 |
+
def normalize(self):
|
| 46 |
+
scaler = StandardScaler()
|
| 47 |
+
self.y_dummy = scaler.fit_transform(self.y_dummy)
|
| 48 |
+
self.X = scaler.fit_transform(self.X)
|
| 49 |
+
|
| 50 |
def set_func(self, f: Callable) -> None:
|
| 51 |
assert isinstance(f, Callable)
|
| 52 |
self.func = f
|
nn/train.py
CHANGED
|
@@ -6,7 +6,7 @@ import numpy as np
|
|
| 6 |
|
| 7 |
|
| 8 |
def init_weights_biases(nn: NN):
|
| 9 |
-
|
| 10 |
bh = np.zeros((1, nn.hidden_size))
|
| 11 |
bo = np.zeros((1, nn.output_size))
|
| 12 |
wh = np.random.randn(nn.input_size, nn.hidden_size) * \
|
|
@@ -18,14 +18,15 @@ def init_weights_biases(nn: NN):
|
|
| 18 |
|
| 19 |
def train(nn: NN) -> dict:
|
| 20 |
wh, wo, bh, bo = init_weights_biases(nn=nn)
|
|
|
|
| 21 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 22 |
nn.X.to_numpy(),
|
| 23 |
nn.y_dummy.to_numpy(),
|
| 24 |
test_size=nn.test_size,
|
| 25 |
-
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
| 29 |
loss_hist: list[float] = []
|
| 30 |
for _ in range(nn.epochs):
|
| 31 |
# compute hidden output
|
|
@@ -46,6 +47,8 @@ def train(nn: NN) -> dict:
|
|
| 46 |
# compute error & store it
|
| 47 |
error = y_hat - y_train
|
| 48 |
loss = log_loss(y_true=y_train, y_pred=y_hat)
|
|
|
|
|
|
|
| 49 |
loss_hist.append(loss)
|
| 50 |
|
| 51 |
# compute derivatives of weights & biases
|
|
@@ -84,6 +87,7 @@ def train(nn: NN) -> dict:
|
|
| 84 |
"loss_hist": loss_hist,
|
| 85 |
"log_loss": log_loss(y_true=y_test, y_pred=y_hat),
|
| 86 |
"accuracy": accuracy_score(y_true=y_test, y_pred=y_hat),
|
|
|
|
| 87 |
}
|
| 88 |
|
| 89 |
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
def init_weights_biases(nn: NN):
|
| 9 |
+
np.random.seed(0)
|
| 10 |
bh = np.zeros((1, nn.hidden_size))
|
| 11 |
bo = np.zeros((1, nn.output_size))
|
| 12 |
wh = np.random.randn(nn.input_size, nn.hidden_size) * \
|
|
|
|
| 18 |
|
| 19 |
def train(nn: NN) -> dict:
|
| 20 |
wh, wo, bh, bo = init_weights_biases(nn=nn)
|
| 21 |
+
|
| 22 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 23 |
nn.X.to_numpy(),
|
| 24 |
nn.y_dummy.to_numpy(),
|
| 25 |
test_size=nn.test_size,
|
| 26 |
+
random_state=0,
|
| 27 |
)
|
| 28 |
|
| 29 |
+
accuracy_scores = []
|
| 30 |
loss_hist: list[float] = []
|
| 31 |
for _ in range(nn.epochs):
|
| 32 |
# compute hidden output
|
|
|
|
| 47 |
# compute error & store it
|
| 48 |
error = y_hat - y_train
|
| 49 |
loss = log_loss(y_true=y_train, y_pred=y_hat)
|
| 50 |
+
accuracy = accuracy_score(y_true=y_train, y_pred=y_hat)
|
| 51 |
+
accuracy_scores.append(accuracy)
|
| 52 |
loss_hist.append(loss)
|
| 53 |
|
| 54 |
# compute derivatives of weights & biases
|
|
|
|
| 87 |
"loss_hist": loss_hist,
|
| 88 |
"log_loss": log_loss(y_true=y_test, y_pred=y_hat),
|
| 89 |
"accuracy": accuracy_score(y_true=y_test, y_pred=y_hat),
|
| 90 |
+
"accuracy_scores": accuracy_scores,
|
| 91 |
}
|
| 92 |
|
| 93 |
|