-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
49 lines (38 loc) · 1.27 KB
/
test.py
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
from MLP import MLPNetwork
import numpy as np
from time import perf_counter
# Use to get a slice of our data set
def sample(i, idx):
return i[idx, :]
# Create our truth table dataset
input = np.zeros((16, 4))
output = np.zeros((16, 1))
for a in range(2):
for b in range(2):
for c in range(2):
for d in range(2):
input[8 * a + 4 * b + 2 * c + d, :] = np.array([a, b, c, d])
output[8 * a + 4 * b + 2 * c + d, :] = (
1 if (a + b + c + d) % 2 == 1 else 0
)
print(*[np.append(sample(input, i), output[i]) for i in range(16)], sep="\n")
# Validate our set
for i in range(16):
row = sample(input, i)
assert (1 if np.sum(row[:4]) % 2 == 1 else 0) == output[i], "Incorrect set"
print("All cases correct!")
def time(
func, *params
): # helper function to print the time it takes to execute a function
start = perf_counter()
result = func(*params)
end = perf_counter()
print(f"{func.__name__} took {end-start} seconds to complete.")
return result
layers = [4, 4, 1]
nn = MLPNetwork(layers)
nn.display()
nn.train(input, output, 1000)
results = nn.predict(input[0, :])[0]
print(f"Model predicts: {np.round(results)}. (Expected: {output[0][0]}, got {results})")
nn.display()