-
Notifications
You must be signed in to change notification settings - Fork 86
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 Stopping Criteria for loop #286
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from typing import List, Dict | ||
|
||
import numpy as np | ||
|
||
from baal import ActiveLearningDataset | ||
|
||
|
||
class StoppingCriterion: | ||
def __init__(self, active_dataset: ActiveLearningDataset): | ||
self._active_ds = active_dataset | ||
|
||
def should_stop(self, metrics: Dict[str, float], uncertainty: List[float]) -> bool: | ||
raise NotImplementedError | ||
|
||
|
||
class LabellingBudgetStoppingCriterion(StoppingCriterion): | ||
"""Stops when the labelling budget is exhausted.""" | ||
|
||
def __init__(self, active_dataset: ActiveLearningDataset, labelling_budget: int): | ||
super().__init__(active_dataset) | ||
self._start_length = len(active_dataset) | ||
self.labelling_budget = labelling_budget | ||
|
||
def should_stop(self, uncertainty: List[float]) -> bool: | ||
return (len(self._active_ds) - self._start_length) >= self.labelling_budget | ||
|
||
|
||
class LowAverageUncertaintyStoppingCriterion(StoppingCriterion): | ||
"""Stops when the average uncertainty is on average below a threshold.""" | ||
|
||
def __init__(self, active_dataset: ActiveLearningDataset, avg_uncertainty_thresh: float): | ||
super().__init__(active_dataset) | ||
self.avg_uncertainty_thresh = avg_uncertainty_thresh | ||
|
||
def should_stop(self, metrics: Dict[str, float], uncertainty: List[float]) -> bool: | ||
return np.mean(uncertainty) < self.avg_uncertainty_thresh | ||
|
||
|
||
class EarlyStoppingCriterion(StoppingCriterion): | ||
"""Early stopping on a particular metrics. | ||
|
||
Notes: | ||
We don't have any mandatory dependency with an early stopping implementation. | ||
So we have our own. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
active_dataset: ActiveLearningDataset, | ||
metric_name: str, | ||
patience: int = 10, | ||
epsilon: float = 1e-4, | ||
): | ||
super().__init__(active_dataset) | ||
self.metric_name = metric_name | ||
self.patience = patience | ||
self.epsilon = epsilon | ||
self._acc = [] | ||
|
||
def should_stop(self, metrics: Dict[str, float], uncertainty: List[float]) -> bool: | ||
self._acc.append(metrics[self.metric_name]) | ||
near_threshold = np.isclose(np.array(self._acc), self._acc[-1], atol=self.epsilon) | ||
return len(near_threshold) > self.patience and near_threshold[-self.patience].all() |
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
Oops, something went wrong.
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.
Wondering if you wanna change these two lines to below lines to account for general exhaustion of the dataset. The scenario does not apply to this example but as a demonstration of how to account for general exhaustion of the dataset and if the stop criteria is a metric limit
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.
My thinking was that stopping criteria must be added to ALLoop which would check for both the criteria and exhaustion. It would require ALLoop to know about metrics which it can't do right now.
That would be a major breaking change, so we might want to have a new object instead and deprecate ALLoop 🤔 and then this new object would do the entire experiment?