Guide To Avalanche: A Python Library for Continual Learning

Guide To Avalanche: A Python Library for Continual Learning

Originally posted on analyticsindiamag.

When does a deployed Machine Learning model fail? There could be some good answers. One among them is when a model is deployed to make predictions on unforeseen data whose pattern is entirely different from training data. This issue is common in dynamic scenarios where non-stationary data streams in continuously. Continual Learning (CL) is a real-time machine learning approach that tries to solve dynamically varying data patterns. While making predictions on incoming unforeseen data, a CL model uses the same data and the feedback on its predictions (whether correct or incorrect) to train itself continuously in real-time.

Continual Learning is also termed Incremental Learning or Life-long Learning. Though some attempts have been made in the field of Continual Learning, the developed methods lack in providing end-to-end trainingreproducibilityportability and scalability. When a CL model attempts to learn the new pattern among data, it may forget the previous learning knowledge.

To this end, ContinualAI, a non-profit research organization and open community, including researchers from more than 15 organizations across the world, has introduced Avalanche, an end-to-end Python library for all Continuous Learning tasks. Avalanche is an open-source library based on PyTorch. This enables quick prototyping, training, evaluation, benchmarking and deployment of Continual Learning models and algorithms with minimal code. In contrast, Avalanche is an all-in-one Continual Learning solution driven by an open development community aiming for sharing, collaboration and improvement.

Design & Architecture of Avalanche

Avalanche has been designed keeping in mind the challenges of real-world applications and research projects. Avalanche’s major design principles are comprehensiveness, consistency, ease of use, modular design, reusability, reproducibility, portability, efficiency, and scalability. This makes Avalanche robust, suitable for any Continuous Learning environment and extensible.

Avalanche introduces its data structure, called learning experience (e). A learning experience is a task or batch of one or more samples. A learning experience is used to update the model in real-time. It is usually represented by a triplet of (x,y,t), where x refers to the input streaming data, y refers to its target and t refers to the task label. Task labels may be available in supervised learning but not in unsupervised learning. The actual data streams in as a sequence of experiences.

A sample stream in Avalanche
A sample stream of five experiences in Avalanche from the SplitMNIST benchmark (source).

Avalanche is composed of five modules:

  1. Benchmarks

The Benchmarks module provides most of the benchmark datasets, contents of the stream, task labels, etc. in an off-the-shelf strategy. By performing different class assignments, different instantiations of the same dataset with different streams can be created in Avalanche. The supported benchmarks in Avalanche are Split/Permuted/Rotated MNISTSplit Fashion MNIST, Split CIFAR 10/100/110, Split CUB200, Split ImageNet, Split TinyImageNet, Split/Permuted/Rotated Omniglot, CORe50, OpenLORIS, and Stream5. Further, Avalanche employs different benchmark-creation scenarios using which new benchmarks, new datasets can be created and/or added to the library. Benchmark instances are generally modeled as the composition of experience streams.

  1. Training

The Training module of Avalanche supports both popular continual learning algorithms and custom-developed algorithms. The strategies that Avalanche supports at present are Naive (Fine-tuning), CWR, Replay, GDumb, Cumulative, LwF, GEM, A-GEM, EWC, Synaptic Intelligence, AR1, and Joint Training. Avalanche expects more training algorithms and strategies to be included in the near future. Training can be performed with a few lines of code common to any algorithm or strategy in the library.

  1. Evaluation

The dynamic system of continual learning is tough to monitor. Avalanche provides the Evaluation module to monitor training progress effectively in real-time. Evaluation metrics of Avalanche consists of Accuracy, Loss (user-specified), Confusion Matrix, Forgetting, CPU Usage, GPU usage, RAM usage, Disk Usage, Timing, Multiply, and Accumulate. With few pieces of code, Avalanche is able to monitor and control its model’s training process.

  1. Models

Avalanche contains popular and ready-to-deploy architectures and models in the Models module. At present, Avalanche contains a collection of feedforward neural networks, convolutional neural networks, and a pre-trained MobileNet v1 architecture. In the near future, more pre-trained models will be included in the library.

  1. Logging

Logging is more important in continual learning because of the data stream’s dynamic nature. The loggers that are available in the module are Text Logger, Interactive Logger, Tensorboard Logger, Weights and Biases (W&B) Logger. Logging helps decide automatically whether to stop or start training, or to alter parameters, and so on.

Modules of Avalanche
An overview of the five main modules of Avalanche library (source).

SimpleMLP modeling on PermutedMNIST dataset

Avalanche and its dependencies can be installed using the following command. Avalanche runs on either CPU or CUDA GPU based on availability. Since Avalanche is built on top of PyTorch, a PyTorch environment has to be created.

 # Install Avalanche and its dependencies
 !pip install git+

Create the environment by importing necessary libraries and modules.

 import torch
 # use CrossEntropyLoss
 from torch.nn import CrossEntropyLoss
 # use stochastic GD optimizer
 from torch.optim import SGD
 # import the PermutedMNIST dataset
 from avalanche.benchmarks.classic import PermutedMNIST
 # import the SimpleMLP dataset
 from avalanche.models import SimpleMLP
 # we will use Naive training strategy
 from import Naive

Configure the device settings by checking for availability of CUDA GPU. Instantiate a simple multi-layer perceptron model.

 # Configure the device settings
 # check for CUDA GPU
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 # model instantiation
 model = SimpleMLP(num_classes = 10)

Prepare the benchmark dataset. Split the dataset into train and test sets.

 # Benchmark instantiation
 perm_mnist = PermutedMNIST(n_experiences=3)
 # split stream into train and test set
 train_stream = perm_mnist.train_stream
 test_stream = perm_mnist.test_stream

Instantiate optimizer and loss functions.

 # define optimizer and loss function for training
 optimizer = SGD(model.parameters(), lr=0.001, momentum=0.9)
 criterion = CrossEntropyLoss()

Devise a training strategy. Here, the Naive strategy is implemented.




 # Instantiate Continual learning strategy
 strategy = Naive(
     model, optimizer, criterion, train_mb_size=32, train_epochs=2,
     eval_mb_size=32, device=device)

Perform training, evaluate the model and log results.

 results = []
 # train and test epochs
 for train_task in train_stream:
     # training
     strategy.train(train_task, num_workers=4)
     # evaluate the model and store the results

A portion of the output:

Training may take some time based on the device configuration. Have a look at the log results.


A portion of the output:

Find here the Colab Notebook with the above code implementation.

Performance of Avalanche

Avalanche provides the strategy of clean and simple code that makes it easy to use.

Instantiation of SplitMNIST dataset in a single line of code (source).
Generating train and test set of a benchmark with the nc_scenario class (source).

Defining a strategy and performing training in a few lines of codes (source).

A sample output of a Tensorboard Logger (source).

Wrapping Up

This article discussed the newly introduced Avalanche library for the continual learning tasks. The evolution, architecture and design principles of this open-source library were discussed—further, an end-to-end training example implementation of Continuous Learning application using the Avalanche library. With more attachments of models, datasets, strategies, loggers and algorithms in the near future, Avalanche has the potential to become the top preference for those who do research and real-world deployments in continual learning.



Source: analyticsindiamag