Skip to content

Commit

Permalink
Create classes random_variable_uncertainty.py and variable_uncertaint…
Browse files Browse the repository at this point in the history
…y.py.
  • Loading branch information
albertfrancajosuacosta committed Sep 10, 2023
1 parent 8e270be commit 3837d42
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 12 deletions.
6 changes: 1 addition & 5 deletions base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@

class ActiveLearningBase():

""" Base classe for Active Learning Library """

@abc.abstractmethod()
def describe(self, describe):
...
""" Base classe for Active Learning Library """
9 changes: 2 additions & 7 deletions fixed_uncertainty.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,12 @@ class FixedUncertainty(ActiveLearningBase):
"""

def __init__(self, theta: float = 0.95, seed=None):
def __init__(self, theta: float = 0.95):
super().__init__()
self.theta = theta


def describe(self):
print('Strategy of Active Learning to select instances more significative based on uncertainty.'+
'The fixed uncertainty sampler selects samples for labeling based on the uncertainty of the prediction.'+
'The higher the uncertainty, the more likely the sample will be selected for labeling. The uncertainty'+
'measure is compared with a fixed uncertainty limit.'+
'References [^1]: I. Zliobaite, A. Bifet, B.Pfahringer, G. Holmes. “Active Learning with Drifting Streaming Data”, IEEE Transactions on Neural Netowrks and Learning Systems, Vol.25 (1), pp.27-39, 2014.')


def isSignificative(self, x, y_pred) -> bool:
"""Ask for the label of a current instance.
Expand Down
73 changes: 73 additions & 0 deletions random_variable_uncertainty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
Active Learning 18
@author: Albert França Josuá Costa
@email: [email protected]
"""

import numpy as np

from .base import ActiveLearningBase



class RandomVariableUncertainty (ActiveLearningBase):

r"""Strategy of Active Learning to select instances more significative based on uncertainty.
The random variable uncertainty sampler selects samples for labeling based on the uncertainty of the prediction.
The higher the uncertainty, the more likely the sample will be selected for labeling. The uncertainty
measure is compared with a random variable uncertainty limit.
References
----------
[^1]: I. Zliobaite, A. Bifet, B.Pfahringer, G. Holmes. “Active Learning with Drifting Streaming Data”, IEEE Transactions on Neural Netowrks and Learning Systems, Vol.25 (1), pp.27-39, 2014.
"""

def __init__(self, theta: float = 0.95, s=0.5, delta=1.0):
super().__init__()

self.theta = theta
self.s = s
self.delta = delta


def isSignificative(self, x, y_pred) -> bool:
"""Ask for the label of a current instance.
Based on the uncertainty of the base classifier, it checks whether the current instance should be labeled.
Parameters
----------
x
Instance
y_pred
Arrays of predicted labels
Returns
-------
selected
A boolean indicating whether a label is needed.
True for selected instance.
False for not selecte instance.
"""
maximum_posteriori = max(y_pred.values())
selected = False

thetaRand = self.theta * np.random.normal(1,self.delta)

if maximum_posteriori < thetaRand:
self.theta = self.theta*(1-self.s)
selected = True
else:
self.theta = self.theta*(1+self.s)
selected = False

return selected
70 changes: 70 additions & 0 deletions variable_uncertainty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
Active Learning 18
@author: Albert França Josuá Costa
@email: [email protected]
"""


from .base import ActiveLearningBase



class VariableUncertainty (ActiveLearningBase):

r"""Strategy of Active Learning to select instances more significative based on uncertainty.
The variable uncertainty sampler selects samples for labeling based on the uncertainty of the prediction.
The higher the uncertainty, the more likely the sample will be selected for labeling. The uncertainty
measure is compared with a random variable uncertainty limit.
References
----------
[^1]: I. Zliobaite, A. Bifet, B.Pfahringer, G. Holmes. “Active Learning with Drifting Streaming Data”, IEEE Transactions on Neural Netowrks and Learning Systems, Vol.25 (1), pp.27-39, 2014.
"""

def __init__(self, theta: float = 0.95, s=0.5):
super().__init__()

self.theta = theta
self.s = s


def isSignificative(self, x, y_pred) -> bool:
"""Ask for the label of a current instance.
Based on the uncertainty of the base classifier, it checks whether the current instance should be labeled.
Parameters
----------
x
Instance
y_pred
Arrays of predicted labels
Returns
-------
selected
A boolean indicating whether a label is needed.
True for selected instance.
False for not selecte instance.
"""
maximum_posteriori = max(y_pred.values())
selected = False


if maximum_posteriori < self.theta:
self.theta = self.theta*(1-self.s)
selected = True
else:
self.theta = self.theta*(1+self.s)
selected = False

return selected

0 comments on commit 3837d42

Please sign in to comment.