-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_experiment.py
220 lines (202 loc) · 10.3 KB
/
main_experiment.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import json
import os
import argparse
import numpy as np
from hpo.optimizers.qt_metadataset import QuickTuneMetaDataset
from hpo.optimizers.quick_tune.cost_metalearner import CostMetaLearner
from hpo.optimizers.quick_tune.factory import create_qt_optimizer, SPLITS
from hpo.optimizers.bo import BO
from hpo.optimizers.random_search import RandomSearchOptimizer
if __name__ == "__main__":
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("--max_budget", type=int, default=50)
parser.add_argument("--seed", type=int, default=100)
parser.add_argument("--budget_limit", type=int, default=10000)
parser.add_argument("--output_dir", type=str, default="output/")
parser.add_argument("--aggregate_data", type=int, default=0)
parser.add_argument("--metadataset_version", type=str, default="micro")
parser.add_argument("--hidden_dim", type=int, default=64)
parser.add_argument("--output_dim", type=int, default=32)
parser.add_argument("--learning_rate", type=float, default=0.001)
parser.add_argument("--meta_learning_rate", type=float, default=0.01)
parser.add_argument("--train_iter", type=int, default=10000)
parser.add_argument("--device", type=str, default="cuda")
parser.add_argument("--with_scheduler", type=int, default=0)
parser.add_argument("--include_metafeatures", type=int, default=1)
parser.add_argument("--acqf_fc", type=str, default="ei")
parser.add_argument("--explore_factor", type=float, default=0.1)
parser.add_argument("--experiment_id", type=str, default="")
parser.add_argument("--meta_test_id", type=str, default="mt0")
parser.add_argument("--load_meta_trained", type=int, default=0)
parser.add_argument("--load_cost_predictor", type=int, default=0)
parser.add_argument("--output_dim_metafeatures", type=int, default=2)
parser.add_argument("--freeze_feature_extractor", type=int, default=0)
parser.add_argument("--run_random", type=int, default=0)
parser.add_argument("--meta_train", type=int, default=1)
parser.add_argument("--load_only_dataset_descriptors", type=int, default=1)
parser.add_argument("--cost_aware", type=int, default=0)
parser.add_argument("--use_encoders_for_model", type=int, default=0)
parser.add_argument("--observe_cost", type=int, default=0)
parser.add_argument("--target_model", type=str, default=None)
parser.add_argument("--test_generalization_to_model", type=int, default=0)
parser.add_argument("--use_only_target_model", type=int, default=0)
parser.add_argument("--split_id", type=int, default=None)
parser.add_argument("--dataset_id_in_split", type=str, default=None)
parser.add_argument("--conditioned_time_limit", type=int, default=0)
parser.add_argument("--subsample_models_in_hub", type=int, default=None)
parser.add_argument("--measure_for_target_model", type=str, default=None)
parser.add_argument("--file_with_init_indices", type=str, default=None)
args = parser.parse_args()
print(args)
load_meta_trained = args.load_meta_trained
load_cost_predictor = args.load_cost_predictor
results = {}
rootdir = os.path.dirname(os.path.abspath(__file__))
output_dir = os.path.join(rootdir, args.output_dir, args.experiment_id)
if args.file_with_init_indices is not None:
args.file_with_init_indices = os.path.join(
rootdir, "hpo", "meta_data", args.file_with_init_indices
)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
metadataset = QuickTuneMetaDataset(
aggregate_data=args.aggregate_data,
path="data",
version=args.metadataset_version,
load_only_dataset_descriptors=args.load_only_dataset_descriptors,
)
if not args.meta_train:
args.output_dim_metafeatures = 0
# not used for now
augmentation_ids = [None]
if args.split_id is not None:
split_ids = [args.split_id]
else:
split_ids = np.arange(0, 5)
for split_id in split_ids:
load_meta_trained = args.load_meta_trained
load_cost_predictor = args.load_cost_predictor
datasets = metadataset.get_datasets()
train_splits, test_splits, val_splits = SPLITS[split_id]
datasets_for_split = CostMetaLearner.get_splits(
None,
datasets,
train_splits=train_splits,
test_splits=test_splits,
val_splits=val_splits,
)
if args.dataset_id_in_split is not None:
datasets = [datasets_for_split["test"][int(args.dataset_id_in_split)]]
else:
datasets = datasets_for_split["test"]
for dataset_name in datasets:
for augmentation_id in augmentation_ids:
try:
metadataset.set_dataset_name(
dataset_name, augmentation_id=augmentation_id
)
hyperparameter_candidates = (
metadataset.get_hyperparameters_candidates().values.tolist()
)
log_indicator = [
False for _ in range(len(hyperparameter_candidates[0]))
] # Dont apply log
new_dataset_name = dataset_name.replace("/", "_")
if augmentation_id is not None:
new_dataset_name = (
new_dataset_name + "_aug" + str(augmentation_id)
)
if args.measure_for_target_model is not None:
with open(
os.path.join(
rootdir, "meta_data", "best_models_per_dataset.json"
),
"r",
) as f:
best_models_per_datasets = json.load(f)
target_model = best_models_per_datasets[dataset_name][
args.measure_for_target_model
]
else:
target_model = args.target_model
if args.conditioned_time_limit:
with open(
os.path.join(rootdir, "meta_data", "time_counts.json"), "r"
) as f:
time_limits = json.load(f)
budget_limit = time_limits[args.metadataset_version][new_dataset_name] + 100
else:
budget_limit = args.budget_limit
temp_output_dir = os.path.join(
output_dir, args.meta_test_id, new_dataset_name
)
if not os.path.exists(temp_output_dir):
os.makedirs(temp_output_dir)
if args.run_random:
random_search = RandomSearchOptimizer(
metadataset, seed=args.seed
)
optimizer_budget, optimizer_cost, optimizer_performance = BO(
random_search, metadataset, args.budget_limit
)
results = [
optimizer_budget,
optimizer_cost,
optimizer_performance,
]
else:
optimizer = create_qt_optimizer(
metadataset,
experiment_id=args.experiment_id,
output_dim_metafeatures=args.output_dim_metafeatures,
freeze_feature_extractor=args.freeze_feature_extractor,
explore_factor=args.explore_factor,
load_meta_trained=load_meta_trained,
meta_output_dir=output_dir,
output_dir=temp_output_dir,
dataset_name=dataset_name,
new_dataset_name=new_dataset_name,
log_indicator=log_indicator,
budget_limit=budget_limit,
include_metafeatures=args.include_metafeatures,
meta_train=args.meta_train,
acqf_fc=args.acqf_fc,
learning_rate=args.learning_rate,
meta_learning_rate=args.meta_learning_rate,
train_iter=args.train_iter,
hidden_dim=args.hidden_dim,
output_dim=args.output_dim,
with_scheduler=args.with_scheduler,
cost_aware=args.cost_aware,
use_encoders_for_model=args.use_encoders_for_model,
load_cost_predictor=load_cost_predictor,
split_id=split_id,
augmentation_id=augmentation_id,
observe_cost=args.observe_cost,
target_model=target_model,
seed=args.seed,
test_generalization_to_model=args.test_generalization_to_model,
use_only_target_model=args.use_only_target_model,
subsample_models_in_hub=args.subsample_models_in_hub,
file_with_init_indices=args.file_with_init_indices,
)
optimizer_budget, optimizer_cost, optimizer_performance = BO(
optimizer,
metadataset,
budget_limit,
observe_cost=args.observe_cost,
)
results = [
optimizer_budget,
optimizer_cost,
optimizer_performance,
]
load_meta_trained = True
load_cost_predictor = True
with open(os.path.join(temp_output_dir, f"results.json"), "w") as f:
json.dump(results, f)
except Exception as e:
print(e)
print("Error in dataset: ", dataset_name, augmentation_id)