-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add callbacks #11
Closed
Closed
Add callbacks #11
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module XGBoost | ||
class CallbackContainer | ||
attr_reader :callbacks, :history | ||
|
||
def initialize(callbacks) | ||
@callbacks = callbacks | ||
@history = {} | ||
|
||
callbacks.each do |callback| | ||
raise ArgumentError, 'callback must subclass XGBoost::TrainingCallback.' unless callback.is_a?(TrainingCallback) | ||
end | ||
end | ||
|
||
def before_training(model: nil) | ||
callbacks.each do |callback| | ||
model = callback.before_training(model: model) | ||
unless model.is_a?(XGBoost::Booster) | ||
raise ArgumentError, "Callback #{callback.class}#before_training must return an instance of XGBoost::Booster" | ||
end | ||
end | ||
model | ||
end | ||
|
||
def after_training(model: nil) | ||
callbacks.each do |callback| | ||
model = callback.after_training(model: model) | ||
unless model.is_a?(XGBoost::Booster) | ||
raise ArgumentError, "Callback #{callback.class}#after_training must return an instance of XGBoost::Booster" | ||
end | ||
end | ||
model | ||
end | ||
|
||
# If ANY callback returns false, then EXIT | ||
def before_iteration(model: nil, epoch: nil) | ||
callbacks.none? || callbacks.all? do |callback| | ||
callback.before_iteration(model: model, epoch: epoch) | ||
end | ||
end | ||
|
||
# If ANY callback returns false, then EXIT | ||
def after_iteration(model: nil, epoch: nil, res: nil) | ||
update_history(res) | ||
|
||
callbacks.none? || callbacks.all? do |callback| | ||
callback.after_iteration(model: model, epoch: epoch, history: history) | ||
end | ||
end | ||
|
||
private | ||
|
||
def update_history(res) | ||
res.each do |name, value| | ||
data_name, metric_name = name.split('-', 2) | ||
history[data_name] ||= {} | ||
history[data_name][metric_name] ||= [] | ||
history[data_name][metric_name] << value | ||
end | ||
end | ||
end | ||
end |
This file contains 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module XGBoost | ||
class TrainingCallback | ||
def before_training(model: nil) | ||
# Run before training starts | ||
model | ||
end | ||
|
||
def after_training(model: nil) | ||
# Run after training is finished | ||
model | ||
end | ||
|
||
def before_iteration(model: nil, epoch: nil) | ||
# Run before each iteration. Returns true when training should stop. | ||
false | ||
end | ||
|
||
def after_iteration(model: nil, epoch: nil, history: nil) | ||
# Run after each iteration. Returns true when training should stop. | ||
false | ||
end | ||
end | ||
end |
This file contains 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 |
---|---|---|
@@ -0,0 +1,186 @@ | ||
require_relative 'test_helper' | ||
|
||
class CallbacksTest < Minitest::Test | ||
class MockCallback < XGBoost::TrainingCallback | ||
attr_reader :before_training_count, :after_training_count, :before_iteration_count, :after_iteration_count, | ||
:before_training_args, :after_training_args, :before_iteration_args, :after_iteration_args, :history | ||
|
||
def initialize | ||
@before_training_count = 0 | ||
@after_training_count = 0 | ||
@before_iteration_count = 0 | ||
@after_iteration_count = 0 | ||
@before_training_args = [] | ||
@after_training_args = [] | ||
@before_iteration_args = [] | ||
@after_iteration_args = [] | ||
@history = {} | ||
end | ||
|
||
def before_training(model: nil) | ||
@before_training_count += 1 | ||
model | ||
end | ||
|
||
def after_training(model: nil) | ||
@after_training_count += 1 | ||
model | ||
end | ||
|
||
def before_iteration(model: nil, epoch: nil) | ||
@before_iteration_count += 1 | ||
@before_iteration_args << { epoch: epoch } | ||
true | ||
end | ||
|
||
def after_iteration(model: nil, epoch: nil, history: nil) | ||
@after_iteration_count += 1 | ||
@history = history | ||
true | ||
end | ||
end | ||
|
||
def test_callback_raises_when_not_training_callback | ||
num_boost_round = 10 | ||
|
||
assert_raises(ArgumentError, /callback must subclass XGBoost::TrainingCallback/) do | ||
XGBoost.train( | ||
regression_params, | ||
regression_train, | ||
num_boost_round: num_boost_round, | ||
callbacks: ['not a callback'], | ||
evals: [[regression_train, 'train'], [regression_test, 'eval']] | ||
) | ||
end | ||
end | ||
|
||
def test_callback | ||
callback = MockCallback.new | ||
num_boost_round = 10 | ||
|
||
XGBoost.train( | ||
regression_params, | ||
regression_train, | ||
num_boost_round: num_boost_round, | ||
callbacks: [callback], | ||
evals: [[regression_train, 'train'], [regression_test, 'eval']] | ||
) | ||
|
||
assert_equal 1, callback.before_training_count | ||
assert_equal 1, callback.after_training_count | ||
assert_equal num_boost_round, callback.before_iteration_count | ||
assert_equal num_boost_round, callback.after_iteration_count | ||
|
||
# Verify arguments | ||
train_rmse = callback.history['train']['rmse'] | ||
assert_equal num_boost_round, train_rmse.size | ||
train_rmse.each do |value| | ||
assert_in_delta 0.00, value, 1.0 | ||
end | ||
eval_rmse = callback.history['eval']['rmse'] | ||
assert_equal num_boost_round, eval_rmse.size | ||
eval_rmse.each do |value| | ||
assert_in_delta 0.00, value, 1.0 | ||
end | ||
|
||
epochs = callback.before_iteration_args.map { |e| e[:epoch] } | ||
assert_equal (0...num_boost_round).to_a, epochs | ||
end | ||
|
||
def test_callback_breaks_on_before_iteration | ||
callback = MockCallback.new | ||
def callback.before_iteration(model: nil, epoch: nil) | ||
@before_iteration_count += 1 | ||
@before_iteration_args << { epoch: epoch } | ||
# If any callback returns false, break | ||
epoch.even? | ||
end | ||
num_boost_round = 10 | ||
|
||
XGBoost.train( | ||
regression_params, | ||
regression_train, | ||
num_boost_round: num_boost_round, | ||
callbacks: [callback], | ||
evals: [[regression_train, 'train'], [regression_test, 'eval']] | ||
) | ||
|
||
assert_equal 1, callback.before_training_count | ||
assert_equal 1, callback.after_training_count | ||
assert_equal 2, callback.before_iteration_count | ||
assert_equal 1, callback.after_iteration_count | ||
|
||
# Verify arguments | ||
train_rmse = callback.history['train']['rmse'] | ||
assert_equal 1, train_rmse.size | ||
train_rmse.each do |value| | ||
assert_in_delta 0.00, value, 1.0 | ||
end | ||
eval_rmse = callback.history['eval']['rmse'] | ||
assert_equal 1, eval_rmse.size | ||
eval_rmse.each do |value| | ||
assert_in_delta 0.00, value, 1.0 | ||
end | ||
|
||
epochs = callback.before_iteration_args.map { |e| e[:epoch] } | ||
assert_equal (0...2).to_a, epochs | ||
end | ||
|
||
def test_callback_breaks_on_after_iteration | ||
callback = MockCallback.new | ||
def callback.after_iteration(model: nil, epoch: nil, history: nil) | ||
@after_iteration_count += 1 | ||
@history = history | ||
epoch < 7 | ||
end | ||
num_boost_round = 10 | ||
|
||
XGBoost.train( | ||
regression_params, | ||
regression_train, | ||
num_boost_round: num_boost_round, | ||
callbacks: [callback], | ||
evals: [[regression_train, 'train'], [regression_test, 'eval']] | ||
) | ||
|
||
assert_equal 1, callback.before_training_count | ||
assert_equal 1, callback.after_training_count | ||
assert_equal 8, callback.before_iteration_count | ||
assert_equal 8, callback.after_iteration_count | ||
|
||
# Verify arguments | ||
train_rmse = callback.history['train']['rmse'] | ||
assert_equal 8, train_rmse.size | ||
train_rmse.each do |value| | ||
assert_in_delta 0.00, value, 1.0 | ||
end | ||
eval_rmse = callback.history['eval']['rmse'] | ||
assert_equal 8, eval_rmse.size | ||
eval_rmse.each do |value| | ||
assert_in_delta 0.00, value, 1.0 | ||
end | ||
|
||
epochs = callback.before_iteration_args.map { |e| e[:epoch] } | ||
assert_equal (0...8).to_a, epochs | ||
end | ||
|
||
def test_updates_model_before_training | ||
callback = MockCallback.new | ||
def callback.before_training(model: nil) | ||
model['device'] = 'cuda:0' | ||
model | ||
end | ||
|
||
num_boost_round = 10 | ||
|
||
model = XGBoost.train( | ||
regression_params, | ||
regression_train, | ||
num_boost_round: num_boost_round, | ||
callbacks: [callback], | ||
evals: [[regression_train, 'train'], [regression_test, 'eval']] | ||
) | ||
|
||
assert_equal model['device'], 'cuda:0' | ||
end | ||
end |
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.
Please keep the existing code where possible to keep the changeset minimal / easier to review.
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.
Sorry, disabled rubocop