A container to hold all callbacks, and trigger them iteratively. This is only used by the base trainer to run all the callbacks. Users do not need to use this class.
| 45 | |
| 46 | |
| 47 | class Callbacks(Callback): |
| 48 | """ |
| 49 | A container to hold all callbacks, and trigger them iteratively. |
| 50 | |
| 51 | This is only used by the base trainer to run all the callbacks. |
| 52 | Users do not need to use this class. |
| 53 | """ |
| 54 | |
| 55 | def __init__(self, cbs): |
| 56 | """ |
| 57 | Args: |
| 58 | cbs(list): a list of :class:`Callback` instances. |
| 59 | """ |
| 60 | # check type |
| 61 | for cb in cbs: |
| 62 | assert isinstance(cb, Callback), cb.__class__ |
| 63 | self.cbs = cbs |
| 64 | |
| 65 | def _setup_graph(self): |
| 66 | with tf.name_scope(None): # clear the name scope |
| 67 | for cb in self.cbs: |
| 68 | cb.setup_graph(self.trainer) |
| 69 | |
| 70 | def _before_train(self): |
| 71 | for cb in self.cbs: |
| 72 | cb.before_train() |
| 73 | |
| 74 | def _after_train(self): |
| 75 | for cb in self.cbs: |
| 76 | # make sure callbacks are properly finalized |
| 77 | try: |
| 78 | cb.after_train() |
| 79 | except Exception: |
| 80 | traceback.print_exc() |
| 81 | |
| 82 | def get_hooks(self): |
| 83 | return [CallbackToHook(cb) for cb in self.cbs] |
| 84 | |
| 85 | def trigger_step(self): |
| 86 | for cb in self.cbs: |
| 87 | cb.trigger_step() |
| 88 | |
| 89 | def _trigger_epoch(self): |
| 90 | tm = CallbackTimeLogger() |
| 91 | |
| 92 | for cb in self.cbs: |
| 93 | display_name = str(cb) |
| 94 | with tm.timed_callback(display_name): |
| 95 | cb.trigger_epoch() |
| 96 | tm.log() |
| 97 | |
| 98 | def _before_epoch(self): |
| 99 | for cb in self.cbs: |
| 100 | cb.before_epoch() |
| 101 | |
| 102 | def _after_epoch(self): |
| 103 | for cb in self.cbs: |
| 104 | cb.after_epoch() |
no outgoing calls
no test coverage detected