-
Notifications
You must be signed in to change notification settings - Fork 2
/
sweep_cml.py
151 lines (135 loc) · 4.32 KB
/
sweep_cml.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
import argparse
import multiprocessing as mp
import os
from datetime import datetime
from functools import partial
import wandb
import train_cml
def forked(fn):
"""
Does not work on Windows (except WSL2), since the fork syscall is not supported here.
fork creates a new process which inherits all the memory without it being copied.
Memory is copied on write instead, meaning it is very cheap to create a new process
Reference: https://gist.github.com/schlamar/2311116?permalink_comment_id=3932763#gistcomment-3932763
"""
def call(*args, **kwargs):
ctx = mp.get_context("fork")
q = ctx.Queue(1)
is_error = ctx.Value("b", False)
def target():
try:
q.put(fn(*args, **kwargs))
except BaseException as e:
is_error.value = True
q.put(e)
ctx.Process(target=target).start()
result = q.get()
if is_error.value:
raise result
return result
return call
class Args:
def __init__(
self,
id: str,
config: wandb.Config,
output_dir: str,
learner: str,
num_workers: int = 2,
verbose: int = 1,
split_mode: int = 0,
path2featurizer: str = None,
):
self.learner = learner
self.output_dir = os.path.join(
output_dir, f"{datetime.now():%Y%m%d-%Hh%Mm}-{id}"
)
self.path2featurizer = path2featurizer
self.batch_size = 256
self.num_workers = num_workers
self.device = None
self.split_mode = split_mode
self.dataset = "data/preprocessed/sl512_ss128"
self.seed = 1234
self.save_test_model_outputs = False
self.test_time = False
self.reuse_stats = True
self.format = "svg"
self.dpi = 120
self.verbose = verbose
self.clear_output_dir = False
self.use_wandb = True
for key, value in config.items():
if not hasattr(self, key):
setattr(self, key, value)
def main(
output_dir: str,
wandb_group: str,
learner: str,
num_workers: int = 2,
verbose: int = 1,
split_mode: int = None,
path2featurizer: str = None,
):
run = wandb.init(group=wandb_group)
config = run.config
run.name = run.id
args = Args(
id=run.id,
config=config,
output_dir=output_dir,
num_workers=num_workers,
split_mode=split_mode,
path2featurizer=path2featurizer,
verbose=verbose,
learner=learner,
)
train_cml.main(args, wandb_sweep=True)
@forked
def agent(params):
wandb.agent(
sweep_id=params.sweep_id,
function=partial(
main,
output_dir=params.output_dir,
wandb_group=params.wandb_group,
verbose=params.verbose,
split_mode=params.split_mode,
path2featurizer=params.path2featurizer,
learner=params.learner,
),
count=1,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--output_dir", type=str, required=True)
parser.add_argument("--sweep_id", type=str, required=True)
parser.add_argument("--wandb_group", type=str, required=True)
parser.add_argument(
"--num_trials",
type=int,
default=1,
help="number of trials to run with this agent",
)
parser.add_argument("--verbose", type=int, default=1, choices=[0, 1, 2])
parser.add_argument(
"--split_mode",
type=int,
default=0,
choices=[0, 1],
required=False,
help="criterion for train/val/test split:"
"0) time-split: each session is split into 70:15:15 along the temporal "
"dimension such that segments from different splits map to "
"different parts of the recording"
"1) subject-split: cases and controls are split into 70:15:15 "
"train/val/test such that subjects are not shared across splits",
)
parser.add_argument("--path2featurizer", type=str, required=False)
parser.add_argument("--num_workers", type=int, default=2)
parser.add_argument(
"--learner", type=str, default=None, choices=["xgboost", "svm", "knn", "enet"]
)
params = parser.parse_args()
for _ in range(params.num_trials):
agent(params)