Esempio n. 1
0
    def test_multihead_head_creation(self):
        # Check if the optimizer is updated correctly
        # when heads are created and updated.
        model = MTSimpleMLP(input_size=6, hidden_size=10)
        optimizer = SGD(model.parameters(), lr=1e-3)
        criterion = CrossEntropyLoss()
        benchmark = get_fast_benchmark(use_task_labels=True, shuffle=False)

        strategy = Naive(
            model,
            optimizer,
            criterion,
            train_mb_size=100,
            train_epochs=1,
            eval_mb_size=100,
            device="cpu",
        )
        strategy.evaluator.loggers = [TextLogger(sys.stdout)]
        print(
            "Current Classes: ",
            benchmark.train_stream[4].classes_in_this_experience,
        )
        print(
            "Current Classes: ",
            benchmark.train_stream[0].classes_in_this_experience,
        )

        # head creation
        strategy.train(benchmark.train_stream[0])
        w_ptr = model.classifier.classifiers["0"].classifier.weight.data_ptr()
        b_ptr = model.classifier.classifiers["0"].classifier.bias.data_ptr()
        opt_params_ptrs = [
            w.data_ptr() for group in optimizer.param_groups
            for w in group["params"]
        ]
        assert w_ptr in opt_params_ptrs
        assert b_ptr in opt_params_ptrs

        # head update
        strategy.train(benchmark.train_stream[4])
        w_ptr_t0 = model.classifier.classifiers[
            "0"].classifier.weight.data_ptr()
        b_ptr_t0 = model.classifier.classifiers["0"].classifier.bias.data_ptr()
        w_ptr_new = model.classifier.classifiers[
            "4"].classifier.weight.data_ptr()
        b_ptr_new = model.classifier.classifiers["4"].classifier.bias.data_ptr(
        )
        opt_params_ptrs = [
            w.data_ptr() for group in optimizer.param_groups
            for w in group["params"]
        ]

        assert w_ptr not in opt_params_ptrs  # head0 has been updated
        assert b_ptr not in opt_params_ptrs  # head0 has been updated
        assert w_ptr_t0 in opt_params_ptrs
        assert b_ptr_t0 in opt_params_ptrs
        assert w_ptr_new in opt_params_ptrs
        assert b_ptr_new in opt_params_ptrs
Esempio n. 2
0
def main(args):

    # Config
    device = torch.device(f"cuda:{args.cuda}" if torch.cuda.is_available()
                          and args.cuda >= 0 else "cpu")
    # model
    model = MTSimpleMLP()

    # CL Benchmark Creation
    scenario = SplitMNIST(n_experiences=5, return_task_id=True)
    train_stream = scenario.train_stream
    test_stream = scenario.test_stream

    # Prepare for training & testing
    optimizer = Adam(model.parameters(), lr=0.01)
    criterion = CrossEntropyLoss()

    # choose some metrics and evaluation method
    interactive_logger = InteractiveLogger()

    eval_plugin = EvaluationPlugin(
        accuracy_metrics(minibatch=False,
                         epoch=True,
                         experience=True,
                         stream=True),
        forgetting_metrics(experience=True),
        loggers=[interactive_logger],
    )

    # Choose a CL strategy
    strategy = EWC(
        model=model,
        optimizer=optimizer,
        criterion=criterion,
        train_mb_size=128,
        train_epochs=3,
        eval_mb_size=128,
        device=device,
        evaluator=eval_plugin,
        ewc_lambda=0.4,
    )

    # train and test loop
    for train_task in train_stream:
        strategy.train(train_task)
        strategy.eval(test_stream)
    def test_reproduce_old_cumulative_strategy(self):
        mt_model = MTSimpleMLP(input_size=6, hidden_size=10)
        criterion = CrossEntropyLoss()
        ######################################
        # OLD BASE STRATEGY
        ######################################
        old_model = copy.deepcopy(mt_model)
        p_old = StoreLosses()
        old_strategy = OldCumulative(
            old_model,
            SGD(old_model.parameters(), lr=0.01),
            criterion,
            train_epochs=2,
            plugins=[p_old],
            evaluator=None,
            train_mb_size=128,
        )
        torch.manual_seed(42)
        old_strategy.train(self.benchmark.train_stream, shuffle=False)
        old_losses = np.array(p_old.values)

        ######################################
        # NEW SUPERVISED STRATEGY
        ######################################
        new_model = copy.deepcopy(mt_model)
        p_new = StoreLosses()
        new_strategy = Cumulative(
            new_model,
            SGD(new_model.parameters(), lr=0.01),
            criterion,
            train_epochs=2,
            plugins=[p_new],
            evaluator=None,
            train_mb_size=128,
        )
        torch.manual_seed(42)
        new_strategy.train(self.benchmark.train_stream, shuffle=False)
        new_losses = np.array(p_new.values)

        print(old_losses)
        print(new_losses)
        np.testing.assert_allclose(old_losses, new_losses)
        for par_old, par_new in zip(
            old_model.parameters(), new_model.parameters()
        ):
            np.testing.assert_allclose(par_old.detach(), par_new.detach())