Skip to content

Commit

Permalink
Preparation for release of skorch v0.12.0 (#902)
Browse files Browse the repository at this point in the history
* Bump version to 0.12.0
* Update CHANGES.md
* Remove warning about signature change in on_batch

We had a change in the signature of on_batch_begin and on_batch_end.
Therefore, we tried to detect if users had overridden these methods and
issued a (hopefully) helpful warning.

This change is now established long enough that we can remove the
warning and associated tests.
  • Loading branch information
BenjaminBossan committed Oct 7, 2022
1 parent 6ed3b09 commit 1596c51
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 202 deletions.
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

### Changed

### Fixed

## [0.12.0] - 2022-10-07

### Added
- Added `load_best` attribute to `EarlyStopping` callback to automatically load module weights of the best result at the end of training
- Added a method, `trim_for_prediction`, on the net classes, which trims the net from everything not required for using it for prediction; call this after fitting to reduce the size of the net
- Added experimental support for [huggingface accelerate](https://github.com/huggingface/accelerate); use the provided mixin class to add advanced training capabilities provided by the accelerate library to skorch
- Add integration for Huggingface tokenizers; use `skorch.hf.HuggingfaceTokenizer` to train a Huggingface tokenizer on your custom data; use `skorch.hf.HuggingfacePretrainedTokenizer` to load a pre-trained Huggingface tokenizer
- Added support for creating model checkpoints on Hugging Face Hub using [`HfHubStorage`](https://skorch.readthedocs.io/en/latest/hf.html#skorch.hf.HfHubStorage)
- Added a [notebook](https://nbviewer.org/github/skorch-dev/skorch/blob/master/notebooks/CORA-geometric.ipynb) that shows how to use skorch with PyTorch Geometric (#863)

### Changed
- The minimum required scikit-learn version has been bumped to 0.22.0
Expand All @@ -23,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix a bug in `SliceDataset` that prevented it to be used with `to_numpy` (#858)
- Fix a bug that occurred when loading a net that has device set to None (#876)
- Fix a bug that in some cases could prevent loading a net that was trained with CUDA without CUDA
- Enable skorch to work on M1/M2 Apple MacBooks (#884)

## [0.11.0] - 2021-10-11

Expand Down Expand Up @@ -273,3 +283,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[0.9.0]: https://github.com/skorch-dev/skorch/compare/v0.8.0...v0.9.0
[0.10.0]: https://github.com/skorch-dev/skorch/compare/v0.9.0...v0.10.0
[0.11.0]: https://github.com/skorch-dev/skorch/compare/v0.10.0...v0.11.0
[0.12.0]: https://github.com/skorch-dev/skorch/compare/v0.11.0...v0.12.0
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.1dev
0.12.0
81 changes: 0 additions & 81 deletions skorch/callbacks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,84 +63,3 @@ def get_params(self, deep=True):

def set_params(self, **params):
BaseEstimator.set_params(self, **params)


# TODO: remove after some deprecation period, e.g. skorch 0.12
def _on_batch_overridden(callback):
"""Check if on_batch_begin or on_batch_end were overridden
If the method does not exist at all, it's not considered overridden. This is
mostly for callbacks that are mocked.
"""
try:
base_skorch_cls = next(cls for cls in callback.__class__.__mro__
if cls.__module__.startswith('skorch'))
except StopIteration:
# does not derive from skorch callback, possibly a mock
return False

obb = base_skorch_cls.on_batch_begin
obe = base_skorch_cls.on_batch_end
return (
getattr(callback.__class__, 'on_batch_begin', obb) is not obb
or getattr(callback.__class__, 'on_batch_end', obe) is not obe
)


# TODO: remove after some deprecation period, e.g. skorch 0.12
def _issue_warning_if_on_batch_override(callback_list):
"""Check callbacks for overridden on_batch method and issue warning
We introduced a breaking change by changing the signature of on_batch_begin
and on_batch_end. To help users, we try to detect if they use any custom
callback that overrides on of these methods and issue a warning if they do.
The warning states how to adjust the method signature and how it can be
filtered.
After some transition period, the checking and the warning should be
removed again.
Parameters
----------
callback_list : list of (str, callback) tuples
List of initialized callbacks.
Warns
-----
Issues a ``SkorchWarning`` if any of the callbacks fits the conditions.
"""
if not callback_list:
return

callbacks = [callback for _, callback in callback_list]

# first detect if there are any user defined callbacks
user_defined_callbacks = [
callback for callback in callbacks
if not callback.__module__.startswith('skorch')
]
if not user_defined_callbacks:
return

# check if any of these callbacks overrides on_batch_begin or on_batch_end
overriding_callbacks = [
callback for callback in user_defined_callbacks
if _on_batch_overridden(callback)
]

if not overriding_callbacks:
return

warning_msg = (
"You are using an callback that overrides on_batch_begin "
"or on_batch_end. As of skorch 0.10, the signature was changed "
"from 'on_batch_{begin,end}(self, X, y, ...)' to "
"'on_batch_{begin,end}(self, batch, ...)'. To recover, change "
"the signature accordingly and add 'X, y = batch' on the first "
"line of the method body. To suppress this warning, add:\n"
"'import warnings; from skorch.exceptions import SkorchWarning\n"
"warnings.filterwarnings('ignore', category=SkorchWarning)'.")

warnings.warn(warning_msg, SkorchWarning)
5 changes: 0 additions & 5 deletions skorch/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from skorch.callbacks import EpochTimer
from skorch.callbacks import PrintLog
from skorch.callbacks import PassthroughScoring
from skorch.callbacks.base import _issue_warning_if_on_batch_override
from skorch.dataset import Dataset
from skorch.dataset import ValidSplit
from skorch.dataset import get_len
Expand Down Expand Up @@ -352,10 +351,6 @@ def notify(self, method_name, **cb_kwargs):
* on_batch_end
"""
# TODO: remove after some deprecation period, e.g. skorch 0.12
if not self.history: # perform check only at the start
_issue_warning_if_on_batch_override(self.callbacks_)

getattr(self, method_name)(self, **cb_kwargs)
for _, cb in self.callbacks_:
getattr(cb, method_name)(self, **cb_kwargs)
Expand Down
115 changes: 0 additions & 115 deletions skorch/tests/callbacks/test_base.py

This file was deleted.

0 comments on commit 1596c51

Please sign in to comment.