import json
from pathlib import Path
results = []
for exp_dir in Path("experiments").glob("*/"):
# Training state is saved in trainer_state.json
state_file = exp_dir / "trainer_state.json"
if state_file.exists():
with open(state_file) as f:
state = json.load(f)
results.append({
"experiment": exp_dir.name,
"best_metric": state.get("best_metric"),
"global_step": state.get("global_step"),
"epoch": state.get("epoch"),
})
# Sort by best_metric (typically eval_loss)
results.sort(key=lambda x: x.get("best_metric") or float("inf"))
# Print best
print("Best experiment:", results[0]["experiment"])