# Step 5: pruning. Weights very close to zero barely change the answer,
# so we set them to exactly zero. A chip can skip a zero.
from tiny_nn import load_windows, split, accuracy, save, load

LIMIT = 0.2

xs, ys = load_windows("windows.csv")
train_x, train_y, test_x, test_y = split(xs, ys)
student = load("student.txt")
before = accuracy(student, test_x, test_y)

removed = 0
total = 0
for weights, bias in student:
    for row in weights:
        for i in range(len(row)):
            total += 1
            if -LIMIT < row[i] < LIMIT:
                row[i] = 0.0
                removed += 1

print(f"Set {removed} of {total} weights to zero (the ones between -{LIMIT} and {LIMIT})")
print(f"Test windows right before: {before} of {len(test_y)}. After: {accuracy(student, test_x, test_y)} of {len(test_y)}")
save(student, "pruned.txt")
print("Saved the pruned student to pruned.txt")
