Skip to content
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 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions baal/active/stopping_criteria.py
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()
11 changes: 7 additions & 4 deletions experiments/mlp_mcdropout.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from baal import ActiveLearningDataset, ModelWrapper
from baal.active import ActiveLearningLoop
from baal.active.heuristics import BALD
from baal.active.stopping_criteria import LabellingBudgetStoppingCriterion
from baal.bayesian.dropout import patch_module

use_cuda = torch.cuda.is_available()
Expand Down Expand Up @@ -54,16 +55,18 @@

# Following Gal 2016, we reset the weights at the beginning of each step.
initial_weights = deepcopy(model.state_dict())
stopping_criterion = LabellingBudgetStoppingCriterion(
active_dataset=al_dataset, labelling_budget=10
)

for step in range(100):
while True:
model.load_state_dict(initial_weights)
train_loss = wrapper.train_on_dataset(
al_dataset, optimizer=optimizer, batch_size=32, epoch=10, use_cuda=use_cuda
)
test_loss = wrapper.test_on_dataset(test_ds, batch_size=32, use_cuda=use_cuda)

pprint(wrapper.get_metrics())
flag = al_loop.step()
if not flag:
# We are done labelling! stopping
al_loop.step()
if stopping_criterion.should_stop():
Copy link
Collaborator

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

Suggested change
al_loop.step()
if stopping_criterion.should_stop():
flag = al_loop.step()
if stopping_criterion.should_stop() or flag:

Copy link
Member Author

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?

break
Loading