-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_experiments.py
More file actions
57 lines (50 loc) · 1.54 KB
/
run_experiments.py
File metadata and controls
57 lines (50 loc) · 1.54 KB
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
"""
experiments must be defined in configs/experiments.json in the following format:
{
"experiment1": {
"flag1 (e.g. fixed_start or init_health)": 2
},
"experiment2": {
"flag1": 1,
"flag2": 2
},
...
}
"""
import json
import multiprocessing
import subprocess
import shlex
from multiprocessing.pool import ThreadPool
N_PROCS = 2 #multiprocessing.cpu_count() // 2
N_SEEDS = 10
START_SEED = 0
# read experiments.json
with open("configs/experiments/experiments.json", "r") as f:
experiments = json.load(f)
# turn experiments in json into cmdline commands
experiment_cmds = []
for experiment_name in experiments:
flags = experiments[experiment_name]
for i in range(N_SEEDS):
flags["name"] = experiment_name + f"_SEED{i+START_SEED}"
experiment_cmds.append(
"python3 train.py"
+ "".join([f" --{flag}={flags[flag]}" for flag in flags])
+ f" --seed {i+START_SEED}"
)
# https://stackoverflow.com/questions/25120363/multiprocessing-execute-external-command-and-wait-before-proceeding
def call_proc(cmd):
p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
return (out, err)
print(f"running on {multiprocessing.cpu_count()} cpus")
print(f"choosing to run {N_PROCS} processes")
pool = ThreadPool(N_PROCS)
results = []
for cmd in experiment_cmds:
print(f"starting exp parametrized by: {cmd}")
results.append(pool.apply_async(call_proc, (cmd,)))
pool.close()
pool.join()
#print(results)