def run_vcl(hidden_size, no_epochs, data_gen, coreset_method, coreset_size=0, batch_size=None, single_head=True):
    in_dim, out_dim = data_gen.get_dims()
    x_coresets, y_coresets = [], []
    x_testsets, y_testsets = [], []

    all_acc = np.array([])

    for task_id in range(data_gen.max_iter):
        x_train, y_train, x_test, y_test = data_gen.next_task()
        x_testsets.append(x_test)
        y_testsets.append(y_test)

        # Set the readout head to train
        head = 0 if single_head else task_id
        bsize = x_train.shape[0] if (batch_size is None) else batch_size

        # Train network with maximum likelihood to initialize first model
        if task_id == 0:
            ml_model = Vanilla_NN(in_dim, hidden_size, out_dim, x_train.shape[0])
            ml_model.train(x_train, y_train, task_id, no_epochs, bsize)
            mf_weights = ml_model.get_weights()
            mf_variances = None
            ml_model.close_session()

        # Train on non-coreset data
        mf_model = MFVI_NN(in_dim, hidden_size, out_dim, x_train.shape[0], prev_means=mf_weights, prev_log_variances=mf_variances)
        mf_model.train(x_train, y_train, head, no_epochs, bsize)
        mf_weights, mf_variances = mf_model.get_weights()

        # Select coreset if needed
        if coreset_size > 0:
            if type(coreset_method) is str and coreset_method == "uncertainty_based":
                x_coresets, y_coresets, x_train, y_train = uncertainty_based(
                    mf_model, task_id, x_coresets, y_coresets, x_train, y_train, coreset_size)
            else:
                x_coresets, y_coresets, x_train, y_train = coreset_method(x_coresets, y_coresets, x_train, y_train, coreset_size)

        # Incorporate coreset data and make prediction
        acc = utils.get_scores(mf_model, x_testsets, y_testsets, x_coresets, y_coresets, hidden_size, no_epochs, single_head, batch_size)
        all_acc = utils.concatenate_results(acc, all_acc)

        mf_model.close_session()

    return all_acc
Example #2
0
def run_vcl(hidden_size, no_epochs, data_gen, coreset_method, coreset_size=0, batch_size=None, single_head=True):
  
  in_dim, out_dim = data_gen.get_dims()
  x_coresets, y_coresets = [], []
  x_testsets, y_testsets = [], []
  
  all_acc = np.array([])
  
  for task_id in range(data_gen.max_iter):
    
    x_train, y_train, x_test, y_test = data_gen.next_task()
    x_testsets.append(x_test)
    y_testsets.append(y_test)
    
    head = 0 if single_head else task_id
    bsize = x_train.shape[0] if (batch_size is None) else batch_size
    
    if task_id == 0:
      
      ml_model = VCL(in_dim, hidden_size, output_size=10).
      ml_model.train(x_train, y_train, bsize, no_epochs, task_id)
      torch.save(ml_model.state_dict(), 'my_model.pth')
    
    else: 
    
      ml_model = VCL(in_dim, hidden_size, output_size=10)
      ml_model.load_state_dict(torch.load('my_model.pth'))
      ml_model.bfc3 = vcl_model.BayesLinear(hidden_size, 10)
      ml_model.train(x_train, y_train, bsize, no_epochs, task_id)
      torch.save(ml_model.state_dict(), 'my_model.pth')
    
    if coreset_size > 0:
      x_coresets, y_coresets, x_train, y_train = coreset_method(x_coresets, y_coresets, x_train, y_train, coreset_size)
    
    acc = utils.get_scores(ml_model, x_testsets, y_testsets, x_coresets, y_coresets, hidden_size, no_epochs, single_head, batch_size)
    all_acc = utils.concatenate_results(acc, all_acc)
    
  return all_acc
Example #3
0
def run_vcl(hidden_size, no_epochs, data_gen, coreset_method, coreset_size=0, batch_size=None, single_head=True, train_info = None):
    in_dim, out_dim = data_gen.get_dims()
    x_coresets, y_coresets = [], []
    x_testsets, y_testsets = [], []

    all_acc = np.array([])
    all_acc_for_save = np.zeros((data_gen.max_iter, data_gen.max_iter), dtype=np.float32)
    
    for task_id in range(data_gen.max_iter):
        x_train, y_train, x_test, y_test = data_gen.next_task()
        x_testsets.append(x_test)
        y_testsets.append(y_test)

        # Set the readout head to train
        head = 0 if single_head else task_id
        bsize = x_train.shape[0] if (batch_size is None) else batch_size

        # Train network with maximum likelihood to initialize first model
        if task_id == 0:
            print('Vanilla NN train for task 0!')
            ml_model = Vanilla_NN(in_dim, hidden_size, out_dim, x_train.shape[0])
            ml_model.train(x_train, y_train, task_id, no_epochs, bsize)
            mf_weights = ml_model.get_weights()
            mf_variances = None
            ml_model.close_session()

        # Select coreset if needed
        if coreset_size > 0:
            x_coresets, y_coresets, x_train, y_train = coreset_method(x_coresets, y_coresets, x_train, y_train, coreset_size)
        print('Current task : {}'.format(task_id))
        # Train on non-coreset data
        mf_model = MFVI_NN(in_dim, hidden_size, out_dim, x_train.shape[0], prev_means=mf_weights, prev_log_variances=mf_variances)
        mf_model.train(x_train, y_train, head, no_epochs, bsize)
        mf_weights, mf_variances = mf_model.get_weights()

        # Incorporate coreset data and make prediction
        acc = utils.get_scores(mf_model, x_testsets, y_testsets, x_coresets, y_coresets, hidden_size, no_epochs, single_head, batch_size)
        all_acc = utils.concatenate_results(acc, all_acc)
        
        
        for u in range(task_id + 1):

            print('>>> Test on task {:2d} : acc={:5.1f}% <<<'.format(u, 100 * acc[u]))
            all_acc_for_save[task_id, u] = acc[u]

        # Save

        log_name = '{}_{}_{}_{}epochs_batch{}_{}_{}coreset_{}'.format(train_info['date'], train_info['experiment'], train_info['tasknum'], no_epochs, train_info['batch'], train_info['coreset_method'], coreset_size, train_info['trial'])
        
        if single_head:
            log_name += '_single'
        
        save_path = './results/' + log_name + '.txt'
        print('Save at ' + save_path)
        np.savetxt(save_path, all_acc_for_save, '%.4f')
        
        
        
        mf_model.close_session()

    return all_acc
Example #4
0
def run_vcl(hidden_size,
            num_epochs,
            data_generator,
            coreset_method,
            coreset_size=0,
            batch_size=None,
            single_head=True):
    """It runs the variational continual learning algorithm presented in "Variational Continual Learning" (2018) by
    Cuong V. Nguyen et al.

    :param hidden_size:
    :param num_epochs:
    :param data_generator:
    :param coreset_method:
    :param coreset_size:
    :param batch_size:
    :param single_head:
    :return:
    """
    in_dim, out_dim = data_generator.get_dims()

    # TODO: what is difference between coresets and testsets? Maybe coresets are training sets?
    x_coresets, y_coresets = [], []

    x_testsets, y_testsets = [], []

    all_acc = np.array([])

    # max_iter corresponds to the number of tasks (?)
    for task_id in range(data_generator.max_iter):
        x_train, y_train, x_test, y_test = data_generator.next_task()

        x_testsets.append(x_test)
        y_testsets.append(y_test)

        # Set the readout head to train
        head = 0 if single_head else task_id

        bsize = x_train.shape[0] if (batch_size is None) else batch_size

        # Train network with maximum likelihood to initialize first model
        if task_id == 0:
            ml_model = VanillaNN(in_dim, hidden_size, out_dim,
                                 x_train.shape[0])

            ml_model.train(x_train, y_train, task_id, num_epochs, bsize)

            mf_weights = ml_model.get_weights()

            mf_variances = None

            ml_model.close_session()

        # Select coreset if needed
        if coreset_size > 0:
            x_coresets, y_coresets, x_train, y_train = coreset_method(
                x_coresets, y_coresets, x_train, y_train, coreset_size)

        # Train on non-coreset data
        mf_model = MeanFieldVINN(in_dim,
                                 hidden_size,
                                 out_dim,
                                 x_train.shape[0],
                                 prev_means=mf_weights,
                                 prev_log_variances=mf_variances)
        mf_model.train(x_train, y_train, head, num_epochs, bsize)
        mf_weights, mf_variances = mf_model.get_weights()

        # Incorporate coreset data and make prediction
        acc = utils.get_scores(mf_model, x_testsets, y_testsets, x_coresets,
                               y_coresets, hidden_size, num_epochs,
                               single_head, batch_size)

        all_acc = utils.concatenate_results(acc, all_acc)

        mf_model.close_session()

    return all_acc
