Write all scalar data to a json file under ``logger.get_logger_dir()``, grouped by their global step. If found an earlier json history file, will append to it.
| 288 | |
| 289 | |
| 290 | class JSONWriter(MonitorBase): |
| 291 | """ |
| 292 | Write all scalar data to a json file under ``logger.get_logger_dir()``, grouped by their global step. |
| 293 | If found an earlier json history file, will append to it. |
| 294 | """ |
| 295 | |
| 296 | FILENAME = 'stats.json' |
| 297 | """ |
| 298 | The name of the json file. Do not change it. |
| 299 | """ |
| 300 | |
| 301 | def __new__(cls): |
| 302 | if logger.get_logger_dir(): |
| 303 | return super(JSONWriter, cls).__new__(cls) |
| 304 | else: |
| 305 | logger.warn("logger directory was not set. Ignore JSONWriter.") |
| 306 | return NoOpMonitor("JSONWriter") |
| 307 | |
| 308 | @staticmethod |
| 309 | def load_existing_json(dir=None): |
| 310 | """ |
| 311 | Look for an existing json under dir (defaults to |
| 312 | :meth:`logger.get_logger_dir()`) named "stats.json", |
| 313 | and return the loaded list of statistics if found. Returns None otherwise. |
| 314 | """ |
| 315 | if dir is None: |
| 316 | dir = logger.get_logger_dir() |
| 317 | fname = os.path.join(dir, JSONWriter.FILENAME) |
| 318 | if tf.gfile.Exists(fname): |
| 319 | with open(fname) as f: |
| 320 | stats = json.load(f) |
| 321 | assert isinstance(stats, list), type(stats) |
| 322 | return stats |
| 323 | return None |
| 324 | |
| 325 | @staticmethod |
| 326 | def load_existing_epoch_number(dir=None): |
| 327 | """ |
| 328 | Try to load the latest epoch number from an existing json stats file (if any). |
| 329 | Returns None if not found. |
| 330 | """ |
| 331 | stats = JSONWriter.load_existing_json(dir) |
| 332 | try: |
| 333 | return int(stats[-1]['epoch_num']) |
| 334 | except Exception: |
| 335 | return None |
| 336 | |
| 337 | # initialize the stats here, because before_train from other callbacks may use it |
| 338 | def _setup_graph(self): |
| 339 | self._stats = [] |
| 340 | self._stat_now = {} |
| 341 | self._last_gs = -1 |
| 342 | |
| 343 | def _before_train(self): |
| 344 | stats = JSONWriter.load_existing_json() |
| 345 | self._fname = os.path.join(logger.get_logger_dir(), JSONWriter.FILENAME) |
| 346 | if stats is not None: |
| 347 | try: |