Print scalar data into terminal.
| 408 | |
| 409 | |
| 410 | class ScalarPrinter(MonitorBase): |
| 411 | """ |
| 412 | Print scalar data into terminal. |
| 413 | """ |
| 414 | |
| 415 | def __init__(self, enable_step=False, enable_epoch=True, |
| 416 | whitelist=None, blacklist=None): |
| 417 | """ |
| 418 | Args: |
| 419 | enable_step, enable_epoch (bool): whether to print the |
| 420 | monitor data (if any) between steps or between epochs. |
| 421 | whitelist (list[str] or None): A list of regex. Only names |
| 422 | matching some regex will be allowed for printing. |
| 423 | Defaults to match all names. |
| 424 | blacklist (list[str] or None): A list of regex. Names matching |
| 425 | any regex will not be printed. Defaults to match no names. |
| 426 | """ |
| 427 | def compile_regex(rs): |
| 428 | if rs is None: |
| 429 | return None |
| 430 | rs = {re.compile(r) for r in rs} |
| 431 | return rs |
| 432 | |
| 433 | self._whitelist = compile_regex(whitelist) |
| 434 | if blacklist is None: |
| 435 | blacklist = [] |
| 436 | self._blacklist = compile_regex(blacklist) |
| 437 | |
| 438 | self._enable_step = enable_step |
| 439 | self._enable_epoch = enable_epoch |
| 440 | self._dic = {} |
| 441 | |
| 442 | # in case we have something to log here. |
| 443 | def _before_train(self): |
| 444 | self._trigger() |
| 445 | |
| 446 | def _trigger_step(self): |
| 447 | if self._enable_step: |
| 448 | if self.local_step != self.trainer.steps_per_epoch - 1: |
| 449 | # not the last step |
| 450 | self._trigger() |
| 451 | else: |
| 452 | if not self._enable_epoch: |
| 453 | self._trigger() |
| 454 | # otherwise, will print them together |
| 455 | |
| 456 | def _trigger_epoch(self): |
| 457 | if self._enable_epoch: |
| 458 | self._trigger() |
| 459 | |
| 460 | @HIDE_DOC |
| 461 | def process_scalar(self, name, val): |
| 462 | self._dic[name] = float(val) |
| 463 | |
| 464 | def _trigger(self): |
| 465 | # Print stats here |
| 466 | def match_regex_list(regexs, name): |
| 467 | for r in regexs: |