-
Notifications
You must be signed in to change notification settings - Fork 8
/
tune.py
executable file
·188 lines (173 loc) · 6.39 KB
/
tune.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
import os
import sys
import json
import keras
import matchzoo as mz
from flask import request, Blueprint, jsonify
from utils import json_write, IOpool, LogDir
ROOT_PATH = sys.path[0] + '/'
main = Blueprint('tune', __name__)
def test():
train_raw = mz.datasets.toy.load_data('train')
dev_raw = mz.datasets.toy.load_data('dev')
preprocessor = mz.preprocessors.BasicPreprocessor(fixed_length_left=10,
fixed_length_right=40,
remove_stop_words=True)
train = preprocessor.fit_transform(train_raw, verbose=0)
dev = preprocessor.transform(dev_raw, verbose=0)
model = mz.models.MatchPyramid()
model.params['input_shapes'] = preprocessor.context['input_shapes']
model.params['task'] = mz.tasks.Ranking()
model.guess_and_fill_missing_params()
tuner = mz.auto.Tuner(
params=model.params,
train_data=train,
test_data=dev,
num_runs=5
)
results = tuner.tune()
def tune_api(qpool, logdir, dataset_path, train_id, parameter, epochs):
keras.backend.clear_session()
# 重定向输出
logdir.set_preprocess_id(train_id)
old = sys.stdout
sys.stdout = logdir
# 处理逻辑
train_raw = mz.datasets.toy.load_data('train')
dev_raw = mz.datasets.toy.load_data('dev')
preprocessor = mz.models.DenseBaseline.get_default_preprocessor()
train = preprocessor.fit_transform(train_raw, verbose=0)
dev = preprocessor.transform(dev_raw, verbose=0)
model_mapping = {
'ARCI': mz.models.ArcI,
'ARCII': mz.models.ArcII,
'DSSM': mz.models.DSSM,
'DRMM': mz.models.DRMMTKS,
'CDSSM': mz.models.CDSSM,
'MVLSTM': mz.models.MVLSTM,
'DUET': mz.models.DUET,
'KNRM': mz.models.KNRM,
'CONVKNRM': mz.models.ConvKNRM
}
for ks in parameter:
if parameter[ks]['chosen'] is True:
print('======' + ks + '======')
model = model_mapping[ks]()
model.params['input_shapes'] = preprocessor.context['input_shapes']
model.params['task'] = mz.tasks.Ranking()
model.guess_and_fill_missing_params()
param = parameter[ks]
print(param)
settings = {}
for ks, vs in param.items():
if ks == 'chosen':
continue
elif ks == 'optimizer':
print(vs)
model.params.get('optimizer').hyper_space = mz.hyper_spaces.choice(vs)
continue
lpos = ks.find('_low')
hpos = ks.find('_high')
if lpos != -1:
param_name = ks[:lpos]
if settings.get(param_name) is None:
settings[param_name] = [vs]
else:
settings[param_name].append(vs)
else:
param_name = ks[:hpos]
if settings.get(param_name) is None:
settings[param_name] = [vs]
else:
settings[param_name].append(vs)
for ks, vs in settings.items():
vs.sort()
# print(vs)
if ks == 'dropout_rate':
step = 0.1
elif ks == 'sigma':
step = 0.01
else:
step = 1
model.params.get(ks).hyper_space = mz.hyper_spaces.quniform(low=vs[0], high=vs[1], q=step)
tuner = mz.auto.Tuner(
params=model.params,
train_data=train,
test_data=dev,
num_runs=epochs
)
results = tuner.tune()
print('$$$finished$$$')
# 还原输出
sys.stderr = old
@main.route('/query', methods=['POST'])
def tune_query():
request_data = json.loads(request.data.decode('utf-8'))
train_id = request_data['tune_id']
logger_name = os.path.join(ROOT_PATH, 'matchzoo_temp_files/logger/', train_id + '.preprocess_log')
response = {
'score': 0,
'chart': [],
'info': {},
'state': 'run',
'update': False
}
cur_model = ''
epoch = -1
best_score = request_data['best_score']
keep_record = False
with open(logger_name) as f:
for line in f:
if len(line) <= 1:
continue
if line[0:6] == '======':
keep_record = False
cur_model = line.split('======')[1]
response['chart'].append([])
elif line[0:5] == 'Run #':
keep_record = False
epoch = int(line.split('Run #')[1])
elif line[0:7] == 'Score: ':
score = float(line.split('Score: ')[1])
if score > best_score:
best_score = score
keep_record = True
response['update'] = True
response['chart'][-1].append({'x': epoch, 'y': score, 'm': cur_model})
elif line == '$$$finished$$$\n':
keep_record = False
response['state'] = 'end'
if keep_record is True:
ssline = line.split()
if len(ssline) == 2:
ks, vs = ssline[0], ssline[1]
else:
ks = ssline[0]
vs = ' '.join(ssline[1:])
response['info'][ks] = vs
return jsonify(response)
@main.route('/head', methods=['POST'])
def tune():
request_data = json.loads(request.data.decode('utf-8'))
train_id = request_data['train_id']
parameter = request_data['parameter']
epochs = request_data['epochs']
file_name = ROOT_PATH + 'matchzoo_temp_files/data/' + train_id + '.json'
dataset_path = ROOT_PATH + 'matchzoo_temp_files/files/' + train_id + '.train'
init_dict = {
'state': 'run',
'data': {
'loss': [[]],
'accuracy': [[], [], []]
}
}
if not os.path.exists(file_name):
json_write(file_name, init_dict)
with open(ROOT_PATH + 'matchzoo_temp_files/logger/' + train_id + '.preprocess_log', 'w') as f:
f.write('')
qpool = IOpool()
logdir = LogDir()
tune_api(qpool, logdir, dataset_path, train_id, parameter, epochs)
return jsonify({'status': 'ok'})
if __name__ == '__main__':
test()