Ejemplo n.º 1
0
    def test_ewc_online(self):
        # SIT scenario
        model, optimizer, criterion, my_nc_benchmark = self.init_sit()
        strategy = EWC(model,
                       optimizer,
                       criterion,
                       ewc_lambda=0.4,
                       mode='online',
                       decay_factor=0.1,
                       train_mb_size=10,
                       eval_mb_size=50,
                       train_epochs=2)
        self.run_strategy(my_nc_benchmark, strategy)

        # MT scenario
        strategy = EWC(model,
                       optimizer,
                       criterion,
                       ewc_lambda=0.4,
                       mode='online',
                       decay_factor=0.1,
                       train_mb_size=10,
                       eval_mb_size=50,
                       train_epochs=2)
        scenario = self.load_scenario(use_task_labels=True)
        self.run_strategy(scenario, strategy)
Ejemplo n.º 2
0
    def test_ewc_online(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,
                                            use_task_labels=False)
        strategy = EWC(model,
                       optimizer,
                       criterion,
                       ewc_lambda=0.4,
                       mode='online',
                       decay_factor=0.1,
                       train_mb_size=10,
                       eval_mb_size=50,
                       train_epochs=2)
        self.run_strategy(my_nc_scenario, strategy)

        # MT scenario
        strategy = EWC(model,
                       optimizer,
                       criterion,
                       ewc_lambda=0.4,
                       mode='online',
                       decay_factor=0.1,
                       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)
Ejemplo n.º 3
0
    def test_ewc(self):
        # SIT scenario
        model, optimizer, criterion, my_nc_scenario = self.init_sit()
        strategy = EWC(model,
                       optimizer,
                       criterion,
                       ewc_lambda=0.4,
                       mode='separate',
                       train_mb_size=10,
                       eval_mb_size=50,
                       train_epochs=2)

        self.run_strategy(my_nc_scenario, strategy)

        # MT scenario
        strategy = EWC(model,
                       optimizer,
                       criterion,
                       ewc_lambda=0.4,
                       mode='separate',
                       train_mb_size=10,
                       eval_mb_size=50,
                       train_epochs=2)
        scenario = self.load_scenario(use_task_labels=True)
        self.run_strategy(scenario, strategy)
Ejemplo n.º 4
0
    def test_ewc(self):
        # SIT scenario
        model, optimizer, criterion, my_nc_benchmark = self.init_sit()
        strategy = EWC(
            model,
            optimizer,
            criterion,
            ewc_lambda=0.4,
            mode="separate",
            train_mb_size=10,
            eval_mb_size=50,
            train_epochs=2,
        )

        self.run_strategy(my_nc_benchmark, strategy)

        # MT scenario
        strategy = EWC(
            model,
            optimizer,
            criterion,
            ewc_lambda=0.4,
            mode="separate",
            train_mb_size=10,
            eval_mb_size=50,
            train_epochs=2,
        )
        benchmark = self.load_benchmark(use_task_labels=True)
        self.run_strategy(benchmark, strategy)
Ejemplo n.º 5
0
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()
    text_logger = TextLogger(open('log.txt', 'a'))

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

    # create strategy
    strategy = EWC(model,
                   optimizer,
                   criterion,
                   args.ewc_lambda,
                   args.ewc_mode,
                   decay_factor=args.decay_factor,
                   train_epochs=args.epochs,
                   device=device,
                   train_mb_size=args.minibatch_size,
                   evaluator=eval_plugin)

    # 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[:]))
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
            optimizer=Adam(model.parameters()),
            patterns_per_exp=4400,
            criterion=CrossEntropyLoss(),
            train_mb_size=128,
            train_epochs=50,
            eval_mb_size=128,
            evaluator=eval_plugin,
            device=device,
        )
    else:
        cl_strategy = EWC(
            model,
            optimizer=Adam(model.parameters()),
            ewc_lambda=0.001,
            criterion=CrossEntropyLoss(),
            train_mb_size=128,
            train_epochs=50,
            eval_mb_size=128,
            evaluator=eval_plugin,
            device=device,
        )

    # TRAINING LOOP
    print("Starting experiment...")

    os.makedirs(f"CNN1D_0inTask{task_order+1}", exist_ok=True)

    results = []

    for task_number, experience in enumerate(generic_scenario.train_stream):
        print("Start of experience: ", experience.current_experience)
Ejemplo n.º 8
0
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":
        mnist_train = MNIST(
            root=expanduser("~") + "/.avalanche/data/mnist/",
            train=True,
            download=True,
            transform=ToTensor(),
        )
        mnist_test = MNIST(
            root=expanduser("~") + "/.avalanche/data/mnist/",
            train=False,
            download=True,
            transform=ToTensor(),
        )
        scenario = nc_benchmark(mnist_train,
                                mnist_test,
                                5,
                                task_labels=False,
                                seed=1234)
    else:
        raise ValueError("Wrong scenario name. Allowed pmnist, smnist.")

    # choose some metrics and evaluation method
    interactive_logger = InteractiveLogger()
    tensorboard_logger = TensorboardLogger()
    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, stream=True),
        bwt_metrics(experience=True, stream=True),
        loggers=[interactive_logger, tensorboard_logger],
    )

    # create strategy
    strategy = EWC(
        model,
        optimizer,
        criterion,
        args.ewc_lambda,
        args.ewc_mode,
        decay_factor=args.decay_factor,
        train_epochs=args.epochs,
        device=device,
        train_mb_size=args.minibatch_size,
        evaluator=eval_plugin,
    )

    # 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[:]))
Ejemplo n.º 9
0
    cl_strategy = GEM(model,
                      Adam(model.parameters(), lr=0.001),
                      CrossEntropyLoss(),
                      patterns_per_exp=150,
                      memory_strength=0.5,
                      train_mb_size=args.batch_size,
                      train_epochs=args.num_epochs,
                      eval_mb_size=args.batch_size * 2,
                      evaluator=eval_plugin,
                      device=device)
elif (args.cl_strategy == "EWC"):
    cl_strategy = EWC(model,
                      Adam(model.parameters(), lr=0.001),
                      CrossEntropyLoss(),
                      ewc_lambda=0.5,
                      mode="separate",
                      train_mb_size=args.batch_size,
                      train_epochs=args.num_epochs,
                      eval_mb_size=args.batch_size * 2,
                      evaluator=eval_plugin,
                      device=device)
elif (args.cl_strategy == "AR1"):
    cl_strategy = AR1(model,
                      Adam(model.parameters(), lr=0.001),
                      CrossEntropyLoss(),
                      ewc_lambda=0.5,
                      train_mb_size=args.batch_size,
                      train_epochs=args.num_epochs,
                      eval_mb_size=args.batch_size * 2,
                      evaluator=eval_plugin,
                      device=device)
elif (args.cl_strategy == "GDumb"):