-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathautolearn.py
More file actions
60 lines (49 loc) · 1.88 KB
/
autolearn.py
File metadata and controls
60 lines (49 loc) · 1.88 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
#!/usr/bin/env python3
"""
Autolearn loop: run main.py (100 steps) -> train.py outcome_summary (10 epochs) -> repeat.
Graceful exit on Ctrl+C.
"""
import subprocess
import sys
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parent
MAIN_STEPS = 100
OUTCOME_EPOCHS = 10
# Exit codes often used for SIGINT (Ctrl+C)
SIGINT_EXIT = (130, 2)
def run_main(steps: int = MAIN_STEPS) -> int:
"""Run main.py for given steps. Returns process returncode."""
return subprocess.run(
[sys.executable, str(PROJECT_ROOT / "main.py"), str(steps)],
cwd=PROJECT_ROOT,
).returncode
def run_train(mode: str, epochs: int) -> int:
"""Run train.py with --mode and --epochs. Returns process returncode."""
return subprocess.run(
[sys.executable, str(PROJECT_ROOT / "train.py"), "--mode", mode, "--epochs", str(epochs)],
cwd=PROJECT_ROOT,
).returncode
def main() -> None:
cycle = 0
try:
while True:
cycle += 1
print(f"\n--- Autolearn cycle {cycle}: main.py {MAIN_STEPS} steps ---")
rc = run_main(MAIN_STEPS)
if rc in SIGINT_EXIT:
print("\nAutolearn: interrupted, exiting gracefully.")
sys.exit(0)
if rc != 0:
print(f"main.py exited with {rc}; continuing to training.", file=sys.stderr)
print(f"\n--- Autolearn cycle {cycle}: train.py outcome_summary {OUTCOME_EPOCHS} epochs ---")
rc = run_train("outcome_summary", OUTCOME_EPOCHS)
if rc in SIGINT_EXIT:
print("\nAutolearn: interrupted, exiting gracefully.")
sys.exit(0)
if rc != 0:
print(f"train.py outcome_summary exited with {rc}", file=sys.stderr)
except KeyboardInterrupt:
print("\nAutolearn: Ctrl+C, exiting gracefully.")
sys.exit(0)
if __name__ == "__main__":
main()