Exemple #1
0
    def test_cuda_predict(self, reg_data):
        Xtr, Ytr, Xts, Yts = reg_data
        kernel = kernels.GaussianKernel(20.0)

        def error_fn(t, p):
            return torch.sqrt(torch.mean((t - p)**2)), "RMSE"

        opt = FalkonOptions(use_cpu=False,
                            keops_active="no",
                            debug=True,
                            min_cuda_pc_size_64=1,
                            min_cuda_iter_size_64=1)

        flk = Falkon(kernel=kernel,
                     penalty=1e-6,
                     M=500,
                     seed=10,
                     options=opt,
                     error_fn=error_fn)
        flk.fit(Xtr, Ytr, Xts=Xts, Yts=Yts)
        flk.to("cuda:0")

        cuda_ts_preds = flk.predict(Xts.to("cuda:0"))
        cuda_tr_preds = flk.predict(Xtr.to("cuda:0"))
        assert cuda_ts_preds.device.type == "cuda"
        assert cuda_ts_preds.shape == (Yts.shape[0], 1)
        ts_err = error_fn(cuda_ts_preds.cpu(), Yts)[0]
        tr_err = error_fn(cuda_tr_preds.cpu(), Ytr)[0]
        assert tr_err < ts_err
        assert ts_err < 2.5
Exemple #2
0
    def test_compare_cuda_cpu(self, reg_data):
        Xtr, Ytr, Xts, Yts = reg_data
        kernel = kernels.GaussianKernel(20.0)

        def error_fn(t, p):
            return torch.sqrt(torch.mean((t - p)**2)).item(), "RMSE"

        opt_cpu = FalkonOptions(use_cpu=True, keops_active="no", debug=True)
        flk_cpu = Falkon(kernel=kernel,
                         penalty=1e-6,
                         M=500,
                         seed=10,
                         options=opt_cpu,
                         maxiter=10,
                         error_fn=error_fn)
        flk_cpu.fit(Xtr, Ytr, Xts=Xts, Yts=Yts)
        opt_gpu = FalkonOptions(use_cpu=False, keops_active="no", debug=True)
        flk_gpu = Falkon(kernel=kernel,
                         penalty=1e-6,
                         M=500,
                         seed=10,
                         options=opt_gpu,
                         maxiter=10,
                         error_fn=error_fn)
        flk_gpu.fit(Xtr, Ytr, Xts=Xts, Yts=Yts)

        np.testing.assert_allclose(flk_cpu.alpha_.numpy(),
                                   flk_gpu.alpha_.numpy())
Exemple #3
0
def main(path, semi_supervised, kernel_function, max_iterations, gpu):
    # loading dataset as ndarray
    dataset = np.load(path).astype(np.float32)
    print("Dataset loaded ({} points, {} features per point)".format(
        dataset.shape[0], dataset.shape[1] - 1))

    # adjusting label's range {-1, 1}
    dataset[:, 0] = (2 * dataset[:, 0]) - 1

    # defining train and test set
    x_train, x_test, y_train, y_test = train_test_split(dataset[:, 1:],
                                                        dataset[:, 0],
                                                        test_size=0.2,
                                                        random_state=None)
    print("Train and test set defined (test: {} + , train: {} +, {} -)".format(
        np.sum(y_test == 1.), np.sum(y_train == 1.), np.sum(y_train == -1.)))

    # removing some labels (if semi_supervised > 0)
    labels_removed = int(len(y_train) * semi_supervised)
    if labels_removed > 0:
        y_train[np.random.choice(len(y_train), labels_removed,
                                 replace=False)] = 0
        print("{} labels removed".format(labels_removed))

    # removing the mean and scaling to unit variance
    scaler = StandardScaler()
    scaler.fit(x_train)
    x_train = scaler.transform(x_train)
    x_test = scaler.transform(x_test)
    print("Standardization done")

    # choosing kernel function
    kernel = Kernel(kernel_function=kernel_function, gpu=gpu)

    # fitting falkon
    print("Starting falkon fitting routine...")
    falkon = Falkon(nystrom_length=10000,
                    gamma=1e-6,
                    kernel_fun=kernel.get_kernel(),
                    kernel_param=4,
                    optimizer_max_iter=max_iterations,
                    gpu=gpu)
    # parameters = {'nystrom_length': [10000, ], 'gamma': [1e-6, ], 'kernel_param': [4, ]}
    # gsht = GridSearchCV(falkon, param_grid=parameters, scoring=make_scorer(roc_auc_score), cv=3, verbose=3)
    # gsht.fit(x_train, y_train)
    start_ = time()
    falkon.fit(x_train, y_train)
    print("Fitting time: {:.3f} seconds".format(time() - start_))

    # printing some information of the best model
    # print("Best model information: {} params, {:.3f} time (sec)".format(gsht.best_params_, gsht.refit_time_))

    # testing falkon
    print("Starting falkon testing routine...")
    y_pred = falkon.predict(x_test)
    accuracy = accuracy_score(y_test, np.sign(y_pred))
    auc = roc_auc_score(y_test, y_pred)
    print("Accuracy: {:.3f} - AUC: {:.3f}".format(accuracy, auc))
Exemple #4
0
def main(path, kernel_function, max_iterations, gpu):
    # loading dataset as ndarray
    dataset = np.load(path).astype(np.float32)
    print("Dataset loaded ({} points, {} features per point)".format(
        dataset.shape[0], dataset.shape[1] - 1))
    dataset = dataset[1:, :]
    np.random.shuffle(dataset)
    # defining train and test set
    # x_train, x_test, y_train, y_test = train_test_split(dataset[:, 1:], dataset[:, 0], test_size=51630, random_state=None)
    x_train = dataset[0:15000, 1:]
    x_test = dataset[15000:16000, 1:]
    y_train = dataset[0:15000, 0]
    y_test = dataset[15000:16000, 0]
    print(x_test)
    print("Train and test set defined")

    # removing the mean and scaling to unit variance
    #x_scaler = StandardScaler()
    y_scaler = StandardScaler()
    #x_scaler.fit(x_train)
    y_scaler.fit(y_train.reshape(-1, 1))
    #x_train = x_scaler.transform(x_train)
    #x_test = x_scaler.transform(x_test)
    y_train = y_scaler.transform(y_train.reshape(-1, 1)).reshape(-1)
    y_test = y_scaler.transform(y_test.reshape(-1, 1)).reshape(-1)
    print("Standardization done")

    # choosing kernel function
    kernel = Kernel(kernel_function=kernel_function, gpu=gpu)

    # fitting falkon
    print("Starting falkon fit routine...")
    falkon = Falkon(nystrom_length=10000,
                    gamma=1e-19,
                    kernel_fun=kernel.get_kernel(),
                    kernel_param=19,
                    optimizer_max_iter=max_iterations,
                    gpu=gpu)
    start_ = time()
    falkon.fit(x_train, y_train, sample_weights=1.)
    print("Fitting time: {:.3f} seconds".format(time() - start_))

    # testing falkon
    print("Starting falkon testing routine...")
    y_pred = falkon.predict(x_test)
    print(np.round(inv_transform(y_scaler, y_pred)))
    print(np.round(inv_transform(y_scaler, y_test)))
    x = (np.equal(np.round(inv_transform(y_scaler, y_pred)),
                  np.round(inv_transform(y_scaler, y_test))))
    print(collections.Counter(x))
    mse = mean_squared_error(inv_transform(y_scaler, y_test),
                             inv_transform(y_scaler, y_pred))
    print("Mean squared error: {:.3f}".format(mse))
Exemple #5
0
    def test_classif(self, cls_data):
        X, Y = cls_data
        kernel = kernels.GaussianKernel(2.0)

        def error_fn(t, p):
            return 100 * torch.sum(t * p <= 0).to(
                torch.float32) / t.shape[0], "c-err"

        opt = FalkonOptions(use_cpu=True, keops_active="no", debug=True)

        flk = Falkon(kernel=kernel,
                     penalty=1e-6,
                     M=500,
                     seed=10,
                     options=opt,
                     error_fn=error_fn)
        flk.fit(X, Y)
        preds = flk.predict(X)
        err = error_fn(preds, Y)[0]
        assert err < 5
