Commit fabb7e7e authored by Yuxin Wu's avatar Yuxin Wu

update docs about callbacks

parent 2c129ded
# Callbacks
Callback is an interface to do __everything else__ besides the training iterations.
Apart from the actual training iterations that minimize the cost,
you almost surely would like to do something else.
Callbacks are such an interface to describe what to do besides the
training iterations.
There are several places where you might want to do something else:
* Before the training has started (e.g. initialize the saver, dump the graph)
......
......@@ -8,6 +8,7 @@ def train(self):
callbacks.setup_graph()
# ... create session, initialize session, finalize graph ...
# start training:
with sess.as_default():
callbacks.before_train()
for epoch in range(epoch_start, epoch_end):
callbacks.before_epoch()
......@@ -103,3 +104,16 @@ to let this method run every k steps or every k epochs.
* Access the current status of training, such as `epoch_num`, `global_step`. See [here](../../modules/callbacks.html#tensorpack.callbacks.Callback)
* Stop training by `raise StopTraining()` (with `from tensorpack.train import StopTraining`).
* Anything else that can be done with plain python.
### Typical Steps about Writing/Using a Callback
* Define the callback in `__init__`, prepare for it in `_setup_graph, _before_train`.
* Know whether you want to do something __along with__ the session run or not.
If yes, implement the logic with `_{before,after}_run`.
Otherwise, implement in `_trigger`, or `_trigger_step`.
* You can choose to only implement "what to do", and leave "when to do" to
other wrappers such as
[PeriodicTrigger](../../modules/callbacks.html#tensorpack.callbacks.PeriodicTrigger),
[PeriodicRunHooks](../../modules/callbacks.html#tensorpack.callbacks.PeriodicRunHooks),
or [EnableCallbackIf](../../modules/callbacks.html#tensorpack.callbacks.EnableCallbackIf).
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment