File size: 2,974 Bytes
5a4ff06
 
 
 
 
 
4f5f994
2271609
5a4ff06
 
3fb2dca
5a4ff06
 
3fb2dca
5a4ff06
 
3fb2dca
5a4ff06
 
 
 
 
 
 
 
 
 
 
3fb2dca
5a4ff06
 
3fb2dca
5a4ff06
3fb2dca
 
5a4ff06
3fb2dca
 
 
 
 
5a4ff06
 
 
3fb2dca
5a4ff06
 
 
 
3fb2dca
 
5a4ff06
 
 
3fb2dca
 
 
 
5a4ff06
 
 
 
 
 
3fb2dca
5a4ff06
 
 
 
 
 
 
 
 
 
3fb2dca
 
 
 
 
5a4ff06
 
3fb2dca
5a4ff06
 
 
3fb2dca
5a4ff06
 
 
2271609
 
5a4ff06
4f5f994
 
 
 
 
 
 
 
 
 
 
 
 
2271609
4f5f994
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
"""Print the best model parameters and loss"""
import sys
import numpy as np
import pickle as pkl
import hyperopt
from hyperopt import hp, fmin, tpe, Trials
from space import space
from pprint import PrettyPrinter


# Change the following code to your file
################################################################################
# TODO: Declare a folder to hold all trials objects
TRIALS_FOLDER = "trials2"
################################################################################


def merge_trials(trials1, trials2_slice):
    """Merge two hyperopt trials objects

    :trials1: The primary trials object
    :trials2_slice: A slice of the trials object to be merged,
        obtained with, e.g., trials2.trials[:10]
    :returns: The merged trials object

    """
    max_tid = 0
    if len(trials1.trials) > 0:
        max_tid = max([trial["tid"] for trial in trials1.trials])

    for trial in trials2_slice:
        tid = trial["tid"] + max_tid + 1
        hyperopt_trial = Trials().new_trial_docs(
            tids=[None], specs=[None], results=[None], miscs=[None]
        )
        hyperopt_trial[0] = trial
        hyperopt_trial[0]["tid"] = tid
        hyperopt_trial[0]["misc"]["tid"] = tid
        for key in hyperopt_trial[0]["misc"]["idxs"].keys():
            hyperopt_trial[0]["misc"]["idxs"][key] = [tid]
        trials1.insert_trial_docs(hyperopt_trial)
        trials1.refresh()
    return trials1


np.random.seed()

# Load up all runs:
import glob

path = TRIALS_FOLDER + "/*.pkl"
files = 0
for fname in glob.glob(path):

    trials_obj = pkl.load(open(fname, "rb"))
    n_trials = trials_obj["n"]
    trials_obj = trials_obj["trials"]
    if files == 0:
        trials = trials_obj
    else:
        trials = merge_trials(trials, trials_obj.trials[-n_trials:])
    files += 1


print(files, "trials merged")


best_loss = np.inf
best_trial = None
try:
    trials
except NameError:
    raise NameError("No trials loaded. Be sure to set the right folder")

# for trial in trials:
# if trial['result']['status'] == 'ok':
# loss = trial['result']['loss']
# if loss < best_loss:
# best_loss = loss
# best_trial = trial

# print(best_loss, best_trial['misc']['vals'])
# trials = sorted(trials, key=lambda x: (x['result']['loss'] if trials['result']['status'] == 'ok' else float('inf')))

clean_trials = []
for trial in trials:
    clean_trials.append((trial["result"]["loss"], trial["misc"]["vals"]))

clean_trials = sorted(clean_trials, key=lambda x: x[0])

pp = PrettyPrinter(indent=4)

for trial in clean_trials:
    loss, params = trial
    for k, value in params.items():
        value = value[0]
        if isinstance(value, int):
            possible_args = space[k].pos_args[1:]
            try:
                value = possible_args[value].obj
            except AttributeError:
                value = [arg.obj for arg in possible_args[value].pos_args]


        params[k] = value

    pp.pprint({"loss": loss, "params": params})