Putting All Together
Design Your Continual Learning Experiments
Welcome to the "Putting All Together" tutorial of the "From Zero to Hero" series. In this part we will summarize the major Avalanche features and how you can put them together for your continual learning experiments.
1
!pip install git+https://github.com/ContinualAI/avalanche.git
Copied!

๐Ÿ›ด A Comprehensive Example

Here we report a complete example of the Avalanche usage:
1
from torch.optim import SGD
2
from torch.nn import CrossEntropyLoss
3
from avalanche.benchmarks.classic import SplitMNIST
4
from avalanche.evaluation.metrics import forgetting_metrics, accuracy_metrics, \
5
loss_metrics, timing_metrics, cpu_usage_metrics, confusion_matrix_metrics, disk_usage_metrics
6
from avalanche.models import SimpleMLP
7
from avalanche.logging import InteractiveLogger, TextLogger, TensorboardLogger
8
from avalanche.training.plugins import EvaluationPlugin
9
from avalanche.training.strategies import Naive
10
โ€‹
11
scenario = SplitMNIST(n_experiences=5)
12
โ€‹
13
# MODEL CREATION
14
model = SimpleMLP(num_classes=scenario.n_classes)
15
โ€‹
16
# DEFINE THE EVALUATION PLUGIN and LOGGERS
17
# The evaluation plugin manages the metrics computation.
18
# It takes as argument a list of metrics, collectes their results and returns
19
# them to the strategy it is attached to.
20
โ€‹
21
# log to Tensorboard
22
tb_logger = TensorboardLogger()
23
โ€‹
24
# log to text file
25
text_logger = TextLogger(open('log.txt', 'a'))
26
โ€‹
27
# print to stdout
28
interactive_logger = InteractiveLogger()
29
โ€‹
30
eval_plugin = EvaluationPlugin(
31
accuracy_metrics(minibatch=True, epoch=True, experience=True, stream=True),
32
loss_metrics(minibatch=True, epoch=True, experience=True, stream=True),
33
timing_metrics(epoch=True, epoch_running=True),
34
forgetting_metrics(experience=True, stream=True),
35
cpu_usage_metrics(experience=True),
36
confusion_matrix_metrics(num_classes=scenario.n_classes, save_image=False,
37
stream=True),
38
disk_usage_metrics(minibatch=True, epoch=True, experience=True, stream=True),
39
loggers=[interactive_logger, text_logger, tb_logger]
40
)
41
โ€‹
42
# CREATE THE STRATEGY INSTANCE (NAIVE)
43
cl_strategy = Naive(
44
model, SGD(model.parameters(), lr=0.001, momentum=0.9),
45
CrossEntropyLoss(), train_mb_size=500, train_epochs=1, eval_mb_size=100,
46
evaluator=eval_plugin)
47
โ€‹
48
# TRAINING LOOP
49
print('Starting experiment...')
50
results = []
51
for experience in scenario.train_stream:
52
print("Start of experience: ", experience.current_experience)
53
print("Current Classes: ", experience.classes_in_this_experience)
54
โ€‹
55
# train returns a dictionary which contains all the metric values
56
res = cl_strategy.train(experience)
57
print('Training completed')
58
โ€‹
59
print('Computing accuracy on the whole test set')
60
# test also returns a dictionary which contains all the metric values
61
results.append(cl_strategy.eval(scenario.test_stream))
Copied!

๐Ÿค Run it on Google Colab

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