Beispiel #1
0
    def test_agem(self):
        # SIT scenario
        model, optimizer, criterion, my_nc_benchmark = self.init_sit()
        strategy = AGEM(
            model,
            optimizer,
            criterion,
            patterns_per_exp=250,
            sample_size=256,
            train_mb_size=10,
            eval_mb_size=50,
            train_epochs=2,
        )
        self.run_strategy(my_nc_benchmark, strategy)

        # MT scenario
        strategy = AGEM(
            model,
            optimizer,
            criterion,
            patterns_per_exp=250,
            sample_size=256,
            train_mb_size=10,
            eval_mb_size=50,
            train_epochs=2,
        )
        benchmark = self.load_benchmark(use_task_labels=True)
        self.run_strategy(benchmark, strategy)
Beispiel #2
0
    def test_agem(self):
        model = self.get_model(fast_test=self.fast_test)
        optimizer = SGD(model.parameters(), lr=1e-3)
        criterion = CrossEntropyLoss()

        # SIT scenario
        my_nc_scenario = self.load_scenario(fast_test=self.fast_test)
        strategy = AGEM(model,
                        optimizer,
                        criterion,
                        patterns_per_exp=250,
                        sample_size=256,
                        train_mb_size=10,
                        eval_mb_size=50,
                        train_epochs=2)
        self.run_strategy(my_nc_scenario, strategy)

        # MT scenario
        strategy = AGEM(model,
                        optimizer,
                        criterion,
                        patterns_per_exp=250,
                        sample_size=256,
                        train_mb_size=10,
                        eval_mb_size=50,
                        train_epochs=2)
        scenario = self.load_scenario(fast_test=self.fast_test,
                                      use_task_labels=True)
        self.run_strategy(scenario, strategy)
def main(args):
    model = SimpleMLP(hidden_size=args.hs)
    optimizer = torch.optim.SGD(model.parameters(), lr=args.lr)
    criterion = torch.nn.CrossEntropyLoss()

    # check if selected GPU is available or use CPU
    assert args.cuda == -1 or args.cuda >= 0, "cuda must be -1 or >= 0."
    device = torch.device(f"cuda:{args.cuda}"
                          if torch.cuda.is_available() and
                          args.cuda >= 0 else "cpu")
    print(f'Using device: {device}')

    # create scenario
    if args.scenario == 'pmnist':
        scenario = PermutedMNIST(n_experiences=args.permutations)
    elif args.scenario == 'smnist':
        scenario = SplitMNIST(n_experiences=5, return_task_id=False)
    else:
        raise ValueError("Wrong scenario name. Allowed pmnist, smnist.")

    # 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),
        forgetting_metrics(experience=True),
        loggers=[interactive_logger])

    # create strategy
    if args.strategy == 'gem':
        strategy = GEM(model, optimizer, criterion, args.patterns_per_exp,
                       args.memory_strength, train_epochs=args.epochs,
                       device=device, train_mb_size=10, evaluator=eval_plugin)
    elif args.strategy == 'agem':
        strategy = AGEM(model, optimizer, criterion, args.patterns_per_exp,
                        args.sample_size, train_epochs=args.epochs, device=device,
                        train_mb_size=10, evaluator=eval_plugin)
    else:
        raise ValueError("Wrong strategy name. Allowed gem, agem.")
    # train on the selected scenario with the chosen strategy
    print('Starting experiment...')
    results = []
    for experience in scenario.train_stream:
        print("Start training on experience ", experience.current_experience)

        strategy.train(experience)
        print("End training on experience ", experience.current_experience)
        print('Computing accuracy on the test set')
        results.append(strategy.eval(scenario.test_stream[:]))