-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
67 lines (48 loc) · 1.89 KB
/
Copy pathutils.py
File metadata and controls
67 lines (48 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import json
weights_path = "C:\\Users\\Choaib ELMADI\\Documents\\D.I.F.Y\\4. Artificial Intelligence\\A. AI Projects\\Neural Digit Visualizer\\App\\Weights"
def load_weights():
with open(f"{weights_path}\\b1.txt", "r") as b1_f:
b1_txt = json.load(b1_f)
b1 = np.array(b1_txt)
with open(f"{weights_path}\\W1.txt", "r") as W1_f:
W1_txt = json.load(W1_f)
W1 = np.array(W1_txt)
with open(f"{weights_path}\\b2.txt", "r") as b2_f:
b2_txt = json.load(b2_f)
b2 = np.array(b2_txt)
with open(f"{weights_path}\\W2.txt", "r") as W2_f:
W2_txt = json.load(W2_f)
W2 = np.array(W2_txt)
with open(f"{weights_path}\\b3.txt", "r") as b3_f:
b3_txt = json.load(b3_f)
b3 = np.array(b3_txt)
with open(f"{weights_path}\\W3.txt", "r") as W3_f:
W3_txt = json.load(W3_f)
W3 = np.array(W3_txt)
return b1, W1, b2, W2, b3, W3
def softmax(z):
ez = np.exp(z)
sm = ez / np.sum(ez)
return sm
def model_computations(b1, W1, b2, W2, b3, W3, image_of_nbr):
L1_activations = np.zeros((20,))
L2_activations = np.zeros((16,))
L3_activations = np.zeros((11,))
for i in range(20):
w1_i = W1[:, i]
z1_i = np.dot(image_of_nbr, w1_i) + b1[i]
L1_activations[i] = np.maximum(0, z1_i)
for i in range(16):
w2_i = W2[:, i]
z2_i = np.dot(L1_activations, w2_i) + b2[i]
L2_activations[i] = np.maximum(0, z2_i)
for i in range(11):
w3_i = W3[:, i]
z3_i = np.dot(L2_activations, w3_i) + b3[i]
L3_activations[i] = z3_i
L1_prbs = ", ".join(f"{p*100:2.2f}" for p in softmax(L1_activations))
L2_prbs = ", ".join(f"{p*100:2.2f}" for p in softmax(L2_activations))
L3_prbs = ", ".join(f"{p*100:2.2f}" for p in softmax(L3_activations))
predicted_number = np.argmax(L3_activations)
return L1_prbs, L2_prbs, L3_prbs, predicted_number