Skip to content

Commit

Permalink
Add on_checkpoint decorator (#547)
Browse files Browse the repository at this point in the history
* Add on_checkpoint decorator

* Update changelog

* Update docstrings
  • Loading branch information
MattPainter01 committed May 3, 2019
1 parent 0b4a5eb commit d8138aa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added the option to give the ``only_if`` callback decorator a function of self and state rather than just state
- Added Layer-sequential unit-variance (LSUV) initialization
- Added ClassAppearanceModel callback and example page for visualising CNNs
- Added on_checkpoint callback decorator
### Changed
- `No_grad` and `enable_grad` decorators are now also context managers
### Deprecated
Expand Down
6 changes: 6 additions & 0 deletions tests/callbacks/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ def example(state):
state = 'test'
self.assertTrue(callbacks.on_step_validation(example).on_step_validation(state) == state)

def test_on_checkpoint(self):
def example(state):
return state
state = 'test'
self.assertTrue(callbacks.on_checkpoint(example).on_checkpoint(state) == state)

def test_add_to_loss(self):
def example(state):
return 1
Expand Down
13 changes: 13 additions & 0 deletions torchbearer/callbacks/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,19 @@ def on_step_validation(func):
return bind_to(Callback.on_step_validation)(func)


def on_checkpoint(func):
""" The :func:`on_checkpoint` decorator is used to initialise a :class:`.Callback` with :meth:`~.Callback.on_checkpoint`
calling the decorated function
Args:
func (function): The function(state) to *decorate*
Returns:
Callback: Initialised callback with :meth:`~.Callback.on_checkpoint` calling func
"""
return bind_to(Callback.on_checkpoint)(func)


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
Expand Down

0 comments on commit d8138aa

Please sign in to comment.