modAL Documentation

repository·master·Indexed 25 days ago

https://github.com/modal-python/modal

A modular active learning framework for Python 3 built on top of scikit-learn. modAL provides tools to build intelligent workflows that query the most informative data points for labeling to reduce manual annotation costs. It features the ActiveLearner class for general active learning, BayesianOptimizer for active optimization, and Committee and CommitteeRegressor for ensemble-based active learning and regression. The framework supports custom query strategies, bootstrapping, bagging, and integration with Keras models via scikit-learn wrappers.

Tokens
15.3K
Snippets
32
Records
70
Agent score
80%

What's inside modAL

  1. Overview of modAL active learning strategies

    master

    modAL is a modular active learning framework for Python 3 built on top of scikit-learn. It supports a wide variety of active learning strategies across different paradigms:

    • Uncertainty-based sampling: least_confident, max_margin, and max_entropy.
    • Committee-based algorithms: vote_entropy, consensus_entropy, and max_disagreement.
    • Multilabel strategies: svm_binary_minimum, max_loss, mean_max_loss, MinConfidence, MeanConfidence, MinScore, and MeanScore.
    • Expected error reduction: binary and log_loss.
    • Bayesian optimization: probability_of_improvement, expected_improvement, and upper_confidence_bound.
    • Batch active learning: ranked_batch_mode_sampling.
    • Information density framework.
    • Stream-based sampling.
    • Active regression: max_standard_deviance sampling for Gaussian processes or ensemble regressors.
  2. Use acquisition functions with BayesianOptimizer

    master
    In Bayesian optimization, acquisition functions are used instead of uncertainty-based utility measures. In modAL, Bayesian optimization algorithms are implemented via the modAL.models.BayesianOptimizer class. To use an acquisition function, you must pass the corresponding strategy to the query_strategy parameter during the initialization of the BayesianOptimizer.
  3. How the ActiveLearner workflow works

    master

    The core of modAL is the ActiveLearner class, which manages the active learning loop. A typical workflow consists of three main steps:

    1. Initialization: Create an ActiveLearner instance by providing an estimator (e.g., a scikit-learn classifier), and initial training data (X_training, y_training).
    2. Querying: Use the .query(X_pool) method to identify the most informative instance(s) from an unlabeled pool (X_pool). This returns the index of the queried instance and the instance itself.
    3. Teaching: Once a label is obtained (from an 'Oracle'), use the .teach(X_new, y_new) method to update the learner with the new labeled data.

    This loop can be repeated multiple times to iteratively improve the model's performance.

    from modAL.models import ActiveLearner
    from sklearn.ensemble import RandomForestClassifier
    
    # 1. Initializing the learner
    learner = ActiveLearner(
        estimator=RandomForestClassifier(),
        X_training=X_training, y_training=y_training
    )
    
    # 2. Query for labels
    query_idx, query_inst = learner.query(X_pool)
    
    # ... obtain new label y_new from Oracle ...
    
    # 3. Supply label for queried instance
    learner.teach(X_pool[query_idx], y_new)
  4. Use Consensus Entropy for Classifier Disagreement Sampling

    master

    Consensus entropy is a measure that looks at the average confidence of the committee. It first calculates the consensus probability (the average of the class probabilities provided by each classifier). It then selects the instance with the largest entropy of this consensus probability.

    This differs from vote entropy because it accounts for the underlying confidence (probabilities) of each classifier rather than just their final discrete votes.

  5. Implement a custom query strategy

    master

    A query strategy in modAL is a function that takes at least two arguments: the estimator (the model) and the X_pool (the pool of unlabeled examples). The function must return a tuple containing the index of the queried instance and the instance itself.

    Example of a simple random sampling strategy:

    import numpy as np
    
    def random_sampling(classifier, X_pool):
        n_samples = len(X_pool)
        query_idx = np.random.choice(range(n_samples))
        return query_idx, X_pool[query_idx]
    
    # Use it in the learner
    learner = ActiveLearner(
        estimator=RandomForestClassifier(),
        query_strategy=random_sampling,
        X_training=X_training, y_training=y_training
    )
  6. Use BayesianOptimizer for expensive function optimization

    master

    The BayesianOptimizer class is used when a function is expensive to evaluate or when gradients are unavailable. Unlike ActiveLearner which focuses on uncertainty, BayesianOptimizer evaluates points where there is a high promise of finding a better value.

    Key Requirements:

    • It must be used with a regressor (e.g., GaussianProcessRegressor). Using it with a classifier is possible if labels are numeric, but the results will be meaningless.
    • It uses an acquisition function to estimate expected gains at specific points.

    Both BayesianOptimizer and ActiveLearner inherit from BaseLearner and share the same interface, including the query() and teach() methods.

    from modAL.models import BayesianOptimizer
    from modAL.acquisition import max_EI
    from sklearn.gaussian_process import GaussianProcessRegressor
    from sklearn.gaussian_process.kernels import Matern
    
    kernel = Matern(length_scale=1.0)
    regressor = GaussianProcessRegressor(kernel=kernel)
    
    optimizer = BayesianOptimizer(
        estimator=regressor,
        query_strategy=max_EI
    )
  7. Understand Disagreement Sampling for Classifiers

    master

    Disagreement sampling is a query strategy used when you have a committee of hypotheses (models) and want to select the next instances to label by measuring how much the models disagree. In modAL, this is implemented for classification using committee-based models. There are three primary built-in measures:

    1. Vote Entropy: Calculates the entropy of the distribution of class labels produced by the committee's votes. It selects the instance with the highest entropy.
    2. Consensus Entropy: Calculates the average class probabilities across all classifiers (the consensus probability) and then computes the entropy of that consensus. It selects the instance with the highest consensus entropy.
    3. Max Disagreement: Measures the disagreement of each individual learner against the consensus probability using Kullback-Leibler (KL) divergence. It selects the instance where at least one learner has the highest divergence from the consensus.

    These strategies are typically used with committee-based models for classifiers.

  8. Use Max Disagreement for Classifier Disagreement Sampling

    master
    Max disagreement sampling identifies instances where at least one learner's prediction deviates significantly from the committee's consensus. It calculates the Kullback-Leibler (KL) divergence between each learner's predicted probability distribution and the consensus probability distribution. The instance with the largest maximum KL divergence across all learners is selected.
  9. Measure disagreement in CommitteeRegressor using standard deviation

    master

    In a CommitteeRegressor ensemble, a common way to measure disagreement (and thus uncertainty) is to use the standard deviation of the predictions. This standard deviation can be used as a basis for query strategies to select which instances should be labeled next.

    Note that for many ordinary regressors, measuring uncertainty is difficult unless they explicitly provide a way to calculate it (e.g., Gaussian process regressors).

  10. Use Vote Entropy for Classifier Disagreement Sampling

    master

    Vote entropy selects instances where the distribution of votes from the committee is most uncertain. For each instance, a probability distribution is created based on the frequency of each class label in the committee's predictions. The instance with the largest entropy in this distribution is queried.

    Example logic:

    • Committee votes for an instance: [0, 1, 0] (two votes for class 0, one for class 1).
    • Vote distribution: [0.6666, 0.3333, 0.0].
    • Calculate entropy of this distribution; higher entropy indicates higher disagreement.