Callback that prints metrics to stdout. Arguments: count_mode: One of "steps" or "samples". Whether the progress bar should count samples seen or steps (batches) seen. stateful_metrics: Iterable of string names of metrics that should *not* be averaged ove
| 692 | |
| 693 | @keras_export('keras.callbacks.ProgbarLogger') |
| 694 | class ProgbarLogger(Callback): |
| 695 | """Callback that prints metrics to stdout. |
| 696 | |
| 697 | Arguments: |
| 698 | count_mode: One of "steps" or "samples". |
| 699 | Whether the progress bar should |
| 700 | count samples seen or steps (batches) seen. |
| 701 | stateful_metrics: Iterable of string names of metrics that |
| 702 | should *not* be averaged over an epoch. |
| 703 | Metrics in this list will be logged as-is. |
| 704 | All others will be averaged over time (e.g. loss, etc). |
| 705 | |
| 706 | Raises: |
| 707 | ValueError: In case of invalid `count_mode`. |
| 708 | """ |
| 709 | |
| 710 | def __init__(self, count_mode='samples', stateful_metrics=None): |
| 711 | super(ProgbarLogger, self).__init__() |
| 712 | if count_mode == 'samples': |
| 713 | self.use_steps = False |
| 714 | elif count_mode == 'steps': |
| 715 | self.use_steps = True |
| 716 | else: |
| 717 | raise ValueError('Unknown `count_mode`: ' + str(count_mode)) |
| 718 | self.stateful_metrics = set(stateful_metrics or []) |
| 719 | |
| 720 | def on_train_begin(self, logs=None): |
| 721 | self.verbose = self.params['verbose'] |
| 722 | self.epochs = self.params['epochs'] |
| 723 | |
| 724 | def on_epoch_begin(self, epoch, logs=None): |
| 725 | self.seen = 0 |
| 726 | if self.use_steps: |
| 727 | self.target = self.params['steps'] |
| 728 | else: |
| 729 | self.target = self.params['samples'] |
| 730 | |
| 731 | if self.verbose: |
| 732 | if self.epochs > 1: |
| 733 | print('Epoch %d/%d' % (epoch + 1, self.epochs)) |
| 734 | self.progbar = Progbar( |
| 735 | target=self.target, |
| 736 | verbose=self.verbose, |
| 737 | stateful_metrics=self.stateful_metrics, |
| 738 | unit_name='step' if self.use_steps else 'sample') |
| 739 | |
| 740 | def on_batch_begin(self, batch, logs=None): |
| 741 | self.log_values = [] |
| 742 | |
| 743 | def on_batch_end(self, batch, logs=None): |
| 744 | logs = logs or {} |
| 745 | batch_size = logs.get('size', 0) |
| 746 | # In case of distribution strategy we can potentially run multiple steps |
| 747 | # at the same time, we should account for that in the `seen` calculation. |
| 748 | num_steps = logs.get('num_steps', 1) |
| 749 | if self.use_steps: |
| 750 | self.seen += num_steps |
| 751 | else: |
no outgoing calls
no test coverage detected