Spaces:
Sleeping
Sleeping
Jensen-holm
commited on
Commit
•
8c348c5
1
Parent(s):
29cce3f
handling errors well with the neural netork api
Browse files- nn/activation.py +46 -0
- nn/nn.py +28 -0
- nn/train.py +2 -2
nn/activation.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Callable
|
2 |
+
from nn.nn import NN
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
|
6 |
+
def get_activation(nn: NN) -> Callable:
|
7 |
+
a = nn.activation
|
8 |
+
funcs = {
|
9 |
+
"relu": relu,
|
10 |
+
"sigmoid": sigmoid,
|
11 |
+
"tanh": tanh,
|
12 |
+
}
|
13 |
+
|
14 |
+
prime_funcs = {
|
15 |
+
"sigmoid": sigmoid_prime,
|
16 |
+
"tanh": tanh_prime,
|
17 |
+
"relu": relu_prime,
|
18 |
+
}
|
19 |
+
|
20 |
+
nn.set_func(funcs[a])
|
21 |
+
nn.set_func_prime(funcs[a])
|
22 |
+
|
23 |
+
|
24 |
+
def relu(x):
|
25 |
+
return np.max(0.0, x)
|
26 |
+
|
27 |
+
|
28 |
+
def relu_prime(x):
|
29 |
+
return
|
30 |
+
|
31 |
+
|
32 |
+
def sigmoid(x):
|
33 |
+
return 1.0 / (1.0 + np.exp(-x))
|
34 |
+
|
35 |
+
|
36 |
+
def sigmoid_prime(x):
|
37 |
+
s = sigmoid(x)
|
38 |
+
return s / (1.0 - s)
|
39 |
+
|
40 |
+
|
41 |
+
def tanh(x):
|
42 |
+
return np.tanh(x)
|
43 |
+
|
44 |
+
|
45 |
+
def tanh_prime(x):
|
46 |
+
return
|
nn/nn.py
CHANGED
@@ -1,4 +1,6 @@
|
|
|
|
1 |
import pandas as pd
|
|
|
2 |
|
3 |
|
4 |
class NN:
|
@@ -12,6 +14,11 @@ class NN:
|
|
12 |
features: list[str],
|
13 |
target: str,
|
14 |
data: str,
|
|
|
|
|
|
|
|
|
|
|
15 |
):
|
16 |
self.epochs = epochs
|
17 |
self.hidden_size = hidden_size
|
@@ -21,8 +28,29 @@ class NN:
|
|
21 |
self.features = features
|
22 |
self.target = target
|
23 |
self.data = data
|
|
|
|
|
|
|
|
|
24 |
|
|
|
|
|
25 |
self.df: pd.DataFrame = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
@classmethod
|
28 |
def from_dict(cls, dct):
|
|
|
1 |
+
from typing import Callable
|
2 |
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
|
5 |
|
6 |
class NN:
|
|
|
14 |
features: list[str],
|
15 |
target: str,
|
16 |
data: str,
|
17 |
+
|
18 |
+
wh: np.array,
|
19 |
+
wo: np.array,
|
20 |
+
bh: np.array,
|
21 |
+
bo: np.array,
|
22 |
):
|
23 |
self.epochs = epochs
|
24 |
self.hidden_size = hidden_size
|
|
|
28 |
self.features = features
|
29 |
self.target = target
|
30 |
self.data = data
|
31 |
+
self.wh: np.array = wh
|
32 |
+
self.wo: np.array = wo
|
33 |
+
self.bh: np.array = bh
|
34 |
+
self.bo: np.array = bo
|
35 |
|
36 |
+
self.func_prime: Callable = None
|
37 |
+
self.func: Callable = None
|
38 |
self.df: pd.DataFrame = None
|
39 |
+
self.X: pd.DataFrame = None
|
40 |
+
self.y: pd.DataFrame = None
|
41 |
+
|
42 |
+
def read_csv(self) -> dict[str, str]:
|
43 |
+
self.df = pd.read_csv(self.data)
|
44 |
+
self.X = self.df[self.features]
|
45 |
+
self.y = self.df[self.target]
|
46 |
+
|
47 |
+
def set_func(self, f: Callable) -> None:
|
48 |
+
assert isinstance(f, Callable)
|
49 |
+
self.func = f
|
50 |
+
|
51 |
+
def set_func_prime(self, f: Callable) -> None:
|
52 |
+
assert isinstance(f, Callable)
|
53 |
+
self.func_prime = f
|
54 |
|
55 |
@classmethod
|
56 |
def from_dict(cls, dct):
|
nn/train.py
CHANGED
@@ -4,7 +4,7 @@ import pandas as pd
|
|
4 |
import numpy as np
|
5 |
|
6 |
|
7 |
-
def train(nn: NN):
|
8 |
X_train, X_test, y_train, y_test = train_test_split(
|
9 |
nn.X,
|
10 |
nn.y,
|
@@ -12,4 +12,4 @@ def train(nn: NN):
|
|
12 |
random_state=88,
|
13 |
)
|
14 |
|
15 |
-
|
|
|
4 |
import numpy as np
|
5 |
|
6 |
|
7 |
+
def train(nn: NN) -> dict:
|
8 |
X_train, X_test, y_train, y_test = train_test_split(
|
9 |
nn.X,
|
10 |
nn.y,
|
|
|
12 |
random_state=88,
|
13 |
)
|
14 |
|
15 |
+
return {"status": "you made it!"}
|