-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuild.py
217 lines (187 loc) · 8.43 KB
/
Build.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import pandas as pd
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from tensorflow.python.keras.callbacks import EarlyStopping
from keras.utils import to_categorical
import heapq
class Model:
def train(self, model, train_data, train_labels, validation_data=None, validation_labels=None):
if validation_data is not None and validation_labels is not None:
x_train, x_validation, y_train, y_validation = (train_data, validation_data, train_labels, validation_labels)
else:
x_train, x_validation, y_train, y_validation = train_test_split(
train_images, train_labels, stratify=None, test_size=0.1, random_state=0)
model.fit(x_train, y_train, validation_data=[x_validation, y_validation],
epochs=10, callbacks=[EarlyStopping(patience=5)])
return model
class Point:
def __init__(self, i, j, entry_value):
self.i = i
self.j = j
self.value = entry_value
def __lt__(self, other):
if not isinstance(other, Point):
raise TypeError(f"Argument save must be of type Point, not {type(other)}")
return abs(self.value) < abs(other.value)
class Column:
def __init__(self, i, entry_value):
self.index = i
self.value = entry_value
def __lt__(self, other):
if not isinstance(other, Column):
raise TypeError(f"Argument save must be of type Column, not {type(other)}")
return abs(self.value) < abs(other.value)
class UnitPruning:
def compute_rank(self, weights_matrix):
ordered_cols = []
n_rows, n_cols = np.shape(weights_matrix)
for j in range(0, n_cols):
col_sum = 0
for i in range(0, n_rows):
col_sum = col_sum + (weights_matrix[i][j] ** 2)
l2_norm = col_sum ** (1/2)
cur_col = Column(j, l2_norm)
heapq.heappush(ordered_cols, cur_col)
return ordered_cols
def prune_matrix(self, weights_matrix, k):
"""
Parameters
----------
weights_matrix : np.array
The weights matrix of some layer in the network
k : float in [0, 1]
represents the percentage of weights to remove
"""
ordered_cols = self.compute_rank(weights_matrix)
n_rows, n_cols = np.shape(weights_matrix)
to_remove = int(k * n_cols)
new_weights = weights_matrix
for j in range(0, to_remove):
cur_col = ordered_cols[j]
for i in range(0, n_rows):
new_weights[i][cur_col.index] = 0.0
return new_weights
def prune(self, model, k):
"""
Parameters
----------
model : keras.Model
The model from which to prune weights
k : float in [0, 1]
represents the percentage of weights to remove
"""
for idx, layer in enumerate(model.layers):
if (idx != 0) and (idx != len(model.layers)-1):
layer_weights = layer.get_weights()
weights_matrix = layer_weights[0] #take weight matrix and not biases
new_weights = self.prune_matrix(weights_matrix, k)
layer_weights[0] = new_weights
layer.set_weights(layer_weights)
return model
def prune_and_fine_tune(self, model, k, num_rounds, train_data, train_labels, validation_data=None, validation_labels=None):
model_trainer = Model()
model = weight_pruner.prune(model, k)
model = model_trainer.train(model, train_data, train_labels, validation_data, validation_labels)
model = weight_pruner.prune(model, k)
return model
class WeightPruning:
def compute_rank(self, weights_matrix):
ordered_indices = []
n_rows, n_cols = np.shape(weights_matrix)
for i in range(0, n_rows):
for j in range(0, n_cols):
cur_point = Point(i, j, weights_matrix[i][j])
heapq.heappush(ordered_indices, cur_point)
return ordered_indices
def prune_matrix(self, weights_matrix, k):
"""
Parameters
----------
weights_matrix : np.array
The weights matrix of some layer in the network
k : float in [0, 1]
represents the percentage of weights to remove
"""
ordered_weights = self.compute_rank(weights_matrix)
n_rows, n_cols = np.shape(weights_matrix)
to_remove = int(k * n_rows * n_cols)
new_weights = weights_matrix
for i in range(0, to_remove):
cur_point = ordered_weights[i]
new_weights[cur_point.i][cur_point.j] = 0.0
return new_weights
def prune(self, model, k):
"""
Parameters
----------
model : keras.Model
The model from which to prune weights
k : float in [0, 1]
represents the percentage of weights to remove
"""
for idx, layer in enumerate(model.layers):
if (idx != 0) and (idx != len(model.layers)-1):
layer_weights = layer.get_weights()
weights_matrix = layer_weights[0] #take weight matrix and not biases
new_weights = self.prune_matrix(weights_matrix, k)
layer_weights[0] = new_weights
layer.set_weights(layer_weights)
return model
def prune_and_fine_tune(self, model, k, num_rounds, train_data, train_labels, validation_data=None, validation_labels=None):
model_trainer = Model()
model = weight_pruner.prune(model, k)
model = model_trainer.train(model, train_data, train_labels, validation_data, validation_labels)
model = weight_pruner.prune(model, k)
return model
def read_data():
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
return (train_images, train_labels), (test_images, test_labels)
def check_ratio_of_zeros(tolerance, model):
for idx, layer in enumerate(model.layers):
if (idx != 0) and (idx != len(model.layers) - 1):
num_non_zeros = 0.0
layer_weights = layer.get_weights()
weights_matrix = layer_weights[0]
n_rows, n_cols = np.shape(weights_matrix)
for i in range(0, n_rows):
for j in range(0, n_cols):
if (weights_matrix[i][j] == 0.0):
num_non_zeros = num_non_zeros + 1
zero_ratio = num_non_zeros / (float(n_rows) * float(n_cols))
assert (abs(zero_ratio - el) < tolerance)
if __name__ == '__main__':
(train_images, train_labels), (test_images, test_labels) = read_data()
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
x_train, x_validation, y_train, y_validation = train_test_split(
train_images, train_labels, stratify=None, test_size=0.1, random_state=0)
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(1000, activation='relu'),
keras.layers.Dense(500, activation='relu'),
keras.layers.Dense(200, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, validation_data= [x_validation, y_validation],
epochs=100, callbacks=[EarlyStopping(patience=20)])
test_loss, test_acc1 = model.evaluate(test_images, test_labels)
print('\nTest accuracy:', test_acc1)
weight_pruner = WeightPruning()
pruning_amounts = [0.25, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.97, 0.99]
test_accuracy_list = []
test_accuracy_list.append(test_acc1)
for el in pruning_amounts:
model = weight_pruner.prune_and_fine_tune(model, el, 1, x_train, y_train, x_validation, y_validation)
test_loss, test_acc2 = model.evaluate(test_images, test_labels)
print('\nTest accuracy:', test_acc2)
test_accuracy_list.append(test_acc2)
check_ratio_of_zeros(0.04, model)
print(test_accuracy_list)
pruning_amounts = [0.0, 0.25, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.97, 0.99]