Spaces:
Build error
Build error
Commit
·
c777165
1
Parent(s):
031ac83
making more modular so we can develop our other methods too
Browse files- cluster/kmeans.py +0 -0
- cluster/kmedoids.py +0 -0
- cluster/main.py +2 -0
- cluster/opts.py +4 -0
- main.py +11 -9
- neural_network/activation.py +4 -2
- neural_network/backprop.py +3 -5
- neural_network/main.py +5 -8
- neural_network/opts.py +0 -1
- opts.py +7 -0
cluster/kmeans.py
ADDED
|
File without changes
|
cluster/kmedoids.py
ADDED
|
File without changes
|
cluster/main.py
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def main():
|
| 2 |
+
return
|
cluster/opts.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
clustering_methods = {
|
| 4 |
+
}
|
main.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import numpy as np
|
| 2 |
-
|
| 3 |
-
import neural_network.main as nn
|
| 4 |
|
| 5 |
|
| 6 |
def random_dataset():
|
|
@@ -16,13 +15,16 @@ def random_dataset():
|
|
| 16 |
)
|
| 17 |
|
| 18 |
|
| 19 |
-
|
| 20 |
method = input("\nChoose a method to test: ").lower()
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
X, y = random_dataset()
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
+
from opts import options
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def random_dataset():
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
|
| 18 |
+
def main():
|
| 19 |
method = input("\nChoose a method to test: ").lower()
|
| 20 |
+
try:
|
| 21 |
+
func = options[method]
|
| 22 |
+
except KeyError:
|
| 23 |
+
raise f"Invalid method \"{method}\". Try one of these\n{list(options.keys())}"
|
| 24 |
|
| 25 |
X, y = random_dataset()
|
| 26 |
+
result = func(X, y)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
main()
|
neural_network/activation.py
CHANGED
|
@@ -4,12 +4,14 @@ import numpy as np
|
|
| 4 |
def sigmoid(x: float) -> float:
|
| 5 |
return 1.0 / (1.0 + np.exp(-x))
|
| 6 |
|
|
|
|
| 7 |
def sigmoid_prime(x: float) -> float:
|
| 8 |
-
return sigmoid(x) / (1.0 - sigmoid())
|
|
|
|
| 9 |
|
| 10 |
def relu(x):
|
| 11 |
return np.maximum(x, 0)
|
| 12 |
|
|
|
|
| 13 |
def relu_prime(x):
|
| 14 |
return np.where(x > 0, 1, 0)
|
| 15 |
-
|
|
|
|
| 4 |
def sigmoid(x: float) -> float:
|
| 5 |
return 1.0 / (1.0 + np.exp(-x))
|
| 6 |
|
| 7 |
+
|
| 8 |
def sigmoid_prime(x: float) -> float:
|
| 9 |
+
return sigmoid(x) / (1.0 - sigmoid(x))
|
| 10 |
+
|
| 11 |
|
| 12 |
def relu(x):
|
| 13 |
return np.maximum(x, 0)
|
| 14 |
|
| 15 |
+
|
| 16 |
def relu_prime(x):
|
| 17 |
return np.where(x > 0, 1, 0)
|
|
|
neural_network/backprop.py
CHANGED
|
@@ -21,20 +21,18 @@ def bp(X_train: np.array, y_train: np.array, wb: dict, args: dict):
|
|
| 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),
|
| 26 |
)
|
| 27 |
dw1 = np.dot(
|
| 28 |
-
X_train.T,
|
| 29 |
np.dot(error * func_prime(y_hat), w2.T) * func_prime(node1),
|
| 30 |
)
|
| 31 |
|
| 32 |
# update weights & biases
|
| 33 |
w1 -= lr * dw1
|
| 34 |
w2 -= lr * dw2
|
| 35 |
-
|
| 36 |
|
| 37 |
def compute_node(X, w, b, func):
|
| 38 |
return func(np.dot(X, w) + b)
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 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),
|
| 26 |
)
|
| 27 |
dw1 = np.dot(
|
| 28 |
+
X_train.T,
|
| 29 |
np.dot(error * func_prime(y_hat), w2.T) * func_prime(node1),
|
| 30 |
)
|
| 31 |
|
| 32 |
# update weights & biases
|
| 33 |
w1 -= lr * dw1
|
| 34 |
w2 -= lr * dw2
|
| 35 |
+
|
| 36 |
|
| 37 |
def compute_node(X, w, b, func):
|
| 38 |
return func(np.dot(X, w) + b)
|
|
|
|
|
|
neural_network/main.py
CHANGED
|
@@ -31,19 +31,16 @@ def init(X: np.array, y: np.array, hidden_size: int) -> dict:
|
|
| 31 |
|
| 32 |
|
| 33 |
def main(
|
| 34 |
-
|
| 35 |
-
|
| 36 |
) -> None:
|
| 37 |
-
|
| 38 |
args = get_args()
|
| 39 |
wb = init(X, y, args["hidden_size"])
|
| 40 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 41 |
-
X,
|
| 42 |
-
y,
|
| 43 |
-
test_size=0.3,
|
| 44 |
random_state=8675309
|
| 45 |
)
|
| 46 |
|
| 47 |
results = bp(X_train, y_train, wb, args)
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
def main(
|
| 34 |
+
X: np.array,
|
| 35 |
+
y: np.array,
|
| 36 |
) -> None:
|
|
|
|
| 37 |
args = get_args()
|
| 38 |
wb = init(X, y, args["hidden_size"])
|
| 39 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 40 |
+
X,
|
| 41 |
+
y,
|
| 42 |
+
test_size=0.3,
|
| 43 |
random_state=8675309
|
| 44 |
)
|
| 45 |
|
| 46 |
results = bp(X_train, y_train, wb, args)
|
|
|
|
|
|
neural_network/opts.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
from neural_network.activation import *
|
| 2 |
|
| 3 |
-
|
| 4 |
activation = {
|
| 5 |
"relu": {
|
| 6 |
"main": relu,
|
|
|
|
| 1 |
from neural_network.activation import *
|
| 2 |
|
|
|
|
| 3 |
activation = {
|
| 4 |
"relu": {
|
| 5 |
"main": relu,
|
opts.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from neural_network.main import main as nn
|
| 2 |
+
from cluster.main import main as clust
|
| 3 |
+
|
| 4 |
+
options = {
|
| 5 |
+
"nn": nn,
|
| 6 |
+
"cluster": clust,
|
| 7 |
+
}
|