forked from ContinualAI/avalanche
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnaive.py
59 lines (48 loc) · 1.66 KB
/
naive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import torch
from os.path import expanduser
"""
A simple example on how to use the Naive strategy.
"""
from avalanche.models import SimpleMLP
from avalanche.evaluation.metrics import (
accuracy_metrics,
loss_metrics,
)
from avalanche.training.plugins import EvaluationPlugin
from avalanche.benchmarks.classic import SplitMNIST
from avalanche.logging import InteractiveLogger
from avalanche.training.supervised import Naive
def main():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# create the benchmark
benchmark = SplitMNIST(
n_experiences=5, dataset_root=expanduser("~") + "/.avalanche/data/mnist/"
)
# choose some metrics and evaluation method
interactive_logger = InteractiveLogger()
eval_plugin = EvaluationPlugin(
accuracy_metrics(minibatch=True, epoch=True, experience=True, stream=True),
loss_metrics(minibatch=True, epoch=True, experience=True, stream=True),
loggers=[interactive_logger],
)
model = SimpleMLP(hidden_size=128)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
criterion = torch.nn.CrossEntropyLoss()
# create strategy
strategy = Naive(
model,
optimizer,
criterion,
train_epochs=1,
device=device,
train_mb_size=32,
evaluator=eval_plugin,
)
# train on the selected benchmark with the chosen strategy
for experience in benchmark.train_stream:
print("Start training on experience ", experience.current_experience)
strategy.train(experience)
strategy.eval(benchmark.test_stream[:])
if __name__ == "__main__":
main()