MilesCranmer commited on
Commit
5a4ff06
1 Parent(s): 20ec2db

Add file for printing best model

Browse files
Files changed (1) hide show
  1. benchmarks/print_best_model.py +91 -0
benchmarks/print_best_model.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Print the best model parameters and loss"""
2
+ import sys
3
+ import numpy as np
4
+ import pickle as pkl
5
+ import hyperopt
6
+ from hyperopt import hp, fmin, tpe, Trials
7
+
8
+
9
+ #Change the following code to your file
10
+ ################################################################################
11
+ # TODO: Declare a folder to hold all trials objects
12
+ TRIALS_FOLDER = 'trials2'
13
+ ################################################################################
14
+
15
+ def merge_trials(trials1, trials2_slice):
16
+ """Merge two hyperopt trials objects
17
+
18
+ :trials1: The primary trials object
19
+ :trials2_slice: A slice of the trials object to be merged,
20
+ obtained with, e.g., trials2.trials[:10]
21
+ :returns: The merged trials object
22
+
23
+ """
24
+ max_tid = 0
25
+ if len(trials1.trials) > 0:
26
+ max_tid = max([trial['tid'] for trial in trials1.trials])
27
+
28
+ for trial in trials2_slice:
29
+ tid = trial['tid'] + max_tid + 1
30
+ hyperopt_trial = Trials().new_trial_docs(
31
+ tids=[None],
32
+ specs=[None],
33
+ results=[None],
34
+ miscs=[None])
35
+ hyperopt_trial[0] = trial
36
+ hyperopt_trial[0]['tid'] = tid
37
+ hyperopt_trial[0]['misc']['tid'] = tid
38
+ for key in hyperopt_trial[0]['misc']['idxs'].keys():
39
+ hyperopt_trial[0]['misc']['idxs'][key] = [tid]
40
+ trials1.insert_trial_docs(hyperopt_trial)
41
+ trials1.refresh()
42
+ return trials1
43
+
44
+ np.random.seed()
45
+
46
+ # Load up all runs:
47
+ import glob
48
+ path = TRIALS_FOLDER + '/*.pkl'
49
+ files = 0
50
+ for fname in glob.glob(path):
51
+
52
+ trials_obj = pkl.load(open(fname, 'rb'))
53
+ n_trials = trials_obj['n']
54
+ trials_obj = trials_obj['trials']
55
+ if files == 0:
56
+ trials = trials_obj
57
+ else:
58
+ trials = merge_trials(trials, trials_obj.trials[-n_trials:])
59
+ files += 1
60
+
61
+
62
+ print(files, 'trials merged')
63
+
64
+
65
+ best_loss = np.inf
66
+ best_trial = None
67
+ try:
68
+ trials
69
+ except NameError:
70
+ raise NameError("No trials loaded. Be sure to set the right folder")
71
+
72
+ # for trial in trials:
73
+ # if trial['result']['status'] == 'ok':
74
+ # loss = trial['result']['loss']
75
+ # if loss < best_loss:
76
+ # best_loss = loss
77
+ # best_trial = trial
78
+
79
+ # print(best_loss, best_trial['misc']['vals'])
80
+ #trials = sorted(trials, key=lambda x: (x['result']['loss'] if trials['result']['status'] == 'ok' else float('inf')))
81
+
82
+ clean_trials = []
83
+ for trial in trials:
84
+ clean_trials.append((trial['result']['loss'], trial['misc']['vals']))
85
+
86
+ clean_trials = sorted(clean_trials, key=lambda x: x[0])
87
+
88
+ for trial in clean_trials:
89
+ print(trial)
90
+
91
+