-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentral_poly.py
148 lines (115 loc) · 3.97 KB
/
central_poly.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import numpy as np
import matplotlib.pyplot as plt
import sys
import os
import re
import random
import tensorflow as tf
import keras
from settings import *
def read_weights(model):
res = {}
layer_num = 0
for layer in model.layers:
weights = layer.get_weights()
if weights:
res[layer_num] = {'weights': weights, 'name': layer.name, 'layer': layer}
if layer.name == 'batch_normalization':
res[layer_num]['epsilon'] = layer.epsilon
layer_num += 1
return res
def parse_file(filename):
layers = []
lines = []
cur_layer = None
with open(filename, 'r') as reader:
line = reader.readline()
while line != '':
if line.startswith('Printing'):
# start of a new layer
if len(lines) > 0 and cur_layer != None:
layers.append(lines)
lines = []
cur_layer = None
m = re.match("Printing layer: hidden_(\d+)", line)
if m != None:
# new layer
cur_layer = m.group(1)
else:
# this is an output layer
m = re.match("Printing layer: (.+)", line)
if m == None:
raise Exception("Cannot happen, as there was a match just a few lines before")
cur_layer = m.group(1)
else:
lines.append(line)
line = reader.readline()
if len(lines) > 0 and cur_layer != None:
layers.append(lines)
lines = []
cur_layer = None
return layers
def simple_max_approximation(x, y):
return ((x**10 + y**10) / 2.0)**0.1
def get_max(val_list):
max_index = -1
index = 0
max_val = 0.0
for index in range(val_list):
if index == 0:
max_val = val_list[index]
else:
max_val = simple_max_approximation(val_list[index], max_val)
index = 0
max_index = -1
closest = max(val_list)
for val in val_list:
if abs(val - max_val) < closest:
max_index = index
closest = abs(val - max_val)
index += 1
return max_index
def run_poly(inputs, layers):
for i in range(len(inputs)):
exec("x{0}={1}".format(i, inputs[i]))
for layeri in layers:
for unit in layeri:
exec(unit)
exec("arr = [c{0}0, c{0}1, c{0}2, c{0}3, c{0}4, c{0}5, c{0}6, c{0}7, c{0}8, c{0}9]".format(5))
mind = -1
#mind = eval("arr.index(max(arr))")
mind = eval("get_max(arr)")
return mind
# def run_implicit(input, res):
if __name__ == "__main__":
if len(sys.argv) == 1:
print("Usage: central_poly.py <network filename in text format>")
exit(0)
layers = parse_file(sys.argv[1])
# get the data
# load data
f_mnist = keras.datasets.fashion_mnist
(X_train, Y_train), (X_test, Y_test) = f_mnist.load_data()
class_labels = np.array(["T-shirt", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Bag", "Ankle Boot"])
model = tf.keras.models.load_model("trained_model.h5")
model_layers = read_weights(model)
num_test_items = 1000
diff_results = 0
model_results = 0
poly_results = 0
print("Poly:\tModel:\tY:")
for i in range(0,num_test_items):
xind = random.randint(0, len(X_test))
x = np.array(X_test[xind]).flatten()
ans = run_poly(x, layers)
marr = model.predict(X_test[xind:xind+1])
if ans == Y_test[xind]:
poly_results += 1
if np.argmax(marr) == Y_test[xind]:
model_results += 1
if np.argmax(marr) != ans:
diff_results += 1
print(ans, "\t", np.argmax(marr), "\t", Y_test[xind])
print("Poly results: " + str(poly_results/num_test_items))
print("Model results: " + str(model_results/num_test_items))
print("Difference between poly and model: " + str(diff_results/num_test_items))