Exemple #6
0
    def test_regression(self, reg_data):
        Xtr, Ytr, Xts, Yts = reg_data
        kernel = kernels.GaussianKernel(20.0)

        def error_fn(t, p):
            return torch.sqrt(torch.mean((t - p)**2)).item(), "RMSE"

        opt = FalkonOptions(use_cpu=True, keops_active="no", debug=True)
        flk = Falkon(kernel=kernel,
                     penalty=1e-6,
                     M=500,
                     seed=10,
                     options=opt,
                     maxiter=10)
        flk.fit(Xtr, Ytr, Xts=Xts, Yts=Yts)

        assert flk.predict(Xts).shape == (Yts.shape[0], 1)
        ts_err = error_fn(flk.predict(Xts), Yts)[0]
        tr_err = error_fn(flk.predict(Xtr), Ytr)[0]
        assert tr_err < ts_err
        assert ts_err < 2.5
Exemple #7
0
    def test_multiclass(self, multicls_data):
        X, Y = multicls_data
        kernel = kernels.GaussianKernel(10.0)

        def error_fn(t, p):
            t = torch.argmax(t, dim=1)
            p = torch.argmax(p, dim=1)
            return torch.mean((t.reshape(-1, ) != p.reshape(-1, )).to(
                torch.float64)), "multic-err"

        opt = FalkonOptions(use_cpu=True, keops_active="no", debug=True)

        flk = Falkon(kernel=kernel,
                     penalty=1e-6,
                     M=500,
                     seed=10,
                     options=opt,
                     error_fn=error_fn)
        flk.fit(X, Y)
        preds = flk.predict(X)
        err = error_fn(preds, Y)[0]
        assert err < 0.23
class FALKONWrapper(ca.ClassifierAbstract):
    def __init__(self, cfg_path=None, is_rpn=False, is_segmentation=False):
        if cfg_path is not None:
            self.cfg = yaml.load(open(cfg_path), Loader=yaml.FullLoader)
            if is_rpn:
                self.cfg = self.cfg['RPN']
        if not is_segmentation:
            opts = self.cfg['ONLINE_REGION_CLASSIFIER']['CLASSIFIER']
        else:
            opts = self.cfg['ONLINE_SEGMENTATION']['CLASSIFIER']

        if 'sigma' in opts:
            self.sigma = opts['sigma']
        else:
            print(
                'Sigma not given for creating Falkon, default value is used.')
            self.sigma = 5

        if 'lambda' in opts:
            self.lam = opts['lambda']
        else:
            print(
                'Lambda not given for creating Falkon, default value is used.')
            self.lam = 0.001

        self.kernel = None
        self.nyst_centers = opts['M']

    def train(self, X, y, sigma=None, lam=None):
        # Set sigma and lambda
        if sigma is None:
            sigma = self.sigma
        if lam is None:
            lam = self.lam
        # Initialize kernel
        self.kernel = kernels.GaussianKernel(sigma=sigma)
        # Compute indices of nystrom centers
        indices = self.compute_indices_selection(y)
        center_selector = MyCenterSelector(indices)
        opt = FalkonOptions(min_cuda_iter_size_32=0,
                            min_cuda_iter_size_64=0,
                            keops_active="no")
        # Initialize FALKON model
        self.model = Falkon(kernel=self.kernel,
                            penalty=lam,
                            M=len(indices),
                            center_selection=center_selector,
                            options=opt)
        # Train FALKON model
        if self.model is not None:
            self.model.fit(X, y)
        else:
            print('Model is None in trainRegionClassifier function')
            sys.exit(0)

        return copy.deepcopy(self.model)

    def predict(self, model, X_np, y=None):
        # Predict values
        if y is not None:
            predictions = model.predict(X_np, y)
        else:
            predictions = model.predict(X_np)

        return predictions

    def test(self):
        pass

    def compute_indices_selection(self, y):
        # Choose at most M/2 nystrom centers from positive training examples
        positive_indices = (y == 1).nonzero()
        if positive_indices.size()[0] > int(self.nyst_centers / 2):
            positive_indices = positive_indices[torch.randint(
                positive_indices.size()[0], (int(self.nyst_centers / 2), ))]
        # Fill the centers with negative examples training examples
        negative_indices = (y == -1).nonzero()
        if negative_indices.size(
        )[0] > self.nyst_centers - positive_indices.size()[0]:
            negative_indices = negative_indices[torch.randint(
                negative_indices.size()[0],
                (self.nyst_centers - positive_indices.size()[0], ))]

        indices = torch.cat((positive_indices, negative_indices), dim=0)

        return indices.squeeze().tolist()
Exemple #9
0
def main(path, n_labeled, kernel_function, max_iterations, gpu):
    # loading dataset as ndarray
    dataset = np.load(path).astype(np.float32)
    print("Dataset loaded ({} points, {} features per point)".format(dataset.shape[0], dataset.shape[1] - 1))

    # adjusting label's range {-1, 1}
    dataset[:, 0] = (2 * dataset[:, 0]) - 1

    # defining train and test set
    x_train, x_test, y_train, y_test = train_test_split(dataset[:, 1:], dataset[:, 0], test_size=50000, random_state=None)
    print("Train and test set defined (test: {} + , train: {} +, {} -)".format(np.sum(y_test == 1.), np.sum(y_train == 1.), np.sum(y_train == -1.)))

    # removing the mean and scaling to unit variance
    scaler = StandardScaler()
    scaler.fit(x_train)
    x_train = scaler.transform(x_train)
    x_test = scaler.transform(x_test)
    print("Standardization done")

    # defining labeled and unlabeled set
    labeled = np.random.choice(np.where(y_train == 1)[0], size=int(n_labeled/2), replace=False)
    labeled = np.concatenate((labeled, np.random.choice(np.where(y_train == -1)[0], size=int(n_labeled/2), replace=False)), axis=0)
    x_labeled = x_train[labeled, :].copy()  # train
    y_labeled = y_train[labeled].copy()  # train
    x_train = np.delete(x_train, obj=labeled, axis=0)
    y_train = np.delete(y_train, obj=labeled)

    x_unlabeled = x_train.copy()  # test
    y_unlabeled = y_train.copy()  # test

    # choosing kernel function
    kernel = Kernel(kernel_function=kernel_function, gpu=gpu)

    # fitting falkon (semi-supervised scenario)
    best_score = -np.infty
    best_gamma, best_ker_param = None, None
    print("First training...")
    for gamma in [1e-6]:
        for ker_param in [4]:
            falkon = Falkon(nystrom_length=x_labeled.shape[0], gamma=gamma, kernel_fun=kernel.get_kernel(), kernel_param=ker_param, optimizer_max_iter=max_iterations, gpu=gpu)
            falkon.fit(x_labeled,  y_labeled)
            score = accuracy_score(y_labeled, np.sign(falkon.predict(x_labeled)))
            best_score, best_gamma, best_ker_param = (score, gamma, ker_param) if (score > best_score) else (best_score, best_gamma, best_ker_param)

    print("  -> [debug info] best score {:.3f} -- best gamma {} -- best kernel_param {}".format(best_score, best_gamma, best_ker_param))
    falkon = Falkon(nystrom_length=x_labeled.shape[0], gamma=best_gamma, kernel_fun=kernel.get_kernel(), kernel_param=best_ker_param, optimizer_max_iter=max_iterations, gpu=gpu)
    falkon.fit(x_labeled, y_labeled)
    functional_margin = falkon.predict(x_test)

    print(np.sum(functional_margin >= 0), np.sum(functional_margin < 0))

    print("Starting falkon testing routine...")
    accuracy = accuracy_score(y_test, np.sign(functional_margin))
    auc = roc_auc_score(y_test, functional_margin)
    print("Accuracy: {:.3f} - AUC: {:.3f}".format(accuracy, auc))

    print("Annealing loop...")
    functional_margin = falkon.predict(x_unlabeled)
    falkon = Falkon(nystrom_length=10000, gamma=best_gamma, kernel_fun=kernel.get_kernel(), kernel_param=best_ker_param, optimizer_max_iter=max_iterations, gpu=gpu)
    balance_constraint = (2 * 0.5) - 1  # 2r - 1
    start_ = time()
    for idx, weight in enumerate([0.1, 0.25, 0.5, 0.75, 1.]):
        print(" -> iteration {}".format(idx+1))

        lam0 = ((2/x_unlabeled.shape[0])*np.sum(functional_margin)) - (2*balance_constraint)
        y_u, lam, iter = labelling(functional_margin, balance_constraint, lam0, 1., int(x_unlabeled.shape[0]*0.005))
        print("  -> [debug info] balance constraint {:.2}".format(np.divide(np.sum(y_u), x_unlabeled.shape[0])))
        print("  -> [debug info] lambda from {:.3e} to {:.3e} in {} iterations".format(lam0, lam, iter+1))
        print("  -> [debug info] wrong labels {}".format(np.sum(y_u != y_unlabeled)))

        sample_weights = ([1.] * x_labeled.shape[0]) + ([weight] * x_unlabeled.shape[0])
        falkon.fit(np.vstack((x_labeled, x_unlabeled)), np.concatenate((y_labeled, y_u)).astype(np.float32), sample_weights=sample_weights)
        functional_margin = falkon.predict(x_unlabeled)
    print("Annealing done in {:.3} seconds".format(time()-start_))

    # testing semi-supervised falkon
    print("Starting falkon testing routine...")
    functional_margin = falkon.predict(x_test)
    accuracy = accuracy_score(y_test, np.sign(functional_margin))
    auc = roc_auc_score(y_test, functional_margin)
    print("Accuracy: {:.3f} - AUC: {:.3f}".format(accuracy, auc))
Exemple #10
0
def main(path, n_labeled, kernel_function, max_iterations, gpu):
    # loading dataset as ndarray
    dataset = np.load(path).astype(np.float32)
    print("Dataset loaded ({} points, {} features per point)".format(dataset.shape[0], dataset.shape[1] - 1))

    scaler = StandardScaler()
    scaler.fit(dataset[:, 1:])
    dataset[:, 1:] = scaler.transform(dataset[:, 1:])
    print("Standardization done")

    # defining labeled, unlabeled and validation set
    labeled = np.random.choice(np.where(dataset[:, 0] == 1)[0], size=int(n_labeled/2), replace=False)
    labeled = np.concatenate((labeled, np.random.choice(np.where(dataset[:, 0] == -1)[0], size=int(n_labeled/2), replace=False)), axis=0)
    x_labeled = dataset[labeled, 1:].copy()  # train
    y_labeled = dataset[labeled, 0].copy()  # train
    dataset = np.delete(dataset, obj=labeled, axis=0)

    # validation = np.random.choice(np.where(dataset[:, 0] == 1)[0], size=int(dataset.shape[0]*0.06), replace=False)
    # validation = np.concatenate((validation, np.random.choice(np.where(dataset[:, 0] == -1)[0], size=int(dataset.shape[0]*0.06), replace=False)), axis=0)
    # x_validation = dataset[validation, 1:].copy()  # validation
    # y_validation = dataset[validation, 0].copy()  # validation
    # dataset = np.delete(dataset, obj=validation, axis=0)

    x_unlabeled = dataset[:, 1:].copy()  # test
    y_unlabeled = dataset[:, 0].copy()  # test

    # print("train: {} - validation: {} - test: {}".format(x_labeled.shape[0], x_validation.shape[0], x_unlabeled.shape[0]))

    # choosing kernel function
    kernel = Kernel(kernel_function=kernel_function, gpu=gpu)

    print(x_labeled.shape, y_labeled.shape, x_unlabeled.shape, y_unlabeled.shape)
    # fitting falkon (semi-supervised scenario)
    best_score = -np.infty
    best_gamma, best_ker_param = None, None
    print("First training...")
    for gamma in [1e-6]:
        for ker_param in [0.5]:
            falkon = Falkon(nystrom_length=x_labeled.shape[0], gamma=gamma, kernel_fun=kernel.get_kernel(), kernel_param=ker_param, optimizer_max_iter=max_iterations, gpu=gpu)
            falkon.fit(x_labeled,  y_labeled)
            score = accuracy_score(y_labeled, np.sign(falkon.predict(x_labeled)))
            best_score, best_gamma, best_ker_param = (score, gamma, ker_param) if (score > best_score) else (best_score, best_gamma, best_ker_param)

    print("  -> [debug info] best score {:.3f} -- best gamma {} -- best kernel_param {}".format(best_score, best_gamma, best_ker_param))
    falkon = Falkon(nystrom_length=x_labeled.shape[0], gamma=best_gamma, kernel_fun=kernel.get_kernel(), kernel_param=best_ker_param, optimizer_max_iter=max_iterations, gpu=gpu)
    falkon.fit(x_labeled, y_labeled)
    functional_margin = falkon.predict(x_unlabeled)

    print(np.sum(functional_margin >= 0), np.sum(functional_margin < 0))

    print("Starting falkon testing routine...")
    accuracy = accuracy_score(y_unlabeled, np.sign(functional_margin))
    auc = roc_auc_score(y_unlabeled, functional_margin)
    print("Accuracy: {:.3f} - AUC: {:.3f}".format(accuracy, auc))

    # plot_2d_dataset(x_labeled, x_validation, y_labeled, falkon.predict(x_validation), filepath='./fig.png')
    # plot_2d_dataset(x_labeled, x_unlabeled, y_labeled, functional_margin, filepath='./fig0.png')

    print("Annealing loop...")
    falkon = Falkon(nystrom_length=10000, gamma=best_gamma, kernel_fun=kernel.get_kernel(), kernel_param=best_ker_param, optimizer_max_iter=max_iterations, gpu=gpu)
    balance_constraint = (2 * 0.5) - 1  # 2r - 1
    start_ = time()
    for idx, weight in enumerate([0.1, 0.25, 0.5, 1.]):
        print(" -> iteration {}".format(idx+1))

        lam0 = ((2/x_unlabeled.shape[0])*np.sum(functional_margin)) - (2*balance_constraint)
        y_u, lam, iter = labelling(functional_margin, balance_constraint, lam0, 1., int(x_unlabeled.shape[0]*0.005))
        print("  -> [debug info] balance constraint {:.2}".format(np.divide(np.sum(y_u), x_unlabeled.shape[0])))
        print("  -> [debug info] lambda from {:.3e} to {:.3e} in {} iterations".format(lam0, lam, iter+1))
        print("  -> [debug info] wrong labels {}".format(np.sum(y_u != y_unlabeled)))

        sample_weights = ([1.] * x_labeled.shape[0]) + ([weight] * x_unlabeled.shape[0])
        falkon.fit(np.vstack((x_labeled, x_unlabeled)), np.concatenate((y_labeled, y_u)).astype(np.float32), sample_weights=sample_weights)
        functional_margin = falkon.predict(x_unlabeled)
    print("Annealing done in {:.3} seconds".format(time()-start_))

    # testing semi-supervised falkon
    print("Starting falkon testing routine...")
    accuracy = accuracy_score(y_unlabeled, np.sign(functional_margin))
    auc = roc_auc_score(y_unlabeled, functional_margin)
    print("Accuracy: {:.3f} - AUC: {:.3f}".format(accuracy, auc))
Exemple #11
0
def main(path, kernel_function, max_iterations, gpu):
    # loading dataset as ndarray
    dataset = np.load(path).astype(np.float32)
    print("Dataset loaded ({} points, {} features per point)".format(
        dataset.shape[0], dataset.shape[1] - 1))

    # defining train and test set
    x_train = dataset[0:463715, 1:]
    x_test = dataset[463715:515345, 1:]
    y_train = dataset[0:463715, 0]
    y_test = dataset[463715:515345, 0]
    print("Train and test set defined")

    # creating the unsupervised part of the dataset (using x_train)
    labeled_ids, unlabeled_ids = train_test_split(range(x_train.shape[0]),
                                                  test_size=0.7,
                                                  random_state=42)
    x_labeled, y_labeled = x_train[labeled_ids, :], y_train[labeled_ids]
    x_unlabeled, y_unlabeled = x_train[
        unlabeled_ids, :], y_train[unlabeled_ids]
    print("Labeled examples {}, Unlabeled examples {}".format(
        x_labeled.shape[0], x_unlabeled.shape[0]))

    # labels binarization (-1 from 1922 to 2002, 1 from 2002 to 2011) -- balanced (labeled) dataset
    y_labeled, y_test = (y_labeled >= 2002).astype(
        np.float32), (y_test >= 2002).astype(np.float32)
    y_unlabeled = (y_unlabeled >= 2002).astype(np.float32)
    y_labeled, y_unlabeled = (2 * y_labeled) - 1, (2 * y_unlabeled) - 1
    y_test = (2 * y_test) - 1

    # removing the mean and scaling to unit variance
    x_scaler = StandardScaler()
    x_scaler.fit(x_train)  # using labeled + unlabeled part
    x_labeled, x_unlabeled = x_scaler.transform(x_labeled), x_scaler.transform(
        x_unlabeled)
    x_test = x_scaler.transform(x_test)
    print("Standardization done")

    # choosing kernel function
    kernel = Kernel(kernel_function=kernel_function, gpu=gpu)

    # training
    print("First training...")
    falkon = Falkon(nystrom_length=round(np.sqrt(x_labeled.shape[0])),
                    gamma=1e-6,
                    kernel_fun=kernel.get_kernel(),
                    kernel_param=6,
                    optimizer_max_iter=max_iterations,
                    gpu=gpu)
    falkon.fit(x_labeled, y_labeled)
    functional_margin = falkon.predict(x_test)

    # initial Accuracy, AUC_ROC
    accuracy = accuracy_score(y_test, np.sign(functional_margin))
    auc_roc = roc_auc_score(y_test, functional_margin)
    print("Accuracy: {:.4f} - AUC: {:.4f}".format(accuracy, auc_roc))

    print("Annealing loop...")
    functional_margin = falkon.predict(x_unlabeled)
    falkon = Falkon(nystrom_length=10000,
                    gamma=1e-6,
                    kernel_fun=kernel.get_kernel(),
                    kernel_param=6,
                    optimizer_max_iter=max_iterations,
                    gpu=gpu)
    balance_constraint = (2 * 0.5) - 1  # 2r - 1
    tic = time()
    for idx, weight in enumerate([0.1, 0.15, 0.25, 1.]):
        print(" -> iteration {}".format(idx + 1))
        lam0 = ((2 / x_unlabeled.shape[0]) *
                np.sum(functional_margin)) - (2 * balance_constraint)
        y_u, lam, _iter = labelling(functional_margin,
                                    balance_constraint, lam0, 1.,
                                    int(x_unlabeled.shape[0] * 0.005))
        print("  -> [debug info] balance constraint {:.2}".format(
            np.divide(np.sum(y_u), x_unlabeled.shape[0])))
        print(
            "  -> [debug info] lambda from {:.3e} to {:.3e} in {} iterations".
            format(lam0, lam, _iter + 1))
        print("  -> [debug info] wrong labels {}".format(
            np.sum(y_u != y_unlabeled)))
        sample_weights = ([1.] * x_labeled.shape[0]) + ([weight] *
                                                        x_unlabeled.shape[0])
        falkon.fit(np.vstack((x_labeled, x_unlabeled)),
                   np.concatenate((y_labeled, y_u)).astype(np.float32),
                   sample_weights=sample_weights)
        functional_margin = falkon.predict(x_unlabeled)
    print("Annealing done in {:.3} seconds".format(time() - tic))

    # testing falkon
    print("Starting falkon testing routine...")
    y_pred = falkon.predict(x_test)
    functional_margin = falkon.predict(x_test)
    accuracy = accuracy_score(y_test, np.sign(functional_margin))
    auc_roc = roc_auc_score(y_test, functional_margin)
    print("Accuracy: {:.3f} - AUC_ROC: {:.3f}".format(accuracy, auc_roc))