-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): allow dynamic active active learning strategies. i.e. …
…start with strategy x and mode to strategy y
- Loading branch information
Showing
7 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
label_sleuth/active_learning/core/active_learning_policies.py
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,13 @@ | ||
from label_sleuth.active_learning.core.catalog import ActiveLearningCatalog | ||
from label_sleuth.active_learning.policy.static_active_learning_policy import StaticActiveLearningPolicy | ||
|
||
|
||
class ActiveLearningPolicies: | ||
""" | ||
Active learning policies determine which type of active learning strategy is used. Policies can be static, i.e. | ||
always return the same active learning strategy, or dynamic, e.g. a different strategy is returned depending on | ||
the current iteration number. | ||
""" | ||
STATIC_RETROSPECTIVE = StaticActiveLearningPolicy(ActiveLearningCatalog.RETROSPECTIVE) | ||
STATIC_HARD_MINING = StaticActiveLearningPolicy(ActiveLearningCatalog.HARD_MINING) | ||
STATIC_RANDOM = StaticActiveLearningPolicy(ActiveLearningCatalog.RANDOM) |
Empty file.
60 changes: 60 additions & 0 deletions
60
label_sleuth/active_learning/policy/active_learning_changing_policy.py
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,60 @@ | ||
# | ||
# Copyright (c) 2022 IBM Corp. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
from typing import List | ||
|
||
import numpy as np | ||
|
||
from label_sleuth.active_learning.core.active_learning_api import ActiveLearningStrategy | ||
from label_sleuth.active_learning.policy.active_learning_policy import ActiveLearningPolicy | ||
|
||
|
||
class ActiveLearningChangingPolicy(ActiveLearningPolicy): | ||
""" | ||
A dynamic active learning policy that follows a predefined pattern of switching between active learning strategies | ||
after a certain number of iterations. | ||
""" | ||
|
||
def __init__(self, active_learning_strategies: List[ActiveLearningStrategy], | ||
num_iterations_per_strategy: List[int]): | ||
""" | ||
:param active_learning_strategies: a list of the N strategies to be used by the policy | ||
:param num_iterations_per_strategy: a corresponding list specifying the number of iterations for each of the | ||
first N-1 strategies to be used; the Nth strategy type will be used for all subsequent iterations. | ||
For example, if there are 3 strategies, and num_iterations_per_strategy=[5, 2], then strategy A will be used for | ||
iterations 0-4, strategy B for iterations 5-6, and strategy C from iteration 7 onwards. | ||
""" | ||
if len(active_learning_strategies) != len(num_iterations_per_strategy) + 1: | ||
raise Exception( | ||
f"The number of strategies provided ({len(active_learning_strategies)}) does not match the provided " | ||
f"list of {len(num_iterations_per_strategy)} active learning strategy switch points. " | ||
f"For each active learning type, except the last one, the number of iterations for this active " | ||
f"learning strategy to be used must be specified.") | ||
self.active_learning_strategies = active_learning_strategies | ||
self.num_iterations_per_strategy = num_iterations_per_strategy | ||
self.switch_points = np.cumsum(num_iterations_per_strategy) | ||
|
||
def get_active_learning_strategy(self, iteration_num: int) -> ActiveLearningStrategy: | ||
for active_learning_strategy, switch_point in zip(self.active_learning_strategies, self.switch_points): | ||
if iteration_num < switch_point: | ||
return active_learning_strategy | ||
return self.active_learning_strategies[-1] | ||
|
||
def get_name(self): | ||
name = "" | ||
for active_learning_strategy, n_iter in zip(self.active_learning_strategies, self.num_iterations_per_strategy): | ||
name += f'{active_learning_strategy.name}x{n_iter}-' | ||
name += f"{self.active_learning_strategies[-1].name}" | ||
return name |
41 changes: 41 additions & 0 deletions
41
label_sleuth/active_learning/policy/active_learning_policy.py
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,41 @@ | ||
# | ||
# Copyright (c) 2022 IBM Corp. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import abc | ||
|
||
from label_sleuth.active_learning.core.active_learning_api import ActiveLearningStrategy | ||
|
||
|
||
class ActiveLearningPolicy(object, metaclass=abc.ABCMeta): | ||
""" | ||
Base class for implementing an active learning policy, that determines which type of active learning strategy | ||
will be used. Policies can be static, i.e. always return the same active learning strategy, | ||
or dynamic, i.e. a different active learning strategy is returned | ||
depending on the current iteration. | ||
""" | ||
|
||
@abc.abstractmethod | ||
def get_active_learning_strategy(self, iteration_num: int) -> ActiveLearningStrategy: | ||
""" | ||
Given *iteration_num*, return the type of active learning strategy to be used | ||
:param iteration_num: | ||
:return: An instance of ActiveLearningStrategy | ||
""" | ||
|
||
@abc.abstractmethod | ||
def get_name(self) -> str: | ||
""" | ||
:return: a name that describes the policy | ||
""" |
35 changes: 35 additions & 0 deletions
35
label_sleuth/active_learning/policy/static_active_learning_policy.py
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,35 @@ | ||
# | ||
# Copyright (c) 2022 IBM Corp. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
from label_sleuth.active_learning.core.active_learning_api import ActiveLearningStrategy | ||
from label_sleuth.active_learning.policy.active_learning_policy import ActiveLearningPolicy | ||
|
||
|
||
class StaticActiveLearningPolicy(ActiveLearningPolicy): | ||
""" | ||
A simple policy that is initialized using a specific classification active learning strategy and always returns | ||
this strategy. | ||
""" | ||
|
||
def __init__(self, active_learning_strategy: ActiveLearningStrategy): | ||
self.static_active_learning_strategy = active_learning_strategy | ||
|
||
def get_active_learning_strategy(self, iteration_num: int) -> ActiveLearningStrategy: | ||
""" | ||
Ignores *iteration_num* and returns the active learning strategy defined in the initialization | ||
""" | ||
return self.static_active_learning_strategy | ||
|
||
def get_name(self): | ||
return f'Static-{self.static_active_learning_strategy.name}' |
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