-
Notifications
You must be signed in to change notification settings - Fork 397
[MRG] Adds Batch Count in History #445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BenjaminBossan
merged 7 commits into
skorch-dev:master
from
thomasjpfan:batch_cnt_in_history
May 1, 2019
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0e139ba
ENH Adds batch_count to history
thomasjpfan eb5f522
ENH Adds batch_count support to lr_scheduler
thomasjpfan ea20e6a
BUG Fix print logging
thomasjpfan ddc47d1
DOC Adds to changelog
thomasjpfan 3787df4
CLN Address comments
thomasjpfan ab14a49
CLN Address comments
thomasjpfan b9c9049
CLN Address comments
thomasjpfan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,18 +104,70 @@ def test_lr_callback_batch_steps_correctly( | |
policy, | ||
kwargs, | ||
): | ||
num_examples = 1000 | ||
batch_size = 100 | ||
max_epochs = 2 | ||
|
||
X, y = classifier_data | ||
num_examples = len(X) | ||
|
||
lr_policy = LRScheduler(policy, **kwargs) | ||
net = NeuralNetClassifier(classifier_module(), max_epochs=max_epochs, | ||
batch_size=batch_size, callbacks=[lr_policy]) | ||
net.fit(X, y) | ||
expected = (num_examples // batch_size) * max_epochs - 1 | ||
|
||
total_iterations_per_epoch = num_examples / batch_size | ||
# 80% of sample used for training by default | ||
total_training_iterations_per_epoch = 0.8 * total_iterations_per_epoch | ||
|
||
expected = int(total_training_iterations_per_epoch * max_epochs) | ||
# pylint: disable=protected-access | ||
assert lr_policy.batch_idx_ == expected | ||
|
||
@pytest.mark.parametrize('policy, kwargs', [ | ||
('CyclicLR', {}), | ||
]) | ||
def test_lr_callback_batch_steps_correctly_fallback( | ||
self, | ||
classifier_module, | ||
classifier_data, | ||
policy, | ||
kwargs, | ||
): | ||
batch_size = 100 | ||
max_epochs = 2 | ||
|
||
X, y = classifier_data | ||
num_examples = len(X) | ||
|
||
lr_policy = LRScheduler(policy, **kwargs) | ||
net = NeuralNetClassifier(classifier_module(), max_epochs=max_epochs, | ||
batch_size=batch_size, callbacks=[lr_policy]) | ||
net.fit(X, y) | ||
|
||
# Removes batch count information in the last two epochs | ||
for i in range(2): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't 2 be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup! PR updated to reflect this. |
||
del net.history[i]["train_batch_count"] | ||
del net.history[i]["valid_batch_count"] | ||
net.partial_fit(X, y) | ||
|
||
total_iterations_per_epoch = num_examples / batch_size | ||
|
||
# batch_counts were removed thus the total iterations of the last | ||
# epoch is used | ||
total_iterations_fit_run = total_iterations_per_epoch * max_epochs | ||
|
||
# 80% of sample used for training by default | ||
total_iterations_partial_fit_run = ( | ||
0.8 * total_iterations_per_epoch * max_epochs) | ||
|
||
# called fit AND partial_fit | ||
total_iterations = (total_iterations_fit_run + | ||
total_iterations_partial_fit_run) | ||
# Failback to using both valid and training batches counts on | ||
# second run | ||
expected = int(total_iterations) | ||
# pylint: disable=protected-access | ||
assert lr_policy.lr_scheduler_.last_batch_idx == expected | ||
assert lr_policy.batch_idx_ == expected | ||
|
||
def test_lr_scheduler_cloneable(self): | ||
# reproduces bug #271 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very minor nitpick: I don't like the indentation here, this should fit in one line (88 chars is okay).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have gotten too use to sklearn's 80 characters limit. :)
This PR has been updated.