Jensen-holm commited on
Commit
24f2542
·
1 Parent(s): 31806c5

splitting up some backprop functions that may have been too long

Browse files
app.py CHANGED
@@ -25,6 +25,11 @@ def index():
25
  return render_template("index.html")
26
 
27
 
 
 
 
 
 
28
  @app.route("/process_algorithm", methods=["GET", "POST"])
29
  def process_algorithm():
30
  alg = request.form.get('model-select')
@@ -33,7 +38,7 @@ def process_algorithm():
33
  # have a form for options based on the algorithm the user chose
34
  # and set it as the args variable, make a 'go' button for this funcitonality
35
  # to start the algorithm
36
- args = 0
37
  if args:
38
  # create random numpy array dataset
39
  X, y = random_dataset(100, 3)
 
25
  return render_template("index.html")
26
 
27
 
28
+ @app.route("/select_algorithm", methods=["GET", "POST"])
29
+ def select_algorithm():
30
+ return
31
+
32
+
33
  @app.route("/process_algorithm", methods=["GET", "POST"])
34
  def process_algorithm():
35
  alg = request.form.get('model-select')
 
38
  # have a form for options based on the algorithm the user chose
39
  # and set it as the args variable, make a 'go' button for this funcitonality
40
  # to start the algorithm
41
+ args = request.form.get("params")
42
  if args:
43
  # create random numpy array dataset
44
  X, y = random_dataset(100, 3)
neural_network/backprop.py CHANGED
@@ -4,21 +4,50 @@ 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) -> (dict, np.array):
8
- epochs = args["epochs"]
9
- func = activation[args["activation_func"]]["main"]
10
- func_prime = activation[args["activation_func"]]["prime"]
11
- w1, w2 = wb["W1"], wb["W2"]
12
- b1, b2 = wb["b1"], wb["b2"]
13
- lr = args["learning_rate"]
 
 
 
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  r = {}
16
  loss_history = []
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.append(mean_squared_error)
24
 
@@ -32,7 +61,8 @@ def bp(X_train: np.array, y_train: np.array, wb: dict, args: dict) -> (dict, np.
32
  error * func_prime(y_hat),
33
  )
34
  db2 = np.sum(error * func_prime(y_hat), axis=0)
35
- db1 = np.sum(np.dot(error * func_prime(y_hat), w2.T) * func_prime(node1), axis=0)
 
36
 
37
  # update weights & biases using gradient descent.
38
  # this is -= and not += because if the gradient descent
 
4
  from neural_network.opts import activation
5
 
6
 
7
+ def get_args(args: dict, wb: dict):
8
+ return (
9
+ args["epochs"],
10
+ args["activation_func"],
11
+ args["func_prime"],
12
+ args["learning_rate"],
13
+ wb["W1"],
14
+ wb["W2"],
15
+ wb["b1"],
16
+ wb["b2"],
17
+ )
18
 
19
+
20
+ def fp(
21
+ X_train: np.array,
22
+ y_train: np.array,
23
+ actiavtion: callable,
24
+ w1: np.array,
25
+ w2: np.array,
26
+ b1: np.array,
27
+ b2: np.array,
28
+ ):
29
+ n1 = compute_node(arr=X_train, w=w1, b=b1, func=activation)
30
+ y_hat = compute_node(arr=n1, w=w2, b=b2, func=activation)
31
+ return y_hat, n1, (y_hat-y_train)
32
+
33
+
34
+ def bp(
35
+ X_train: np.array,
36
+ y_train: np.array,
37
+ wb: dict,
38
+ args: dict
39
+ ):
40
+ epochs, func, func_prime, lr, w1, w2, b1, b2 = get_args(args, wb)
41
  r = {}
42
  loss_history = []
43
  for e in tqdm(range(epochs)):
44
  # forward prop
45
+ y_hat, node1, error = fp(
46
+ X_train=X_train,
47
+ y_train=y_train,
48
+ actiavtion=func,
49
+ w1=w1, w2=w2, b1=b1, b2=b2,
50
+ )
51
  mean_squared_error = mse(y_train, y_hat)
52
  loss_history.append(mean_squared_error)
53
 
 
61
  error * func_prime(y_hat),
62
  )
63
  db2 = np.sum(error * func_prime(y_hat), axis=0)
64
+ db1 = np.sum(np.dot(error * func_prime(y_hat), w2.T)
65
+ * func_prime(node1), axis=0)
66
 
67
  # update weights & biases using gradient descent.
68
  # this is -= and not += because if the gradient descent
neural_network/main.py CHANGED
@@ -7,7 +7,10 @@ from neural_network.model import Network
7
  from neural_network.plot import loss_history_plt, save_plt
8
 
9
 
10
- def init(X: np.array, hidden_size: int) -> dict:
 
 
 
11
  """
12
  returns a dictionary containing randomly initialized
13
  weights and biases to start off the neural_network
 
7
  from neural_network.plot import loss_history_plt, save_plt
8
 
9
 
10
+ def init(
11
+ X: np.array,
12
+ hidden_size: int
13
+ ) -> dict:
14
  """
15
  returns a dictionary containing randomly initialized
16
  weights and biases to start off the neural_network
templates/index.html CHANGED
@@ -14,7 +14,7 @@
14
  <p class="text-gray-500">By: Jensen Holm</p>
15
  </div>
16
  <div class="w-full md:w-1/3">
17
- <form method="POST" action="{{ url_for('process_algorithm') }}">
18
  <label class="block font-medium text-gray-700 mb-2" for="model-select">
19
  Select Algorithm
20
  </label>
 
14
  <p class="text-gray-500">By: Jensen Holm</p>
15
  </div>
16
  <div class="w-full md:w-1/3">
17
+ <form method="POST" action="{{ url_for('select_algorithm') }}">
18
  <label class="block font-medium text-gray-700 mb-2" for="model-select">
19
  Select Algorithm
20
  </label>
templates/opts.html ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Data Mining From Scratch</title>
6
+ <link rel="stylesheet" href="{{ url_for('static', filename='dist/css/output.css') }}">
7
+ </head>
8
+ <body class="bg-gray-100">
9
+ <div class="container mx-auto flex flex-col items-center justify-center h-screen">
10
+ <div class="text-center mb-8">
11
+ <h1 class="text-4xl font-bold">Data Mining From Scratch</h1>
12
+ <p class="text-gray-500">By: Jensen Holm</p>
13
+ </div>
14
+ <div class="w-full md:w-1/3">
15
+ <form method="POST" action="{{ url_for('process_algorithm') }}">
16
+ <label class="block font-medium text-gray-700 mb-2" for="model-select">
17
+ Tune {{ alg }} Parameters
18
+ </label>
19
+ <select id="model-select" name="model-select"
20
+ class="form-select block w-full mt-1 rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
21
+ {{% for i in opts %}}
22
+ <option value="{{i}}">{{ i }}</option>
23
+ {{% endfor %}}
24
+ </select>
25
+ <button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mt-4">
26
+ Go
27
+ </button>
28
+ </form>
29
+ </div>
30
+ </div>
31
+ </body>
32
+ </html>