-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlearning_rate.py
233 lines (183 loc) · 6.87 KB
/
learning_rate.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""Calculate data for learning rate calculation.
Taking data from sampling_learning_rate.py
Output may be processed by plot_learning_rate.py
"""
# @Author: Joey Teng
# @Email: joey.teng.dev@gmail.com
# @Filename: learning_rate.py
# @Last modified by: Joey Teng
# @Last modified time: 28-Apr-2018
import argparse
import collections
import copy
import json
import multiprocessing.pool
import os
import random
import numpy
import sklearn.ensemble
import sklearn.tree
NUMBER_OF_PERCENTAGES = 100
NUMBER_OF_TRAINING_SETS = 10 # Folds
PROCESS_COUNT = int(os.cpu_count() / 2)
def split_data_target(dataset):
"""Split the input CSV files into X, y vectors for sklearn implementations.
Args:
dataset (list): List of list of floats.
[
[0...n - 1]: X, feature vector
[-1]: y, label
]
Returns:
tuple: (X, y) for sklearn implementations
"""
try:
return ([[float(element)
for element in row.strip().split(',')[:-1]]
for row in dataset],
[float(row.strip().split(',')[-1])
for row in dataset])
except ValueError:
print("dataset {}".format(dataset))
raise ValueError
def generate_training_sets(dataset, percentage, copies):
"""Resample from separated training sets to generate smaller training sets.
No instance will present in one new training set more than once.
Mechanism is to shuffle, then pick the first percentage% instances.
Args:
dataset (list): List of vectors (features + label)
percentage (number that supports __mul__ and __floordiv__):
This decides the size of new training set generated related to the
population.
copies (int): The number of new training sets required.
Returns:
list: list of new training datasets
list of list of vectors
"""
training_sets = []
i = copies
while i > 0:
population = copy.deepcopy(dataset)
random.shuffle(population)
training_sets.append(population[:len(population) * percentage // 100])
i -= 1
return training_sets
def generate_result(datasets, classifier, path):
"""Generate the learning rate accuracies.
Args:
datasets (dict): {
'test set': testing set for the specific dataset
'remainder': instances in the dataset but not testing set
}
classifier (func): a function that will return an instance of
sklearn classifier.
path (str): path of the dataset, for logging only.
Returns:
dict: dict of dict {
percentage: results under respective portion of training data {
'raw' (list): raw accuracy values [
accuracy values of each training set-testing set pairs
]
'average': average of 'raw'
'standard deviation': standard deviation of 'raw'
'range': range of 'raw'
}
}
"""
results = []
for dataset in datasets:
test_set = dataset['test set']
result = collections.defaultdict(dict)
for percentage in range(
100 // NUMBER_OF_PERCENTAGES,
100 + 100 // NUMBER_OF_PERCENTAGES,
100 // NUMBER_OF_PERCENTAGES):
if (percentage == 100):
value = [copy.deepcopy(dataset['remainder'])]
else:
value = generate_training_sets(
dataset['remainder'],
percentage,
NUMBER_OF_TRAINING_SETS)
print("{} Running on {}%. Testing set count: {}/{}".format(
path, percentage, (len(results) + 1), len(datasets)),
flush=True)
result[percentage]['raw'] = []
for training_set in value:
clf = classifier()
data, target = split_data_target(training_set)
if data:
clf.fit(data, target)
data, target = split_data_target(test_set)
accuracy = clf.score(data, target)
else:
accuracy = 0.0
result[percentage]['raw'].append(accuracy)
result[percentage]['average'] = numpy.average(
result[percentage]['raw'])
result[percentage]['standard deviation'] = numpy.std(
result[percentage]['raw'])
result[percentage]['range'] = max(
result[percentage]['raw']) - min(result[percentage]['raw'])
results.append(result)
return results
def RandomForestClassifier():
"""Wrap a default Random Forest classifier with fixed parameter."""
return sklearn.ensemble.RandomForestClassifier(n_estimators=64)
def main(path):
"""Start main function here.
Run tasks and dump result files.
"""
print("{} Start".format(path), flush=True)
datasets = json.load(open(path, 'r'))
results = {}
print("{} Evaluating Random Forest".format(path), flush=True)
results['Random Forest'] = generate_result(
datasets, RandomForestClassifier, path)
json.dump(results, open(
"{}.result.json".format(path[:-len('.json')]), 'w'))
print("{} Complete".format(path), flush=True)
def traverse(paths):
"""Travsere to append all files in children folders into the task queue.
Args:
paths (list): Paths of all folders to be detected
Returns:
list: Paths of all files added in the task queue
"""
print("Starting Traverse Through", flush=True)
files = []
while paths:
path = paths[0]
paths = paths[1:]
for file in os.listdir(path):
if (file.endswith('.learning_rate.json')):
files.append('{0}/{1}'.format(path, file))
elif os.path.isdir('{0}/{1}'.format(path, file)):
paths.append('{0}/{1}'.format(path, file))
print("Traverse Completed.", flush=True)
return files
def parse_path():
"""Parse the arguments.
No argument is required for calling this function.
Returns:
Namespace: parsed arguments enclosed by an object defined in argparse
"""
parser = argparse.ArgumentParser(
description="Generate Datasets for Detecting Learning Rate")
parser.add_argument('-r', action='store', nargs='+', default=[],
help='Recursively processing all files in the folder')
parser.add_argument('-i', action='store', nargs='+', default=[],
help='Files that need to be processed')
args = parser.parse_args()
paths = []
if (args.r):
paths = traverse(args.r)
paths.extend(args.i)
paths.sort()
return paths
if __name__ == '__main__':
paths = parse_path()
pool = multiprocessing.pool.Pool(PROCESS_COUNT)
list(pool.map(main, paths))
pool.close()
pool.join()