Example #5
0
def run_vcl(hidden_size,
            no_epochs,
            data_gen,
            coreset_method,
            coreset_size=0,
            batch_size=None,
            single_head=True):
    in_dim, out_dim = data_gen.get_dims()
    x_coresets, y_coresets = [], []
    x_testsets, y_testsets = [], []

    all_acc = np.array([])

    for task_id in list(range(data_gen.max_iter)):
        x_train, y_train, x_test, y_test = data_gen.next_task()
        x_testsets.append(x_test)
        y_testsets.append(y_test)

        # Set the readout head to train
        head = 0 if single_head else task_id
        bsize = x_train.shape[0] if (batch_size is None) else batch_size

        # Train network with maximum likelihood to initialize first model
        if task_id == 0:
            mf_variances = None
            mf_weights = None

        # Select coreset if needed
        if coreset_size > 0:
            x_coresets, y_coresets, x_train, y_train = coreset_method(
                x_coresets, y_coresets, x_train, y_train, coreset_size)

        # Train on non-coreset data
        mf_model = CVI_NN(in_dim,
                          hidden_size,
                          out_dim,
                          x_train.shape[0],
                          prev_means=mf_weights,
                          prev_log_variances=mf_variances)
        no_epochs = 0 if task_id == 1 else 10
        mf_model.train(x_train, y_train, head, no_epochs, bsize)
        mf_weights, mf_variances = mf_model.create_weights()
        prev_mf_weights, prev_mf_variances = mf_weights, mf_variances
        # sess = mf_model.sess
        # with sess.as_default():
        #     if not (mf_weights and mf_variances):
        #         print(sess.run(mf_weights))
        #         print(sess.run(mf_variances))
        #         mf_weights = sess.run(mf_weights)
        #         mf_variances = sess.run(mf_variances)
        #import pdb; pdb.set_trace()

        # Incorporate coreset data and make prediction
        acc = utils.get_scores(mf_model, x_testsets, y_testsets, x_coresets,
                               y_coresets, hidden_size, no_epochs, single_head,
                               batch_size)
        all_acc = utils.concatenate_results(acc, all_acc)
        print(acc)
        mf_model.close_session()

    return all_acc
def run_vcl(hidden_size,
            no_epochs,
            data_gen,
            coreset_method,
            coreset_size=0,
            batch_size=None,
            single_head=True,
            sd=0,
            lr=0.001):
    print("seed ", sd)
    in_dim, out_dim = data_gen.get_dims()
    print('in dim , out ', in_dim, out_dim)
    x_coresets, y_coresets = [], []
    x_testsets, y_testsets = [], []
    path_folder_result = create_path_file_result(lr, sd)

    all_acc = np.array([])
    print("max iter ", data_gen.max_iter)

    for task_id in range(data_gen.max_iter):
        x_train, y_train, x_test, y_test = data_gen.next_task()
        x_testsets.append(x_test)
        y_testsets.append(y_test)

        # Set the readout head to train
        head = 0 if single_head else task_id
        bsize = x_train.shape[0] if (batch_size is None) else batch_size

        # Train network with maximum likelihood to initialize first model
        if task_id == 0:
            ml_model = Vanilla_NN(in_dim, hidden_size, out_dim,
                                  x_train.shape[0])
            ml_model.train(x_train, y_train, task_id, no_epochs, bsize)
            mf_weights = ml_model.get_weights()
            mf_variances = None
            ml_model.close_session()

        # Select coreset if needed
        if coreset_size > 0:
            x_coresets, y_coresets, x_train, y_train = coreset_method(
                x_coresets, y_coresets, x_train, y_train, coreset_size)

        # Train on non-coreset data
        s_time = time.time()
        print("batch size ", bsize)
        mf_model = MFVI_NN(in_dim,
                           hidden_size,
                           out_dim,
                           x_train.shape[0],
                           prev_means=mf_weights,
                           prev_log_variances=mf_variances,
                           learning_rate=lr)
        mf_model.train(x_train, y_train, head, no_epochs, bsize)
        e_time = time.time()
        print("time train ", e_time - s_time)
        mf_weights, mf_variances = mf_model.get_weights()

        # Incorporate coreset data and make prediction
        acc = utils.get_scores(mf_model, x_testsets, y_testsets, x_coresets,
                               y_coresets, hidden_size, no_epochs, single_head,
                               batch_size)
        all_acc = utils.concatenate_results(acc, all_acc)
        print(all_acc)
        write_data_to_file(
            all_acc,
            path_folder_result + "/result_vcl_split_seed" + str(sd) + ".csv")

        mf_model.close_session()

    return all_acc