• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ContinualAI / avalanche / 8892830258

30 Apr 2024 09:55AM UTC coverage: 51.805% (+0.05%) from 51.751%
8892830258

push

github

web-flow
Merge pull request #1633 from AntonioCarta/updatable_objects

Updatable objects

248 of 436 new or added lines in 20 files covered. (56.88%)

9 existing lines in 1 file now uncovered.

15081 of 29111 relevant lines covered (51.81%)

0.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

25.49
/tests/training/test_regularization.py
1
import unittest
1✔
2
from types import SimpleNamespace
1✔
3

4
import torch
1✔
5
from torch.utils.data import DataLoader
1✔
6

7
from avalanche.models import SimpleMLP, MTSimpleMLP
1✔
8
from avalanche.models.utils import avalanche_model_adaptation
1✔
9
from avalanche.training.regularization import LearningWithoutForgetting
1✔
10
from tests.unit_tests_utils import get_fast_benchmark
1✔
11
import numpy as np
1✔
12
import random
1✔
13

14

15
class TestLwF(unittest.TestCase):
1✔
16
    def test_lwf(self):
1✔
17
        seed = 0
×
18
        torch.manual_seed(seed)
×
19
        np.random.seed(seed)
×
20
        random.seed(seed)
×
21
        torch.use_deterministic_algorithms(True)
×
22
        lwf = LearningWithoutForgetting()
×
23
        bm = get_fast_benchmark()
×
24

25
        teacher = SimpleMLP(input_size=6)
×
26
        model = SimpleMLP(input_size=6)
×
27
        for exp in bm.train_stream:
×
28
            mb_x, mb_y, mb_tl = list(DataLoader(exp.dataset))[0]
×
29
            mb_pred = model(mb_x)
×
30
            loss = lwf(mb_x, mb_pred, model)
×
31

32
            # non-zero loss after first task
33
            if lwf.expcount == 0:
×
34
                assert loss == 0
×
35
            else:
36
                assert loss > 0.0
×
NEW
37
            lwf.post_adapt(SimpleNamespace(model=teacher), exp)
×
38

39
        lwf = LearningWithoutForgetting()
×
40
        teacher = MTSimpleMLP(input_size=6)
×
41
        model = MTSimpleMLP(input_size=6)
×
42
        for exp in bm.train_stream:
×
43
            avalanche_model_adaptation(teacher, exp)
×
44
            avalanche_model_adaptation(model, exp)
×
45
            mb_x, mb_y, mb_tl = list(DataLoader(exp.dataset))[0]
×
46
            mb_pred = model(mb_x, task_labels=mb_tl)
×
47
            loss = lwf(mb_x, mb_pred, model)
×
48

49
            # non-zero loss after first task
50
            if lwf.expcount == 0:
×
51
                assert loss == 0
×
52
            else:
53
                assert loss > 0.0
×
54

55
                # non-zero loss for all the previous heads
56
                loss.backward()
×
57
                for tid in lwf.prev_classes_by_task.keys():
×
58
                    head = model.classifier.classifiers[str(tid)]
×
59
                    weight = head.classifier.weight
×
60
                    assert weight.grad is not None
×
61
                    assert torch.norm(weight.grad) > 0
×
62
                model.zero_grad()
×
63

NEW
64
            lwf.post_adapt(SimpleNamespace(model=teacher), exp)
×
65

66

67
if __name__ == "__main__":
1✔
68
    unittest.main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc