Merge monitors together for trainer to use. In training, each trainer will create a :class:`Monitors` instance, and you can access it through ``trainer.monitors``. You should use ``trainer.monitors`` for logging and it will dispatch your logs to each sub-monitor.
| 111 | |
| 112 | |
| 113 | class Monitors(Callback): |
| 114 | """ |
| 115 | Merge monitors together for trainer to use. |
| 116 | |
| 117 | In training, each trainer will create a :class:`Monitors` instance, |
| 118 | and you can access it through ``trainer.monitors``. |
| 119 | You should use ``trainer.monitors`` for logging and it will dispatch your |
| 120 | logs to each sub-monitor. |
| 121 | """ |
| 122 | |
| 123 | _chief_only = False |
| 124 | |
| 125 | def __init__(self, monitors): |
| 126 | self._scalar_history = ScalarHistory() |
| 127 | self._monitors = monitors + [self._scalar_history] |
| 128 | for m in self._monitors: |
| 129 | assert isinstance(m, MonitorBase), m |
| 130 | |
| 131 | def _setup_graph(self): |
| 132 | # scalar_history's other methods were not called. |
| 133 | # but they are not useful for now |
| 134 | self._scalar_history.setup_graph(self.trainer) |
| 135 | |
| 136 | def _dispatch(self, func): |
| 137 | for m in self._monitors: |
| 138 | func(m) |
| 139 | |
| 140 | def put_summary(self, summary): |
| 141 | """ |
| 142 | Put a `tf.Summary`. |
| 143 | """ |
| 144 | if isinstance(summary, six.binary_type): |
| 145 | summary = tf.Summary.FromString(summary) |
| 146 | assert isinstance(summary, tf.Summary), type(summary) |
| 147 | |
| 148 | # TODO other types |
| 149 | for val in summary.value: |
| 150 | if val.WhichOneof('value') == 'simple_value': |
| 151 | val.tag = re.sub('tower[0-9]+/', '', val.tag) # TODO move to subclasses |
| 152 | |
| 153 | # TODO This hack is still needed, seem to disappear only when |
| 154 | # compiled from source. |
| 155 | suffix = '-summary' # tensorflow#6150, tensorboard#59 |
| 156 | if val.tag.endswith(suffix): |
| 157 | val.tag = val.tag[:-len(suffix)] |
| 158 | |
| 159 | self._dispatch(lambda m: m.process_scalar(val.tag, val.simple_value)) |
| 160 | |
| 161 | self._dispatch(lambda m: m.process_summary(summary)) |
| 162 | |
| 163 | def put_scalar(self, name, val): |
| 164 | """ |
| 165 | Put a scalar. |
| 166 | """ |
| 167 | if isinstance(val, np.floating): |
| 168 | val = float(val) |
| 169 | if isinstance(val, np.integer): |
| 170 | val = int(val) |