|
| 1 | +# Active learning |
| 2 | + |
| 3 | +[](https://github.com/astral-sh/ruff) |
| 4 | +[](https://github.com/pre-commit/pre-commit) |
| 5 | + |
| 6 | +Active Learning is a subfield of machine learning where the model iteratively queries the most relevant unlabeled data points, to optimize performance with minimal labeled data. |
| 7 | +This project provides the implementation of various Active Learning query strategies, for an easy application and comparison of different types of acquisition functions. The Active Learning framework is based on [modAL](https://github.com/modAL-python), a popular package for Python. |
| 8 | + |
| 9 | +## Structure |
| 10 | +The repository includes the following features: |
| 11 | + |
| 12 | +- The main scripts `AL_cycle.py` and `AL_selection.py`, which contain functions that can be used to execute an active learning cycle with the specified parameters and to compare the performances of different query strategies. |
| 13 | +- The `activelearning/queries` folder contains the different query strategies that are implemented, both for the pool based and stream based scenario. More detail on this below. |
| 14 | +- The `activelearning/utils` folder contains helper functions for the main scripts and examples. |
| 15 | +- The `examples` folder contains some demostrative notebooks to show how the main features work. |
| 16 | +- The `docs` folder contains additional documentation. |
| 17 | + |
| 18 | + |
| 19 | +## Installation |
| 20 | +The repository is setup us as a poetry project and by default requires Python 3.10 or later. |
| 21 | +To install the repository you can follow these steps: |
| 22 | + |
| 23 | +First, install `poetry` if you haven't already, as indicated by the instructions on the [Poetry installation page](https://python-poetry.org/docs/). |
| 24 | +Then, clone the repository to your local machine using the following command: |
| 25 | +``` |
| 26 | +git clone https://github.com/orobix/active-learning |
| 27 | +cd active-learning |
| 28 | +``` |
| 29 | +Use Poetry to install the project dependencies: |
| 30 | +``` |
| 31 | +poetry install |
| 32 | +``` |
| 33 | +Finally, activate the virtual environment created by Poetry: |
| 34 | +``` |
| 35 | +poetry shell |
| 36 | +``` |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +## Introduction on Active Learning |
| 43 | + |
| 44 | +Active Learning aims to save time and labeling costs by reducing the amount of labeled data required to train models, as annotation is often an expensive and laborious task. The solution is iteratively selecting a small set of the most relevant samples from unlabeled data, and querying an oracle for their label. This can allow to train a model with high accuracy while spending less resources on the construction of the dataset. |
| 45 | + |
| 46 | +For example, when using a random forest classifier on the _Iris_ dataset, and randomly choosing one instance to be labeled at every iteration, it's possible to reach the same accuracy that the model would have when using the whole training set (96%) with only 12 data points. |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +## Query strategies |
| 51 | + |
| 52 | +Query strategies, also called acquisition functions, are the criteria with which data points are selected to be labeled. **Representation based** query strategies try to explore the whole feature space to find samples that are representative of the whole data. They are agnostic methods, as they don't require the training of a model. |
| 53 | +Implemented resentation based query strategies are: |
| 54 | +* Information density query |
| 55 | +* K-Means cluster-based query |
| 56 | +* Diversity query |
| 57 | +* Coreset query |
| 58 | +* ProbCover query |
| 59 | + |
| 60 | +**Information based** query strategies rely on a model trained on a small labeled set of data, and search on the most informative unlabeled sampled according to the model predictions, measured for example with uncertainty criterias. In this category are also **Query by committee** methods, which measure informativeness with the prediction of a committee of models. |
| 61 | +Implemented information based query strategies are: |
| 62 | +* Least Confindent uncertainty sampling |
| 63 | +* Margin uncertainty sampling |
| 64 | +* Entropy uncertainty sampling |
| 65 | +* QBC vote entropy sampling |
| 66 | +* QBC consensus entropy sampling |
| 67 | +* QBC max disagreement sampling |
| 68 | + |
| 69 | +**Bayesian Optimization** based strategies rely on stochastic forward passes in a neural net classifier, referred to as Monte Carlo Dropout, to approximate Bayesian posterior probabilities and measure uncertainty. |
| 70 | +Implemented Bayesian query strategies are: |
| 71 | +* MC max entropy |
| 72 | +* BALD (Bayesian Active Learning by Disagreements) |
| 73 | +* Max variation ratios |
| 74 | +* Max mean std |
| 75 | + |
| 76 | +## Stream based scenario |
| 77 | + |
| 78 | +When data points arrive one at a time from a stream, instead of having a pool of unlabeled data to select from, there are two options: in **Batch setting** samples are saved until a batch is complete, and then the classical query strategies can be performed on the batch; in the pure **Stream** setting, a criteria is used to decide whether to query the new point or discard it. |
| 79 | +Implemented stream based query stratgies are: |
| 80 | +* Stream diversity query |
| 81 | +* Stream Coreset query |
| 82 | +* Stream ProbCover query |
| 83 | +* Stream LC uncertainty sampling |
| 84 | +* Stream Margin uncertainty sampling |
| 85 | +* Stream Entropy uncertainty sampling |
| 86 | + |
| 87 | +## Example |
| 88 | + |
| 89 | +The functions in this repository can be used to effectively compare the effectiveness of different query strategies on a labeled dataset, in order to be able to choose the appropriate one in real applications with unlabeled data. With the following script we can compare some representation-based strategies on the _Iris_ dataset: |
| 90 | + |
| 91 | +``` |
| 92 | +scores = strategy_comparison( |
| 93 | + X_train=None, y_train=None, |
| 94 | + X_pool=X_pool, y_pool=y_pool, |
| 95 | + X_test=X_test, y_test=y_test, |
| 96 | + classifier="randomforest", |
| 97 | + query_strategies=[query_kmeans_foreach, query_density, query_coreset, query_random], |
| 98 | + n_instances=n_instances, |
| 99 | + K=3, # number of clusters for k-means query |
| 100 | + metric="euclidean", # metric for density query |
| 101 | + goal_acc=0.96, |
| 102 | +) |
| 103 | +``` |
| 104 | + |
| 105 | + |
| 106 | +Detail of this implentation can be found in `examples/1_iris.ipynb` |
| 107 | + |
| 108 | +## Resources |
| 109 | + |
| 110 | +- [modAL documentation](https://modal-python.readthedocs.io/en/latest/) |
| 111 | +- [A Survey on Active Learning: State-of-the-Art, Practical Challenges and Research Directions (Tharwat & Schenck, 2023)](https://www.mdpi.com/2227-7390/11/4/820) |
| 112 | +- [A Survey on Deep Active Learning: Recent Advances and New Frontiers (Li et al., 2024)](https://ieeexplore.ieee.org/abstract/document/10537213) |
0 commit comments