def on_criterion(func): """ The :func:`on_criterion` decorator is used to initialise a :class:`.Callback` with :meth:`~.Callback.on_criterion` calling the decorated function :param func: The function(state) to *decorate* :type func: function :return: Initialised callback with :meth:`.Callback.on_criterion` calling func :rtype: :class:`.Callback` """ new_callback = Callback() new_callback.on_criterion = func return new_callback
def add_to_loss(func): """ The :func:`add_to_loss` decorator is used to initialise a :class:`.Callback` with the value returned from func being added to the loss :param func: The function(state) to *decorate* :type func: function :return: Initialised callback which adds the returned value from func to the loss :rtype: :class:`.Callback` """ def add_to_loss_func(state): state[torchbearer.LOSS] += func(state) new_callback = Callback() new_callback.on_criterion = add_to_loss_func new_callback.on_criterion_validation = add_to_loss_func return new_callback
def test_empty_methods(self): callback = Callback() self.assertIsNone(callback.on_start({})) self.assertIsNone(callback.on_start_epoch({})) self.assertIsNone(callback.on_start_training({})) self.assertIsNone(callback.on_sample({})) self.assertIsNone(callback.on_forward({})) self.assertIsNone(callback.on_criterion({})) self.assertIsNone(callback.on_backward({})) self.assertIsNone(callback.on_step_training({})) self.assertIsNone(callback.on_end_training({})) self.assertIsNone(callback.on_end_epoch({})) self.assertIsNone(callback.on_end({})) self.assertIsNone(callback.on_start_validation({})) self.assertIsNone(callback.on_sample_validation({})) self.assertIsNone(callback.on_forward_validation({})) self.assertIsNone(callback.on_end_validation({})) self.assertIsNone(callback.on_step_validation({})) self.assertIsNone(callback.on_criterion_validation({}))