|
6 | 6 | import numpy as np |
7 | 7 | from itertools import imap |
8 | 8 |
|
| 9 | +from hyperopt import hp, STATUS_OK |
| 10 | +from hyperas.distributions import conditional |
| 11 | + |
9 | 12 | import time |
10 | 13 | import sys |
11 | 14 | import os |
|
17 | 20 | from plasma.utils.performance import PerformanceAnalyzer |
18 | 21 | from plasma.utils.evaluation import * |
19 | 22 |
|
20 | | - |
21 | 23 | def train(conf,shot_list_train,loader): |
22 | 24 |
|
23 | 25 | np.random.seed(1) |
@@ -99,8 +101,102 @@ def train(conf,shot_list_train,loader): |
99 | 101 | plot_losses(conf,[training_losses,validation_losses,validation_roc],specific_builder,name='training_validation_roc') |
100 | 102 | print('...done') |
101 | 103 |
|
102 | | - |
103 | | - |
| 104 | +class HyperRunner(object): |
| 105 | + def __init__(self,conf,loader,shot_list): |
| 106 | + self.loader = loader |
| 107 | + self.shot_list = shot_list |
| 108 | + self.conf = conf |
| 109 | + |
| 110 | + #FIXME setup for hyperas search |
| 111 | + def keras_fmin_fnct(self,space): |
| 112 | + from plasma.models import builder |
| 113 | + |
| 114 | + specific_builder = builder.ModelBuilder(self.conf) |
| 115 | + |
| 116 | + train_model, test_model = specific_builder.hyper_build_model(space,False), specific_builder.hyper_build_model(space,True) |
| 117 | + |
| 118 | + np.random.seed(1) |
| 119 | + validation_losses = [] |
| 120 | + validation_roc = [] |
| 121 | + training_losses = [] |
| 122 | + shot_list_train,shot_list_validate = self.shot_list.split_direct(1.0-conf['training']['validation_frac'],do_shuffle=True) |
| 123 | + os.environ['THEANO_FLAGS'] = 'device=gpu,floatX=float32' |
| 124 | + import theano |
| 125 | + from keras.utils.generic_utils import Progbar |
| 126 | + from keras import backend as K |
| 127 | + |
| 128 | + |
| 129 | + num_epochs = self.conf['training']['num_epochs'] |
| 130 | + num_at_once = self.conf['training']['num_shots_at_once'] |
| 131 | + lr_decay = self.conf['model']['lr_decay'] |
| 132 | + lr = self.conf['model']['lr'] |
| 133 | + |
| 134 | + resulting_dict = {'loss':None,'status':STATUS_OK,'model':None} |
| 135 | + |
| 136 | + e = -1 |
| 137 | + #print("Current num_epochs {}".format(e)) |
| 138 | + while e < num_epochs-1: |
| 139 | + e += 1 |
| 140 | + pbar = Progbar(len(shot_list_train)) |
| 141 | + |
| 142 | + shot_list_train.shuffle() |
| 143 | + shot_sublists = shot_list_train.sublists(num_at_once)[:1] |
| 144 | + training_losses_tmp = [] |
| 145 | + |
| 146 | + K.set_value(train_model.optimizer.lr, lr*lr_decay**(e)) |
| 147 | + for (i,shot_sublist) in enumerate(shot_sublists): |
| 148 | + X_list,y_list = self.loader.load_as_X_y_list(shot_sublist) |
| 149 | + for j,(X,y) in enumerate(zip(X_list,y_list)): |
| 150 | + history = builder.LossHistory() |
| 151 | + train_model.fit(X,y, |
| 152 | + batch_size=Loader.get_batch_size(self.conf['training']['batch_size'],prediction_mode=False), |
| 153 | + nb_epoch=1,shuffle=False,verbose=0, |
| 154 | + validation_split=0.0,callbacks=[history]) |
| 155 | + train_model.reset_states() |
| 156 | + train_loss = np.mean(history.losses) |
| 157 | + training_losses_tmp.append(train_loss) |
| 158 | + |
| 159 | + pbar.add(1.0*len(shot_sublist)/len(X_list), values=[("train loss", train_loss)]) |
| 160 | + self.loader.verbose=False |
| 161 | + sys.stdout.flush() |
| 162 | + training_losses.append(np.mean(training_losses_tmp)) |
| 163 | + specific_builder.save_model_weights(train_model,e) |
| 164 | + |
| 165 | + roc_area,loss = make_predictions_and_evaluate_gpu(self.conf,shot_list_validate,self.loader) |
| 166 | + print("Epoch: {}, loss: {}, validation_losses_size: {}".format(e,loss,len(validation_losses))) |
| 167 | + validation_losses.append(loss) |
| 168 | + validation_roc.append(roc_area) |
| 169 | + resulting_dict['loss'] = loss |
| 170 | + resulting_dict['model'] = train_model |
| 171 | + #print("Results {}, before {}".format(resulting_dict,id(resulting_dict))) |
| 172 | + |
| 173 | + #print("Results {}, after {}".format(resulting_dict,id(resulting_dict))) |
| 174 | + return resulting_dict |
| 175 | + |
| 176 | + def get_space(self): |
| 177 | + return { |
| 178 | + 'Dropout': hp.uniform('Dropout', 0, 1), |
| 179 | + } |
| 180 | + |
| 181 | + def frnn_minimize(self, algo, max_evals, trials, rseed=1337): |
| 182 | + from hyperopt import fmin |
| 183 | + |
| 184 | + best_run = fmin(self.keras_fmin_fnct, |
| 185 | + space=self.get_space(), |
| 186 | + algo=algo, |
| 187 | + max_evals=max_evals, |
| 188 | + trials=trials, |
| 189 | + rstate=np.random.RandomState(rseed)) |
| 190 | + |
| 191 | + best_model = None |
| 192 | + for trial in trials: |
| 193 | + vals = trial.get('misc').get('vals') |
| 194 | + for key in vals.keys(): |
| 195 | + vals[key] = vals[key][0] |
| 196 | + if trial.get('misc').get('vals') == best_run and 'model' in trial.get('result').keys(): |
| 197 | + best_model = trial.get('result').get('model') |
| 198 | + |
| 199 | + return best_run, best_model |
104 | 200 |
|
105 | 201 | def plot_losses(conf,losses_list,specific_builder,name=''): |
106 | 202 | unique_id = specific_builder.get_unique_id() |
@@ -140,7 +236,8 @@ def make_predictions(conf,shot_list,loader): |
140 | 236 | model_save_path = specific_builder.get_latest_save_path() |
141 | 237 |
|
142 | 238 | start_time = time.time() |
143 | | - pool = mp.Pool() |
| 239 | + use_cores = max(1,mp.cpu_count()-2) |
| 240 | + pool = mp.Pool(use_cores) |
144 | 241 | fn = partial(make_single_prediction,builder=specific_builder,loader=loader,model_save_path=model_save_path) |
145 | 242 |
|
146 | 243 | print('running in parallel on {} processes'.format(pool._processes)) |
|
0 commit comments