-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_experiments.py
More file actions
93 lines (73 loc) · 2.55 KB
/
run_experiments.py
File metadata and controls
93 lines (73 loc) · 2.55 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
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
import itertools
import subprocess
import sys
from pathlib import Path
import pandas as pd
# -----------------------------
# Hyperparameter grid
# -----------------------------
LAMBDA1_VALUES = [0.01, 0.1, 1, 10] # spec_w
LAMBDA2_VALUES = [0.01, 0.1, 1, 10] # smooth_w
RECON_W = 1.0
SPARSE_W = 0.0
# Change this if you want longer runs
EPOCHS = 5
BATCH_SIZE = 8
LR = 1e-3
SEGMENT_SECONDS = 2.0
SUMMARY_DIR = Path("outputs")
SUMMARY_DIR.mkdir(parents=True, exist_ok=True)
SUMMARY_FILE = SUMMARY_DIR / "grid_search_summary.csv"
def run_command(cmd):
print("\nRunning:", " ".join(cmd))
result = subprocess.run(cmd)
if result.returncode != 0:
raise RuntimeError(f"Command failed: {' '.join(cmd)}")
def main():
results = []
combos = list(itertools.product(LAMBDA1_VALUES, LAMBDA2_VALUES))
for i, (lambda1, lambda2) in enumerate(combos, start=1):
exp_name = f"exp_l1_{lambda1}_l2_{lambda2}"
ckpt_dir = Path("checkpoints") / exp_name
out_dir = Path("outputs") / exp_name
ckpt_dir.mkdir(parents=True, exist_ok=True)
out_dir.mkdir(parents=True, exist_ok=True)
train_cmd = [
sys.executable, "src/train.py",
"--exp_name", exp_name,
"--epochs", str(EPOCHS),
"--batch_size", str(BATCH_SIZE),
"--lr", str(LR),
"--segment_seconds", str(SEGMENT_SECONDS),
"--recon_w", str(RECON_W),
"--spec_w", str(lambda1),
"--smooth_w", str(lambda2),
"--sparse_w", str(SPARSE_W),
]
run_command(train_cmd)
eval_cmd = [
sys.executable, "src/evaluate.py",
"--checkpoint", str(ckpt_dir / "best_model.pt"),
"--out_dir", str(out_dir),
"--segment_seconds", str(SEGMENT_SECONDS),
]
run_command(eval_cmd)
metrics_file = out_dir / "test_metrics.csv"
df = pd.read_csv(metrics_file)
row = {
"exp_name": exp_name,
"lambda1_spec": lambda1,
"lambda2_smooth": lambda2,
"recon_w": RECON_W,
"sparse_w": SPARSE_W,
"epochs": EPOCHS,
"mean_stoi": df["stoi"].mean(),
"mean_si_sdr": df["si_sdr"].mean(),
}
results.append(row)
pd.DataFrame(results).to_csv(SUMMARY_FILE, index=False)
print(f"\nSaved summary after {i}/{len(combos)} runs -> {SUMMARY_FILE}")
print("\nAll experiments completed successfully.")
print(f"Final summary saved to: {SUMMARY_FILE}")
if __name__ == "__main__":
main()