WaitableState thingy and some stuff in the ML world.
authorScott Gasch <[email protected]>
Thu, 18 Nov 2021 06:14:37 +0000 (22:14 -0800)
committerScott Gasch <[email protected]>
Thu, 18 Nov 2021 06:14:37 +0000 (22:14 -0800)
ml/model_trainer.py
state_tracker.py

index f9e132e18aa20ecf2461db55257b6037a0c13a4e..f61b8e745b6cd6c02f23e258003928caba81916b 100644 (file)
@@ -356,6 +356,13 @@ class TrainingBlueprint(ABC):
             import input_utils
             import string_utils
 
+            now: datetime.datetime = datetime_utils.now_pacific()
+            info = f"""Timestamp: {datetime_utils.datetime_to_string(now)}
+Model params: {params}
+Training examples: {num_examples}
+Training set score: {training_score:.2f}%
+Testing set score: {test_score:.2f}%"""
+            print(f'\n{info}\n')
             if (
                     (self.spec.persist_percentage_threshold is not None and
                      test_score > self.spec.persist_percentage_threshold)
@@ -376,12 +383,6 @@ class TrainingBlueprint(ABC):
                 print(msg)
                 logger.info(msg)
                 model_info_filename = f"{self.spec.basename}_model_info.txt"
-                now: datetime.datetime = datetime_utils.now_pacific()
-                info = f"""Timestamp: {datetime_utils.datetime_to_string(now)}
-Model params: {params}
-Training examples: {num_examples}
-Training set score: {training_score:.2f}%
-Testing set score: {test_score:.2f}%"""
                 with open(model_info_filename, "w") as f:
                     f.write(info)
                 msg = f"Wrote {model_info_filename}:"
index 11ce4c3b4198e5f7ff6e0406b35d8b65920b46d7..4f77ff42241474d214f222c428ae0f60e1da1e51 100644 (file)
@@ -4,6 +4,7 @@ from abc import ABC, abstractmethod
 import datetime
 import logging
 import time
+import threading
 from typing import Dict, Optional
 
 import pytz
@@ -112,7 +113,7 @@ class AutomaticStateTracker(StateTracker):
                 logger.debug('pace_maker noticed event; shutting down')
                 return
             self.heartbeat()
-            logger.debug(f'page_maker is sleeping for {self.sleep_delay}s')
+            logger.debug(f'pace_maker is sleeping for {self.sleep_delay}s')
             time.sleep(self.sleep_delay)
 
     def __init__(
@@ -144,3 +145,32 @@ class AutomaticStateTracker(StateTracker):
         self.should_terminate.set()
         self.updater_thread.join()
         logger.debug('Background thread terminated.')
+
+
+class WaitableAutomaticStateTracker(AutomaticStateTracker):
+
+    def __init__(
+            self,
+            update_ids_to_update_secs: Dict[str, float],
+            *,
+            override_sleep_delay: Optional[float] = None,
+    ) -> None:
+        self._something_changed = threading.Event()
+        super().__init__(update_ids_to_update_secs,
+                         override_sleep_delay=override_sleep_delay)
+
+    def something_changed(self):
+        self._something_changed.set()
+
+    def did_something_change(self) -> bool:
+        return self._something_changed.is_set()
+
+    def reset(self):
+        self._something_changed.clear()
+
+    def wait(self,
+             *,
+             timeout=None):
+        return self._something_changed.wait(
+            timeout=timeout
+        )