ContinualAI / avalanche

Avalanche: an End-to-End Library for Continual Learning based on PyTorch.
http://avalanche.continualai.org
MIT License
1.78k stars 290 forks source link

AttributeError: 'function' object has no attribute 'get_last_metrics' #1330

Closed zugexiaodui closed 1 year ago

zugexiaodui commented 1 year ago

๐Ÿ› Describe the bug When I tried to run the example code following https://avalanche.continualai.org/from-zero-to-hero-tutorial/04_training#how-to-write-a-custom-strategy, I encountered an AttributeError: 'function' object has no attribute 'get_last_metrics'. It is caused by the missing of brackets in https://github.com/ContinualAI/avalanche/blob/b225d825181df0f8a941fce51a18fef4f193150e/avalanche/training/templates/common_templates.py#L73

It can be fixed by: evaluator=default_evaluator(),

๐Ÿœ To Reproduce

from avalanche.benchmarks.utils import AvalancheConcatDataset
from avalanche.training.templates import SupervisedTemplate
from torch.optim import SGD
from torch.nn import CrossEntropyLoss
from avalanche.models import SimpleMLP
from avalanche.training.supervised import Naive, CWRStar, Replay, GDumb, Cumulative, LwF, GEM, AGEM, EWC  # and many more!
from avalanche.training.templates import SupervisedTemplate
from avalanche.benchmarks.classic import SplitMNIST

class Cumulative(SupervisedTemplate):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.dataset = None  # cumulative dataset

    def train_dataset_adaptation(self, **kwargs):
        super().train_dataset_adaptation(**kwargs)
        curr_data = self.experience.dataset
        if self.dataset is None:
            self.dataset = curr_data
        else:
            self.dataset = AvalancheConcatDataset([self.dataset, curr_data])
        self.adapted_dataset = self.dataset.train()

benchmark = SplitMNIST(n_experiences=5, seed=1)
model = SimpleMLP(num_classes=10)
optimizer = SGD(model.parameters(), lr=0.01, momentum=0.9)
criterion = CrossEntropyLoss()

strategy = Cumulative(model=model, optimizer=optimizer, criterion=criterion, train_mb_size=128)
strategy.train(benchmark.train_stream)

๐Ÿ Expected behavior The console displays the training process with progress bars.

๐Ÿž Screenshots If applicable, add screenshots to help explain your problem. image

๐Ÿฆ‹ Additional context Avalanche 0.3.1

AntonioCarta commented 1 year ago

Thanks, I believe @lrzpellegrini already fixed this but I think he still needs to do a PR.

AntonioCarta commented 1 year ago

@HamedHemati please verify this is fixed when you check the notebooks (and close it if fixed).

HamedHemati commented 1 year ago

@AntonioCarta Works fine with the master branch. I'm closing this issue.