-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Improve cancel_computation to enable testing. #4293
Merged
vigsterkr
merged 6 commits into
shogun-toolbox:develop
from
geektoni:feature/callback_cancel_computation
May 25, 2018
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f0d3316
Add callback methods to add more condition to cancel_computation.
geektoni de70296
Add unit test for callback generated by the user.
geektoni f5df1ca
add_callback -> set_callback renaming inside StoppableSGObject.
geektoni 2d4c790
Improve unit tests for StoppableSGObject.
geektoni e03dca1
Update cancel_computation() condition.
geektoni 0fe1b67
Apply clang-format and other minor fixes.
geektoni File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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,108 @@ | ||
/* | ||
* This software is distributed under BSD 3-clause license (see LICENSE file). | ||
* | ||
* Authors: Giovanni De Toni | ||
*/ | ||
|
||
#include <functional> | ||
#include <gtest/gtest.h> | ||
#include <rxcpp/rx-lite.hpp> | ||
#include <shogun/lib/Signal.h> | ||
#include <shogun/machine/Machine.h> | ||
|
||
using namespace shogun; | ||
using namespace std; | ||
|
||
/** | ||
* Mock model to show the use of the callback. | ||
*/ | ||
class MockModel : public CMachine | ||
{ | ||
public: | ||
MockModel() : m_check(0), m_last_iteration(0) | ||
{ | ||
// Set up the custom callback | ||
function<bool()> callback = [this]() { | ||
// Stop if we did more than 5 steps | ||
if (m_last_iteration >= 5) | ||
{ | ||
get_global_signal()->get_subscriber()->on_next(SG_BLOCK_COMP); | ||
return true; | ||
} | ||
m_last_iteration++; | ||
return false; | ||
}; | ||
|
||
this->set_callback(callback); | ||
}; | ||
|
||
int get_check() | ||
{ | ||
return m_check; | ||
} | ||
|
||
virtual const char* get_name() const | ||
{ | ||
return "MockModel"; | ||
} | ||
|
||
protected: | ||
virtual bool train_require_labels() const | ||
{ | ||
return false; | ||
} | ||
|
||
/* Custom train machine */ | ||
virtual bool train_machine(CFeatures* data = NULL) | ||
{ | ||
for (int num_iterations_train = 0; num_iterations_train < 10; | ||
num_iterations_train++) | ||
{ | ||
COMPUTATION_CONTROLLERS | ||
m_check++; | ||
} | ||
return true; | ||
} | ||
|
||
/* Control variable, used to check that we stopped the training at the | ||
* exact number of iterations (it will be equal to m_last_iteration)*/ | ||
int m_check; | ||
|
||
/* Addition control variable that is incremented each time by the | ||
* callback.*/ | ||
int m_last_iteration; | ||
}; | ||
|
||
TEST(StoppableSGObject, empty_callback) | ||
{ | ||
MockModel a; | ||
a.set_callback(nullptr); | ||
a.train(); | ||
EXPECT_TRUE(a.get_check() == 10); | ||
} | ||
|
||
TEST(StoppableSGObject, default_callback) | ||
{ | ||
MockModel a; | ||
a.train(); | ||
EXPECT_TRUE(a.get_check() == 5); | ||
} | ||
|
||
TEST(StoppableSGObject, custom_callback_by_user) | ||
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. This test shows more clearly what we are going to do in the future when testing the premature stopping feature. |
||
{ | ||
int i = 0; | ||
function<bool()> callback = [&i]() { | ||
if (i >= 3) | ||
{ | ||
get_global_signal()->get_subscriber()->on_next(SG_BLOCK_COMP); | ||
return true; | ||
} | ||
i++; | ||
return false; | ||
}; | ||
|
||
MockModel a; | ||
a.set_callback(callback); | ||
a.train(); | ||
EXPECT_TRUE(a.get_check() == 3); | ||
} |
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.
you either use the default (nullptr) valued
set_callback
, i.e. useor remove the default value of the function arg. i would opt for the latter