|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
import os |
|
6
|
|
|
import json |
|
7
|
|
|
import shutil |
|
8
|
|
|
import hashlib |
|
9
|
|
|
import inspect |
|
10
|
|
|
|
|
11
|
|
|
import numpy as np |
|
12
|
|
|
|
|
13
|
|
|
from .memory_load import MemoryLoad |
|
14
|
|
|
from .memory_dump import MemoryDump |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class Memory: |
|
18
|
|
|
def __init__(self): |
|
19
|
|
|
current_path = os.path.realpath(__file__) |
|
20
|
|
|
self.meta_learn_path, _ = current_path.rsplit("/", 1) |
|
21
|
|
|
self.meta_path = self.meta_learn_path + "/meta_data/" |
|
22
|
|
|
|
|
23
|
|
|
def get_best_models(self, X, y): |
|
24
|
|
|
# TODO: model_dict key:model value:score |
|
25
|
|
|
|
|
26
|
|
|
return model_dict |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def get_model_search_config(self, model): |
|
29
|
|
|
# TODO |
|
30
|
|
|
return search_config |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
def get_model_init_config(self, model): |
|
33
|
|
|
# TODO |
|
34
|
|
|
return init_config |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def delete_model(self, model): |
|
37
|
|
|
model_hash = self._get_model_hash(model) |
|
38
|
|
|
shutil.rmtree(self.meta_path + str(model_hash)) |
|
39
|
|
|
|
|
40
|
|
|
def delete_model_dataset(self, model, X, y): |
|
41
|
|
|
self.func_path_ = self._get_model_hash(model) + "/" |
|
42
|
|
|
self.func_path = self.meta_path + self.func_path_ |
|
43
|
|
|
|
|
44
|
|
|
self.feature_hash = self._get_hash(X) |
|
45
|
|
|
self.label_hash = self._get_hash(y) |
|
46
|
|
|
|
|
47
|
|
|
csv_file = self._get_file_path() |
|
48
|
|
|
os.remove(csv_file) |
|
49
|
|
|
|
|
50
|
|
|
def merge_model_hashes(self, model1, model2): |
|
51
|
|
|
# do checks if search space has same dim |
|
52
|
|
|
|
|
53
|
|
|
with open(self.meta_path + 'model_connections.json') as f: |
|
54
|
|
|
data = json.load(f) |
|
55
|
|
|
|
|
56
|
|
|
model1_hash = self._get_model_hash(model1) |
|
57
|
|
|
model2_hash = self._get_model_hash(model2) |
|
58
|
|
|
|
|
59
|
|
|
models_dict = {str(model1_hash): str(model2_hash)} |
|
60
|
|
|
data.update(models_dict) |
|
61
|
|
|
|
|
62
|
|
|
with open(self.meta_path + 'model_connections.json', 'w') as f: |
|
63
|
|
|
json.dump(data, f) |
|
64
|
|
|
|
|
65
|
|
|
def split_model_hashes(self, model1, model2): |
|
66
|
|
|
# TODO: do checks if search space has same dim |
|
67
|
|
|
|
|
68
|
|
|
with open(self.meta_path + 'model_connections.json') as f: |
|
69
|
|
|
data = json.load(f) |
|
70
|
|
|
|
|
71
|
|
|
model1_hash = self._get_model_hash(model1) |
|
72
|
|
|
model2_hash = self._get_model_hash(model2) |
|
73
|
|
|
|
|
74
|
|
|
if model1_hash in data.keys(): |
|
75
|
|
|
del data[model1_hash] |
|
76
|
|
|
if model2_hash in data.keys(): |
|
77
|
|
|
del data[model2_hash] |
|
78
|
|
|
|
|
79
|
|
|
with open(self.meta_path + 'model_connections.json', 'w') as f: |
|
80
|
|
|
json.dump(data, f) |
|
81
|
|
|
|
|
82
|
|
|
def _get_model_hash(self, model): |
|
83
|
|
|
return self._get_hash(self._get_func_str(model).encode("utf-8")) |
|
84
|
|
|
|
|
85
|
|
|
def _get_file_path(self): |
|
86
|
|
|
if not os.path.exists(self.date_path): |
|
87
|
|
|
os.makedirs(self.date_path) |
|
88
|
|
|
|
|
89
|
|
|
return self.func_path + (self.feature_hash + "_" + self.label_hash + "_.csv") |
|
90
|
|
|
|
|
91
|
|
|
def _get_func_str(self, func): |
|
92
|
|
|
return inspect.getsource(func) |
|
93
|
|
|
|
|
94
|
|
|
def _get_hash(self, object): |
|
95
|
|
|
return hashlib.sha1(object).hexdigest() |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
class BaseMemory: |
|
99
|
|
|
def __init__(self, _space_, _main_args_, _cand_): |
|
100
|
|
|
self._space_ = _space_ |
|
101
|
|
|
self._main_args_ = _main_args_ |
|
102
|
|
|
|
|
103
|
|
|
self.pos_best = None |
|
104
|
|
|
self.score_best = -np.inf |
|
105
|
|
|
|
|
106
|
|
|
self.memory_type = _main_args_.memory |
|
107
|
|
|
self.memory_dict = {} |
|
108
|
|
|
|
|
109
|
|
|
self.meta_data_found = False |
|
110
|
|
|
|
|
111
|
|
|
self.n_dims = None |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
class ShortTermMemory(BaseMemory): |
|
115
|
|
|
def __init__(self, _space_, _main_args_, _cand_): |
|
116
|
|
|
super().__init__(_space_, _main_args_, _cand_) |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
class LongTermMemory(BaseMemory): |
|
120
|
|
|
def __init__(self, _space_, _main_args_, _cand_): |
|
121
|
|
|
super().__init__(_space_, _main_args_, _cand_) |
|
122
|
|
|
|
|
123
|
|
|
self._load_ = MemoryLoad(_space_, _main_args_, _cand_) |
|
124
|
|
|
self._dump_ = MemoryDump(_space_, _main_args_, _cand_) |
|
125
|
|
|
|
|
126
|
|
|
def load_memory(self, _cand_, _verb_): |
|
127
|
|
|
self.memory_dict = self._load_._load_memory(_cand_, _verb_, self.memory_dict) |
|
128
|
|
|
|
|
129
|
|
|
def save_memory(self, _main_args_, _opt_args_, _cand_): |
|
130
|
|
|
self._dump_._save_memory(_main_args_, _opt_args_, _cand_, self.memory_dict) |
|
131
|
|
|
|
|
132
|
|
|
def _get_hash(self, object): |
|
133
|
|
|
return hashlib.sha1(object).hexdigest() |
|
134
|
|
|
|
|
135
|
|
|
def _get_func_str(self, func): |
|
136
|
|
|
return inspect.getsource(func) |
|
137
|
|
|
|
|
138
|
|
|
def _obj2hash(self): |
|
139
|
|
|
obj2hash_dict = {} |
|
140
|
|
|
para_hash_list = self._get_para_hash_list() |
|
141
|
|
|
|
|
142
|
|
|
for para_hash in para_hash_list: |
|
143
|
|
|
obj = self._read_dill(para_hash) |
|
144
|
|
|
obj2hash_dict[para_hash] = obj |
|
145
|
|
|
|
|
146
|
|
|
return obj2hash_dict |
|
147
|
|
|
|