For each gradient tensor, summary its histogram and add it to moving summaries.
| 152 | # TODO has dependency problems: sess.run may not depend on grad |
| 153 | # maybe group maintain op and grad ? |
| 154 | class SummaryGradient(MapGradient): |
| 155 | """ |
| 156 | For each gradient tensor, summary its histogram and add it to moving |
| 157 | summaries. |
| 158 | """ |
| 159 | # avoid duplicate summaries from towers |
| 160 | # TODO this is global. not good. |
| 161 | _summaried_gradient = set() |
| 162 | |
| 163 | def __init__(self, regex='.*', collections=None): |
| 164 | """ |
| 165 | Args: |
| 166 | regex(str): same as in :class:`MapGradient`. |
| 167 | collections (list[str]): list of collection names |
| 168 | """ |
| 169 | super(SummaryGradient, self).__init__(self._mapper, regex) |
| 170 | self._coll = collections |
| 171 | |
| 172 | def _mapper(self, grad, var): |
| 173 | name = var.op.name |
| 174 | if re.match('tower[0-9]+/', name): |
| 175 | # replicated training, var may come from different towers |
| 176 | return grad |
| 177 | if name not in SummaryGradient._summaried_gradient: |
| 178 | SummaryGradient._summaried_gradient.add(name) |
| 179 | tfv1.summary.histogram(name + '-grad', grad, collections=self._coll) |
| 180 | add_moving_summary(rms(grad, name=name + '/rms')) |
| 181 | return grad |
| 182 | |
| 183 | |
| 184 | class PrintGradient(MapGradient): |