Models
First things first: let's start with a good model!
Welcome to the "Models" tutorial of the "From Zero to Hero" series. In this notebook we will talk about the features offered by the models Avalanche sub-module.

Support for pytorch Modules

Every continual learning experiment needs a model to train incrementally. You can use any torch.nn.Module, even pretrained models. The models sub-module provides for you the most commonly used architectures in the CL literature.
You can use any model provided in the Pytorch official ecosystem models as well as the ones provided by pytorchcv!
1
!pip install git+https://github.com/ContinualAI/avalanche.git
Copied!
1
from avalanche.models import SimpleCNN
2
from avalanche.models import SimpleMLP
3
from avalanche.models import SimpleMLP_TinyImageNet
4
from avalanche.models import MobilenetV1
5
โ€‹
6
model = SimpleCNN()
7
print(model)
Copied!

Dynamic Model Expansion

A continual learning model may change over time. As an example, a classifier may add new units for previously unseen classes, while progressive networks add a new set units after each experience. Avalanche provides DynamicModules to support these use cases. DynamicModules are torch.nn.Modules that provide an addition method, adaptation, that is used to update the model's architecture. The method takes a single argument, the data from the current experience.
For example, an IncrementalClassifier updates the number of output units:
1
from avalanche.benchmarks import SplitMNIST
2
from avalanche.models import IncrementalClassifier
3
โ€‹
4
benchmark = SplitMNIST(5, shuffle=False)
5
model = IncrementalClassifier(in_features=784)
6
โ€‹
7
print(model)
8
for exp in benchmark.train_stream:
9
model.adaptation(exp.dataset)
10
print(model)
Copied!
As you can see, after each call to the adaptation method, the model adds 2 new units to account for the new classes. Notice that no learning occurs at this point since the method only modifies the model's architecture.
Keep in mind that when you use Avalanche strategies you don't have to call the adaptation yourself. Avalanche strategies automatically call the model's adaptation and update the optimizer to include the new parameters.

Multi-Task models

Some models, such as multi-head classifiers, are designed to exploit task labels. In Avalanche, such models are implemented as MultiTaskModules. These are dynamic models (since they need to be updated whenever they encounter a new task) that have an additional task_labels argument in their forward method. task_labels is a tensor with a task id for each sample.
1
from avalanche.benchmarks import SplitMNIST
2
from avalanche.models import MultiHeadClassifier
3
โ€‹
4
benchmark = SplitMNIST(5, shuffle=False, return_task_id=True)
5
model = MultiHeadClassifier(in_features=784)
6
โ€‹
7
print(model)
8
for exp in benchmark.train_stream:
9
model.adaptation(exp.dataset)
10
print(model)
Copied!
When you use a MultiHeadClassifier, a new head is initialized whenever a new task is encountered. Avalanche strategies automatically recognizes multi-task modules and provide the task labels to them.

How to define a multi-task Module

If you want to define a custom multi-task module you need to override two methods: adaptation (if needed), and forward_single_task. The forward method of the base class will split the mini-batch by task-id and provide single task mini-batches to forward_single_task.
1
from avalanche.models import MultiTaskModule
2
โ€‹
3
class CustomMTModule(MultiTaskModule):
4
def __init__(self, in_features, initial_out_features=2):
5
super().__init__()
6
โ€‹
7
def adaptation(self, dataset):
8
super().adaptation(dataset)
9
# your adaptation goes here
10
โ€‹
11
def forward_single_task(self, x, task_label):
12
# your forward goes here.
13
# task_label is a single integer
14
# the mini-batch is split by task-id inside the forward method.
15
pass
Copied!
Alternatively, if you only want to convert a single-head model into a multi-head model, you can use the as_multitask wrapper, which converts the model for you.
1
from avalanche.models import as_multitask
2
โ€‹
3
model = SimpleCNN()
4
print(model)
5
โ€‹
6
mt_model = as_multitask(model, 'classifier')
7
print(mt_model)
Copied!

๐Ÿค Run it on Google Colab

You can run this chapter and play with it on Google Colaboratory: โ€‹
โ€‹
โ€‹
Last modified 10d ago