MCPcopy Index your code
hub / github.com/tensorpack/tensorpack / MovingAverageSummary

Class MovingAverageSummary

tensorpack/callbacks/summary.py:17–63  ·  view source on GitHub ↗

Maintain the moving average of summarized tensors in every step, by ops added to the collection. Note that it only **maintains** the moving averages by updating the relevant variables in the graph, the actual summary should be done in other callbacks. This callback is one o

Source from the content-addressed store, hash-verified

15
16
17class MovingAverageSummary(Callback):
18 """
19 Maintain the moving average of summarized tensors in every step,
20 by ops added to the collection.
21 Note that it only **maintains** the moving averages by updating
22 the relevant variables in the graph,
23 the actual summary should be done in other callbacks.
24
25 This callback is one of the :func:`DEFAULT_CALLBACKS()`.
26 """
27 def __init__(self, collection=MOVING_SUMMARY_OPS_KEY, train_op=None):
28 """
29 Args:
30 collection(str): the collection of EMA-maintaining ops.
31 The default value would work with
32 the tensors you added by :func:`tfutils.summary.add_moving_summary()`,
33 but you can use other collections as well.
34 train_op (tf.Operation or str): the (name of) training op to associate the maintaing ops with.
35 If not provided, the EMA-maintaining ops will be hooked to
36 `trainer.hooked_session` and be executed in every iteration.
37 Otherwise, the EMA-maintaining ops will be executed whenever
38 the training op is executed.
39 """
40 self._collection = collection
41 self._train_op = train_op
42
43 def _setup_graph(self):
44 ops = [k.op for k in tf.get_collection(self._collection)]
45 if self._train_op is None:
46 logger.info("[MovingAverageSummary] {} operations in collection '{}' "
47 "will be run with session hooks.".format(len(ops), self._collection))
48
49 self.ema_op = tf.group(*ops, name='maintain_moving_average_summary')
50 self._fetch = tf.train.SessionRunArgs(fetches=self.ema_op)
51 else:
52 if isinstance(self._train_op, tf.Tensor):
53 self._train_op = self._train_op.op
54 if not isinstance(self._train_op, tf.Operation):
55 self._train_op = self.graph.get_operation_by_name(self._train_op)
56 self._train_op._add_control_inputs(ops)
57 logger.info("[MovingAverageSummary] {} operations in collection '{}'"
58 " will be run together with operation '{}'.".format(
59 len(ops), self._collection, self._train_op.name))
60
61 def _before_run(self, _):
62 if self._train_op is None:
63 return self._fetch
64
65
66class MergeAllSummaries_RunAlone(Callback):

Callers 2

DEFAULT_CALLBACKSFunction · 0.85
get_configFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected