Пример #1
0
def test_sgd_visualization_2d(show=False, block_figure_on_end=False):
    (x_train, y_train), (_, _) = demo.load_synthetic_2d()

    display = Display(freq=10,
                      dpi='auto',
                      show=show,
                      block_on_end=block_figure_on_end,
                      monitor=[{'metrics': ['predict'],
                                'title': "Learning losses",
                                'xlabel': "X1",
                                'ylabel': "X2",
                                'grid_size': 10,
                                'marker_size': 10,
                                'left': None,
                                'right': None,
                                'top': None,
                                'bottom': None
                                }]
                      )

    learner = KSGD(lbd=0.0001,
                   eps=0.001,
                   gamma=30,
                   kernel='gaussian',
                   loss='hinge',
                   batch_size=1,
                   callbacks=[display],
                   avg_weight=False,
                   random_state=random_seed())

    learner.fit(x_train, y_train)
    print("Training error = %.4f" % (1 - learner.score(x_train, y_train)))
Пример #2
0
def test_bank_2d(show=False, block_figure_on_end=False):
    print("========== Test BaNK on 2D data ==========")

    (x_train, y_train), (_, _) = demo.load_synthetic_2d()

    # Being careful at this point
    y_train[y_train == -1] = 0

    learner = BaNK(gamma=20,
                   rf_dim=400,
                   inner_regularization=0.125,
                   alpha=1.0,
                   kappa=1.0,
                   inner_max_loop=1,
                   max_outer_loop=2,
                   batch_size=5)

    learner.fit(x_train, y_train)

    y_train_pred = learner.predict(x_train)
    print(np.unique(y_train))
    print(np.unique(y_train_pred))
    print("Training error = %.4f" %
          (1 - metrics.accuracy_score(y_train, y_train_pred)))
    visualize_classification_prediction(learner,
                                        x_train,
                                        y_train,
                                        grid_size=50,
                                        show=show,
                                        epoch=0,
                                        marker_size=20,
                                        block_on_end=block_figure_on_end)
Пример #3
0
def test_svrg_s_visualization_2d(show=False, block_figure_on_end=False):
    (x_train, y_train), (_, _) = demo.load_synthetic_2d()

    print('num_samples: {}'.format(x_train.shape[0]))

    predict_display = Display(
        freq=10,
        dpi='auto',
        show=show,
        block_on_end=block_figure_on_end,
        monitor=[{'metrics': ['predict'],
                  'title': "Visualization",
                  'xlabel': "X1",
                  'ylabel': "X2",
                  'grid_size': 100,
                  'marker_size': 10,
                  'left': None,
                  'right': None,
                  'top': None,
                  'bottom': None
                  }]
    )

    loss_display = Display(
        freq=1,
        dpi='auto',
        show=show,
        block_on_end=block_figure_on_end,
        monitor=[{'metrics': ['train_loss', 'obj_func'],
                  'type': 'line',
                  'title': "Learning losses",
                  'xlabel': "data points",
                  'ylabel': "loss",
                  }]
    )

    learner = SVRG(
        regular_param=0.01,
        learning_rate_scale=1.0,
        gamma=10,
        rf_dim=400,
        num_epochs=2,
        cache_size=6,
        freq_update_full_model=10,
        oracle=SVRG.COVERAGE,
        core_max=10,
        coverage_radius=100.0,
        loss_func=SVRG.LOGISTIC,
        smooth_hinge_theta=0.5,
        smooth_hinge_tau=0.5,
        callbacks=[loss_display, predict_display],
        metrics=['train_loss', 'obj_func'],
        freq_calc_metrics=50,
        random_state=random_seed())

    learner.fit(x_train, y_train)
    y_train_pred = learner.predict(x_train)
    print("Training error = %.4f" % (1 - metrics.accuracy_score(y_train, y_train_pred)))
def test_sgd_visualization_2d(block_figure_on_end=False):
    (x_train, y_train), (_, _) = demo.load_synthetic_2d()

    predict_display = Display(
        freq=1,
        dpi='auto',
        block_on_end=block_figure_on_end,
        monitor=[{'metrics': ['predict'],
                  'title': "Visualization",
                  'xlabel': "X1",
                  'ylabel': "X2",
                  'grid_size': 100,
                  'marker_size': 10,
                  'left': None,
                  'right': None,
                  'top': None,
                  'bottom': None
                  }]
    )

    display = Display(
        freq=1,
        dpi='auto',
        block_on_end=block_figure_on_end,
        monitor=[{'metrics': ['mistake_rate'],
                  'type': 'line',
                  'title': "Mistake Rate",
                  'xlabel': "data points",
                  'ylabel': "Error",
                  }]
    )

    learner = OnlineDualSVRG(
        regular_param=0.01,
        learning_rate_scale=1.0,
        gamma=10,
        rf_dim=400,
        cache_size=6,
        freq_update_full_model=10,
        oracle='budget',
        core_max=10,
        coverage_radius=0.5,
        loss_func='hinge',
        smooth_hinge_theta=0.5,
        smooth_hinge_tau=0.5,
        callbacks=[display],
        metrics=['mistake_rate'],
        random_state=random_seed())

    learner.fit(x_train, y_train)
    y_train_pred = learner.predict(x_train)
    print("Training error = %.4f" % (1 - metrics.accuracy_score(y_train, y_train_pred)))
Пример #5
0
def test_kmm_syn2d(show=False, block_figure_on_end=False):
    print("========== Test KMM on 2D data ==========")

    np.random.seed(random_seed())

    (x_train, y_train), (x_test, y_test) = demo.load_synthetic_2d()

    idx_train, idx_test = next(
        iter(
            StratifiedShuffleSplit(n_splits=1,
                                   test_size=40,
                                   random_state=random_seed()).split(
                                       x_train, y_train)))
    x0 = x_train[idx_train]
    y0 = y_train[idx_train]
    x1 = x_train[idx_test]
    y1 = y_train[idx_test]

    x = np.vstack([x0, x1])
    y = np.concatenate([y0, y1])

    early_stopping = EarlyStopping(monitor='val_loss', patience=2, verbose=1)
    filepath = os.path.join(
        model_dir(), "male/KMM/syn2d_data_{epoch:04d}_{val_err:.6f}.pkl")
    checkpoint = ModelCheckpoint(filepath,
                                 mode='min',
                                 monitor='val_err',
                                 verbose=0,
                                 save_best_only=True)

    display = Display(layout=(3, 1),
                      dpi='auto',
                      show=show,
                      block_on_end=block_figure_on_end,
                      monitor=[
                          {
                              'metrics': ['loss', 'val_loss'],
                              'type': 'line',
                              'labels': ["training loss", "validation loss"],
                              'title': "Learning losses",
                              'xlabel': "epoch",
                              'ylabel': "loss",
                          },
                          {
                              'metrics': ['err', 'val_err'],
                              'type': 'line',
                              'title': "Learning errors",
                              'xlabel': "epoch",
                              'ylabel': "error",
                          },
                          {
                              'metrics': ['err'],
                              'type': 'line',
                              'labels': ["training error"],
                              'title': "Learning errors",
                              'xlabel': "epoch",
                              'ylabel': "error",
                          },
                      ])

    clf = KMM(model_name="KMM_hinge",
              D=10,
              lbd=0.0,
              gamma=0.5,
              mode='batch',
              loss='hinge',
              num_kernels=4,
              batch_size=4,
              temperature=0.1,
              num_epochs=10,
              num_nested_epochs=0,
              learning_rate=0.001,
              learning_rate_mu=0.0,
              learning_rate_gamma=0.001,
              learning_rate_alpha=0.001,
              metrics=['loss', 'err'],
              callbacks=[display, early_stopping, checkpoint],
              cv=[-1] * x0.shape[0] + [0] * x1.shape[0],
              random_state=random_seed())

    clf.fit(x, y)

    train_err = 1.0 - clf.score(x_train, y_train)
    print("Training error = %.4f" % train_err)

    if block_figure_on_end:
        # save predictions
        y_test_pred = clf.predict(x_test)
        x_test[x_test == 0] = 1e-4
        dump_svmlight_file(x_test,
                           y_test_pred,
                           os.path.join(
                               data_dir(),
                               "demo/synthetic_2D_data_test_predict.libsvm"),
                           zero_based=False)