예제 #1
0
def test_rrf_cv_gridsearch():
    print(
        "========== Tune parameters for RRF including cross-validation =========="
    )

    np.random.seed(random_seed())

    (x_train, y_train), (x_test, y_test) = demo.load_iris()
    print("Number of training samples = {}".format(x_train.shape[0]))
    print("Number of testing samples = {}".format(x_test.shape[0]))

    x = np.vstack([x_train, x_test, x_test])
    y = np.concatenate([y_train, y_test, y_test])

    params = {'gamma': [0.5, 1.0], 'learning_rate': [0.01, 0.05, 0.1]}

    ps = PredefinedSplit(test_fold=[-1] * x_train.shape[0] +
                         [-1] * x_test.shape[0] + [1] * x_test.shape[0])

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

    clf = RRF(model_name="RRF_hinge",
              D=100,
              lbd=0.01,
              gamma=0.125,
              mode='batch',
              loss='hinge',
              num_epochs=10,
              learning_rate=0.001,
              learning_rate_gamma=0.001,
              metrics=['loss', 'err'],
              callbacks=[early_stopping, checkpoint],
              cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0],
              catch_exception=True,
              random_state=random_seed())

    gs = GridSearchCV(clf, params, cv=ps, n_jobs=-1, refit=False, verbose=True)
    gs.fit(x, y)

    print("Best error {} @ params {}".format(1 - gs.best_score_,
                                             gs.best_params_))

    best_clf = clone(clf).set_params(**gs.best_params_)

    best_clf.fit(np.vstack([x_train, x_test]),
                 np.concatenate([y_train, y_test]))

    train_err = 1.0 - best_clf.score(x_train, y_train)
    test_err = 1.0 - best_clf.score(x_test, y_test)
    print("Training error = %.4f" % train_err)
    print("Testing error = %.4f" % test_err)
    assert abs(test_err - (1.0 - gs.best_score_)) < 1e-4
예제 #2
0
def test_kmm_cv():
    print("========== Test cross-validation for KMM ==========")

    np.random.seed(random_seed())

    (x_train, y_train), (x_test, y_test) = demo.load_iris()
    print("Number of training samples = {}".format(x_train.shape[0]))
    print("Number of testing samples = {}".format(x_test.shape[0]))

    x = np.vstack([x_train, x_test])
    y = np.concatenate([y_train, y_test])

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

    clf = KMM(model_name="KMM_hinge",
              D=20,
              lbd=0.0,
              gamma=0.1,
              mode='batch',
              loss='hinge',
              num_kernels=3,
              batch_size=100,
              temperature=1.0,
              num_epochs=10,
              num_nested_epochs=1,
              learning_rate=0.1,
              learning_rate_mu=0.0,
              learning_rate_gamma=0.1,
              learning_rate_alpha=0.1,
              metrics=['loss', 'err'],
              callbacks=[early_stopping, checkpoint],
              cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0],
              random_state=random_seed(),
              verbose=1)

    clf.fit(x, y)

    train_err = 1.0 - clf.score(x_train, y_train)
    test_err = 1.0 - clf.score(x_test, y_test)
    print("Training error = %.4f" % train_err)
    print("Testing error = %.4f" % test_err)
예제 #3
0
def test_checkpoint():
    np.random.seed(random_seed())

    (x_train, y_train), (x_test, y_test) = demo.load_iris()
    print("Number of training samples = {}".format(x_train.shape[0]))
    print("Number of testing samples = {}".format(x_test.shape[0]))

    x = np.vstack([x_train, x_test])
    y = np.concatenate([y_train, y_test])

    filepath = os.path.join(
        model_dir(), "male/glm/checkpoint_{epoch:04d}_{val_loss:.6f}.pkl")
    checkpoint = ModelCheckpoint(filepath,
                                 mode='min',
                                 monitor='val_loss',
                                 verbose=1,
                                 save_best_only=True)
    optz = SGD(learning_rate=0.01)
    clf = GLM(model_name="checkpoint_callback",
              link='softmax',
              loss='softmax',
              optimizer=optz,
              num_epochs=5,
              batch_size=10,
              task='classification',
              metrics=['loss', 'err'],
              callbacks=[checkpoint],
              cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0],
              random_state=random_seed(),
              verbose=1)

    clf.fit(x, y)
    train_err = 1.0 - clf.score(x_train, y_train)
    test_err = 1.0 - clf.score(x_test, y_test)
    print("Training error = %.4f" % train_err)
    print("Testing error = %.4f" % test_err)

    model_filepath = filepath.format(epoch=5, val_loss=0.968786)
    print("Load model at checkpoint: ", model_filepath, ", and predict:")
    clf1 = Model.load_model(model_filepath)
    train_err = 1.0 - clf1.score(x_train, y_train)
    test_err = 1.0 - clf1.score(x_test, y_test)
    print("Training error = %.4f" % train_err)
    print("Testing error = %.4f" % test_err)
예제 #4
0
def test_dcgan_cifar10_inception_metric(show_figure=False,
                                        block_figure_on_end=False):
    print(
        "========== Test DCGAN with Inception Score and FID on CIFAR10 data =========="
    )

    np.random.seed(random_seed())

    num_data = 128
    (x_train, y_train), (x_test, y_test) = demo.load_cifar10()
    x_train = x_train[:num_data].astype(np.float32).reshape([-1, 32, 32, 3
                                                             ]) / 0.5 - 1.
    x_test = x_test.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1.

    root_dir = os.path.join(model_dir(), "male/DCGAN/cifar10")
    checkpoints_is = ModelCheckpoint(os.path.join(
        root_dir, "checkpoints_is/the_best_is.ckpt"),
                                     mode='max',
                                     monitor='inception_score',
                                     verbose=1,
                                     save_best_only=True)
    checkpoints_fid = ModelCheckpoint(os.path.join(
        root_dir, "checkpoints_fid/the_best_fid.ckpt"),
                                      mode='min',
                                      monitor='FID',
                                      verbose=1,
                                      save_best_only=True)
    loss_display = Display(layout=(1, 1),
                           dpi='auto',
                           show=show_figure,
                           block_on_end=block_figure_on_end,
                           filepath=[
                               os.path.join(root_dir,
                                            "loss/loss_{epoch:04d}.png"),
                               os.path.join(root_dir,
                                            "loss/loss_{epoch:04d}.pdf")
                           ],
                           monitor=[
                               {
                                   'metrics': ['d_loss', 'g_loss'],
                                   'type':
                                   'line',
                                   'labels':
                                   ["discriminator loss", "generator loss"],
                                   'title':
                                   "Losses",
                                   'xlabel':
                                   "epoch",
                                   'ylabel':
                                   "loss",
                               },
                           ])
    inception_score_display = Display(
        layout=(1, 1),
        dpi='auto',
        show=show_figure,
        block_on_end=block_figure_on_end,
        filepath=[
            os.path.join(root_dir, "inception_score/"
                         "inception_score_{epoch:04d}.png"),
            os.path.join(root_dir, "inception_score/"
                         "inception_score_{epoch:04d}.pdf")
        ],
        monitor=[
            {
                'metrics': ['inception_score'],
                'type': 'line',
                'labels': ["Inception Score"],
                'title': "Scores",
                'xlabel': "epoch",
                'ylabel': "score",
            },
        ],
    )
    fid_display = Display(
        layout=(1, 1),
        dpi='auto',
        show=show_figure,
        block_on_end=block_figure_on_end,
        filepath=[
            os.path.join(root_dir, "FID/"
                         "FID_{epoch:04d}.png"),
            os.path.join(root_dir, "FID/"
                         "FID_{epoch:04d}.pdf")
        ],
        monitor=[
            {
                'metrics': ['FID'],
                'type': 'line',
                'labels': ["FID"],
                'title': "Scores",
                'xlabel': "epoch",
                'ylabel': "score",
            },
        ],
    )
    sample_display = Display(layout=(1, 1),
                             dpi='auto',
                             figsize=(10, 10),
                             freq=1,
                             show=show_figure,
                             block_on_end=block_figure_on_end,
                             filepath=os.path.join(
                                 root_dir, "samples/samples_{epoch:04d}.png"),
                             monitor=[
                                 {
                                     'metrics': ['x_samples'],
                                     'title': "Generated data",
                                     'type': 'img',
                                     'num_samples': 100,
                                     'tile_shape': (10, 10),
                                 },
                             ])

    model = DCGAN(
        model_name="DCGAN_CIFAR10",
        num_z=10,  # set to 100 for a full run
        img_size=(32, 32, 3),
        batch_size=16,  # set to 64 for a full run
        num_conv_layers=3,  # set to 3 for a full run
        num_gen_feature_maps=4,  # set to 32 for a full run
        num_dis_feature_maps=4,  # set to 32 for a full run
        metrics=[
            'd_loss', 'g_loss', 'inception_score', 'inception_score_std', 'FID'
        ],
        callbacks=[
            loss_display, inception_score_display, fid_display, sample_display,
            checkpoints_is, checkpoints_fid
        ],
        num_epochs=4,  # set to 100 for a full run
        inception_metrics=[InceptionScore(),
                           FID(data="cifar10")],
        inception_metrics_freq=1,
        # summary_freq=1,  # uncomment this for a full run
        log_path=os.path.join(root_dir, "logs"),
        random_state=random_seed(),
        verbose=1)

    model.fit(x_train)
    filepath = os.path.join(root_dir, "checkpoints_fid/the_best_fid.ckpt")
    print("Reloading the latest model at: {}".format(filepath))
    model1 = TensorFlowModel.load_model(filepath)
    model1.inception_metrics = InceptionMetricList(
        [InceptionScore(), FID(data="cifar10")])
    model1.num_epochs = 6
    model1.fit(x_train)
    print("Done!")
예제 #5
0
def test_wgan_gp_cifar10_fid(show_figure=False, block_figure_on_end=False):
    print("========== Test WGAN-GP with FID on CIFAR10 data ==========")

    np.random.seed(random_seed())

    num_data = 128
    (x_train, y_train), (x_test, y_test) = demo.load_cifar10()
    x_train = x_train[:num_data].astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1.
    x_test = x_test.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1.

    root_dir = os.path.join(model_dir(), "male/WGAN-GP/CIFAR10")
    checkpoints = ModelCheckpoint(
        os.path.join(root_dir, "checkpoints/{epoch:04d}_{FID:.6f}.ckpt"),
        mode='min',
        monitor='FID',
        verbose=1,
        save_best_only=True)
    loss_display = Display(layout=(1, 1),
                           dpi='auto',
                           show=show_figure,
                           block_on_end=block_figure_on_end,
                           filepath=[os.path.join(root_dir, "loss/loss_{epoch:04d}.png"),
                                     os.path.join(root_dir, "loss/loss_{epoch:04d}.pdf")],
                           monitor=[{'metrics': ['d_loss', 'g_loss'],
                                     'type': 'line',
                                     'labels': ["discriminator loss", "generator loss"],
                                     'title': "Losses",
                                     'xlabel': "epoch",
                                     'ylabel': "loss",
                                     },
                                    ])
    fid_display = Display(layout=(1, 1),
                          dpi='auto',
                          show=show_figure,
                          block_on_end=block_figure_on_end,
                          filepath=[os.path.join(root_dir, "FID/"
                                                           "FID_{epoch:04d}.png"),
                                    os.path.join(root_dir, "FID/"
                                                           "FID_{epoch:04d}.pdf")],
                          monitor=[{'metrics': ['FID'],
                                    'type': 'line',
                                    'labels': ["FID"],
                                    'title': "Scores",
                                    'xlabel': "epoch",
                                    'ylabel': "score",
                                    },
                                   ],
                          )
    sample_display = Display(layout=(1, 1),
                             dpi='auto',
                             figsize=(10, 10),
                             freq=1,
                             show=show_figure,
                             block_on_end=block_figure_on_end,
                             filepath=os.path.join(root_dir, "samples/"
                                                             "samples_{epoch:04d}.png"),
                             monitor=[{'metrics': ['x_samples'],
                                       'title': "Generated data",
                                       'type': 'img',
                                       'num_samples': 100,
                                       'tile_shape': (10, 10),
                                       },
                                      ])

    model = WGAN_GP(model_name="WGAN_GP_CIFAR10",
                    num_z=10,  # set to 100 for a full run
                    img_size=(32, 32, 3),
                    batch_size=16,  # set to 64 for a full run
                    num_conv_layers=3,  # set to 3 for a full run
                    num_gen_feature_maps=4,  # set to 32 for a full run
                    num_dis_feature_maps=4,  # set to 32 for a full run
                    metrics=['d_loss', 'g_loss', 'FID', 'FID_100points'],
                    callbacks=[loss_display, fid_display,
                               sample_display, checkpoints],
                    num_epochs=4,  # set to 100 for a full run
                    inception_metrics=[FID(data="cifar10"),
                                       FID(name="FID_100points", data=x_train[:100])],
                    inception_metrics_freq=1,
                    # summary_freq=1,  # uncomment this for a full run
                    log_path=os.path.join(root_dir, "logs"),
                    random_state=random_seed(),
                    verbose=1)

    model.fit(x_train)
예제 #6
0
def test_glm_save_load(show=False, block_figure_on_end=False):
    print("========== Test Save and Load functions for GLM ==========")

    np.random.seed(random_seed())

    (x_train, y_train), (x_test, y_test) = demo.load_iris()
    print("Number of training samples = {}".format(x_train.shape[0]))
    print("Number of testing samples = {}".format(x_test.shape[0]))

    x = np.vstack([x_train, x_test])
    y = np.concatenate([y_train, y_test])

    early_stopping = EarlyStopping(monitor='val_err', patience=5, verbose=1)
    filepath = os.path.join(model_dir(),
                            "male/GLM/iris_{epoch:04d}_{val_err:.6f}.pkl")
    checkpoint = ModelCheckpoint(filepath,
                                 mode='min',
                                 monitor='val_err',
                                 verbose=0,
                                 save_best_only=True)
    loss_display = Display(title="Learning curves",
                           dpi='auto',
                           layout=(3, 1),
                           freq=1,
                           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",
                               },
                           ])

    weight_display = Display(title="Filters",
                             dpi='auto',
                             layout=(1, 1),
                             figsize=(6, 15),
                             freq=1,
                             show=show,
                             block_on_end=block_figure_on_end,
                             monitor=[
                                 {
                                     'metrics': ['weights'],
                                     'title': "Learned weights",
                                     'type': 'img',
                                     'disp_dim': (2, 2),
                                     'tile_shape': (3, 1),
                                 },
                             ])

    clf = GLM(
        model_name="GLM_softmax_cv",
        link='softmax',
        loss='softmax',
        optimizer='sgd',
        num_epochs=4,
        batch_size=10,
        task='classification',
        metrics=['loss', 'err'],
        callbacks=[early_stopping, checkpoint, loss_display, weight_display],
        cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0],
        random_state=random_seed(),
        verbose=1)

    clf.fit(x, y)

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

    save_file_path = os.path.join(model_dir(), "male/GLM/saved_model.pkl")
    clf.save(file_path=save_file_path)
    clf1 = Model.load_model(save_file_path)
    clf1.num_epochs = 10
    clf1.fit(x, y)

    train_err = 1.0 - clf1.score(x_train, y_train)
    test_err = 1.0 - clf1.score(x_test, y_test)
    print("Training error = %.4f" % train_err)
    print("Testing error = %.4f" % test_err)
예제 #7
0
def test_gank_logit_cifar10_inception_score(show_figure=False,
                                            block_figure_on_end=False):
    print(
        "========== Test GANK-Logit with Inception Score on CIFAR10 data =========="
    )

    np.random.seed(random_seed())

    (x_train, y_train), (x_test, y_test) = demo.load_cifar10()
    x_train = x_train.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1.
    x_test = x_test.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1.

    filepath = os.path.join(
        model_dir(), "male/GANK/Logit/cifar10/checkpoints/"
        "{epoch:04d}_{inception_score:.6f}.ckpt")
    checkpoint = ModelCheckpoint(filepath,
                                 mode='max',
                                 monitor='inception_score',
                                 verbose=0,
                                 save_best_only=True)
    loss_display = Display(layout=(1, 1),
                           dpi='auto',
                           show=show_figure,
                           block_on_end=block_figure_on_end,
                           filepath=[
                               os.path.join(
                                   model_dir(), "male/GANK/Logit/cifar10/"
                                   "loss/loss_{epoch:04d}.png"),
                               os.path.join(
                                   model_dir(), "male/GANK/Logit/cifar10/"
                                   "loss/loss_{epoch:04d}.pdf")
                           ],
                           monitor=[
                               {
                                   'metrics': ['d_loss', 'g_loss'],
                                   'type':
                                   'line',
                                   'labels':
                                   ["discriminator loss", "generator loss"],
                                   'title':
                                   "Losses",
                                   'xlabel':
                                   "epoch",
                                   'ylabel':
                                   "loss",
                               },
                           ])
    inception_score_display = Display(
        layout=(1, 1),
        dpi='auto',
        show=show_figure,
        block_on_end=block_figure_on_end,
        filepath=[
            os.path.join(
                model_dir(), "male/GANK/Logit/cifar10/inception_score/"
                "inception_score_{epoch:04d}.png"),
            os.path.join(
                model_dir(), "male/GANK/Logit/cifar10/inception_score/"
                "inception_score_{epoch:04d}.pdf")
        ],
        monitor=[
            {
                'metrics': ['inception_score'],
                'type': 'line',
                'labels': ["Inception Score"],
                'title': "Scores",
                'xlabel': "epoch",
                'ylabel': "score",
            },
        ],
    )
    sample_display = Display(layout=(1, 1),
                             dpi='auto',
                             figsize=(10, 10),
                             freq=1,
                             show=show_figure,
                             block_on_end=block_figure_on_end,
                             filepath=os.path.join(
                                 model_dir(),
                                 "male/GANK/Logit/cifar10/samples/"
                                 "samples_{epoch:04d}.png"),
                             monitor=[
                                 {
                                     'metrics': ['x_samples'],
                                     'title': "Generated data",
                                     'type': 'img',
                                     'num_samples': 100,
                                     'tile_shape': (10, 10),
                                 },
                             ])

    model = GANK(
        model_name="GANK-Logit_CIFAR10",
        num_random_features=50,  # set 1000 for a full run
        gamma_init=0.01,
        loss='logit',
        num_z=10,  # set to 100 for a full run
        img_size=(32, 32, 3),
        batch_size=32,  # set to 64 for a full run
        num_conv_layers=3,  # set to 3 for a full run
        num_gen_feature_maps=4,  # set to 32 for a full run
        num_dis_feature_maps=4,  # set to 32 for a full run
        metrics=['d_loss', 'g_loss', 'inception_score'],
        # callbacks=[loss_display, inception_score_display, sample_display, checkpoint],
        callbacks=[checkpoint],
        num_epochs=4,  # set to 500 for a full run
        inception_score_freq=1,
        random_state=random_seed(),
        verbose=1)

    model.fit(x_train)
예제 #8
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)
예제 #9
0
def test_kmm_cv_disp(show=False, block_figure_on_end=False):
    print("========== Test cross-validation for KMM with Display ==========")

    np.random.seed(random_seed())

    (x_train, y_train), (x_test, y_test) = demo.load_iris()
    print("Number of training samples = {}".format(x_train.shape[0]))
    print("Number of testing samples = {}".format(x_test.shape[0]))

    x = np.vstack([x_train, x_test])
    y = np.concatenate([y_train, y_test])

    early_stopping = EarlyStopping(monitor='val_err', patience=2, verbose=1)
    filepath = os.path.join(model_dir(),
                            "male/KMM/iris_{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=20,
              lbd=0.0,
              gamma=0.1,
              mode='batch',
              loss='hinge',
              num_kernels=3,
              batch_size=100,
              temperature=1.0,
              num_epochs=10,
              num_nested_epochs=1,
              learning_rate=0.1,
              learning_rate_mu=0.0,
              learning_rate_gamma=0.1,
              learning_rate_alpha=0.1,
              metrics=['loss', 'err'],
              callbacks=[display, early_stopping, checkpoint],
              cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0],
              random_state=random_seed(),
              verbose=1)

    clf.fit(x, y)

    train_err = 1.0 - clf.score(x_train, y_train)
    test_err = 1.0 - clf.score(x_test, y_test)
    print("Training error = %.4f" % train_err)
    print("Testing error = %.4f" % test_err)
예제 #10
0
def test_ssrbm_classification(show_figure=False, block_figure_on_end=False):
    print("========== Test Semi-Supervised RBM for Classification ==========")

    num_labeled_data = 1000

    from sklearn.metrics import accuracy_score
    from sklearn.neighbors import KNeighborsClassifier

    np.random.seed(random_seed())

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

    # remove some labels
    idx_train, idx_test = next(
        iter(
            StratifiedShuffleSplit(n_splits=1,
                                   test_size=num_labeled_data,
                                   random_state=random_seed()).split(
                                       x_train, y_train)))
    y_train[idx_train] = 10**8

    x = np.vstack([x_train, x_test])
    y = np.concatenate([y_train, y_test])

    learning_display = Display(
        title="Learning curves",
        dpi='auto',
        layout=(3, 1),
        freq=1,
        show=show_figure,
        block_on_end=block_figure_on_end,
        monitor=[
            {
                'metrics': ['recon_err', 'val_recon_err'],
                'type': 'line',
                'labels': ["training recon error", "validation recon error"],
                'title': "Reconstruction Errors",
                'xlabel': "epoch",
                'ylabel': "error",
            },
            {
                'metrics': ['loss', 'val_loss'],
                'type': 'line',
                'labels': ["training loss", "validation loss"],
                'title': "Learning Losses",
                'xlabel': "epoch",
                'ylabel': "loss",
            },
            {
                'metrics': ['err', 'val_err'],
                'type': 'line',
                'labels': ["training error", "validation error"],
                'title': "Prediction Errors",
                'xlabel': "epoch",
                'ylabel': "error",
            },
            # {'metrics': ['loglik_csl', 'val_loglik_csl'],
            #  'type': 'line',
            #  'labels': ["training loglik (CSL)", "validation loglik (CSL)"],
            #  'title': "Loglikelihoods using CSL",
            #  'xlabel': "epoch",
            #  'ylabel': "loglik",
            #  },
        ])

    filter_display = Display(title="Receptive Fields",
                             dpi='auto',
                             layout=(1, 1),
                             figsize=(8, 8),
                             freq=1,
                             show=show_figure,
                             block_on_end=block_figure_on_end,
                             monitor=[
                                 {
                                     'metrics': ['filters'],
                                     'title': "Receptive Fields",
                                     'type': 'img',
                                     'num_filters': 15,
                                     'disp_dim': (28, 28),
                                     'tile_shape': (3, 5),
                                 },
                             ])

    hidden_display = Display(title="Hidden Activations",
                             dpi='auto',
                             layout=(1, 1),
                             figsize=(8, 8),
                             freq=1,
                             show=show_figure,
                             block_on_end=block_figure_on_end,
                             monitor=[
                                 {
                                     'metrics': ['hidden_activations'],
                                     'title': "Hidden Activations",
                                     'type': 'img',
                                     'data': x_train[:100],
                                 },
                             ])

    early_stopping = EarlyStopping(monitor='val_loss', patience=2, verbose=1)
    filepath = os.path.join(model_dir(),
                            "male/ssRBM/mnist_{epoch:04d}_{val_loss:.6f}.pkl")
    checkpoint = ModelCheckpoint(filepath,
                                 mode='min',
                                 monitor='val_loss',
                                 verbose=0,
                                 save_best_only=True)
    model = SemiSupervisedRBM(num_hidden=15,
                              num_visible=784,
                              batch_size=100,
                              num_epochs=4,
                              learning_rate=0.1,
                              w_init=0.1,
                              momentum_method='sudden',
                              weight_cost=0.0,
                              inference_engine='variational_inference',
                              approx_method='first_order',
                              metrics=['recon_err', 'loss', 'err'],
                              callbacks=[
                                  filter_display, learning_display,
                                  hidden_display, early_stopping, checkpoint
                              ],
                              cv=[-1] * x_train.shape[0] +
                              [0] * x_test.shape[0],
                              random_state=random_seed(),
                              verbose=1)

    model.fit(x, y)

    print("Test reconstruction error = %.4f" %
          model.get_reconstruction_error(x_test).mean())

    print("=========== Predicted by Semi-Supervised RBM ============")
    print("Train accuracy = {0:>1.4f}\tTest accuracy = {1:>1.4f}".format(
        accuracy_score(y_train, model.predict(x_train)),
        accuracy_score(y_test, model.predict(x_test))))

    x_train1 = model.transform(x_train)
    x_test1 = model.transform(x_test)

    clf = KNeighborsClassifier(n_neighbors=4)
    clf.fit(x_train1, y_train)

    print("=========== Predicted by kNN ============")
    print("Train accuracy = {0:>1.4f}\tTest accuracy = {1:>1.4f}".format(
        accuracy_score(y_train, clf.predict(x_train1)),
        accuracy_score(y_test, clf.predict(x_test1))))
예제 #11
0
def test_wgan_gp_resnet_cifar10_inception_metric(show_figure=False,
                                                 block_figure_on_end=False):
    print(
        "========== Test WGAN-GP-ResNet with Inception Score and FID on CIFAR10 data =========="
    )

    np.random.seed(random_seed())

    num_data = 128
    (x_train, y_train), (x_test, y_test) = demo.load_cifar10()
    x_train = x_train[:num_data].astype(np.float32).reshape([-1, 32, 32, 3
                                                             ]) / 0.5 - 1.
    x_test = x_test.astype(np.float32).reshape([-1, 32, 32, 3]) / 0.5 - 1.

    # uncomment for full run
    '''
    import pickle
    from male.configs import data_dir
    tmp = pickle.load(open(os.path.join(data_dir(), "cifar10/cifar10_train.pkl"), "rb"))
    x_train = tmp['data'].astype(np.float32).reshape(
        [-1, 32, 32, 3]) / 127.5 - 1.
    '''

    root_dir = os.path.join(model_dir(), "male/WGAN-GP-ResNet/CIFAR10")
    checkpoints_is = ModelCheckpoint(os.path.join(
        root_dir, "checkpoints_is/the_best_is.ckpt"),
                                     mode='max',
                                     monitor='inception_score',
                                     verbose=1,
                                     save_best_only=True)
    checkpoints_fid = ModelCheckpoint(os.path.join(
        root_dir, "checkpoints_fid/the_best_fid.ckpt"),
                                      mode='min',
                                      monitor='FID',
                                      verbose=1,
                                      save_best_only=True)
    loss_display = Display(layout=(1, 1),
                           dpi='auto',
                           show=show_figure,
                           block_on_end=block_figure_on_end,
                           filepath=[
                               os.path.join(root_dir,
                                            "loss/loss_{epoch:04d}.png"),
                               os.path.join(root_dir,
                                            "loss/loss_{epoch:04d}.pdf")
                           ],
                           monitor=[
                               {
                                   'metrics': ['d_loss', 'g_loss'],
                                   'type':
                                   'line',
                                   'labels':
                                   ["discriminator loss", "generator loss"],
                                   'title':
                                   "Losses",
                                   'xlabel':
                                   "epoch",
                                   'ylabel':
                                   "loss",
                               },
                           ])
    inception_score_display = Display(
        layout=(1, 1),
        dpi='auto',
        show=show_figure,
        block_on_end=block_figure_on_end,
        filepath=[
            os.path.join(root_dir, "inception_score/"
                         "inception_score_{epoch:04d}.png"),
            os.path.join(root_dir, "inception_score/"
                         "inception_score_{epoch:04d}.pdf")
        ],
        monitor=[
            {
                'metrics': ['inception_score'],
                'type': 'line',
                'labels': ["Inception Score"],
                'title': "Scores",
                'xlabel': "epoch",
                'ylabel': "score",
            },
        ],
    )
    fid_display = Display(
        layout=(1, 1),
        dpi='auto',
        show=show_figure,
        block_on_end=block_figure_on_end,
        filepath=[
            os.path.join(root_dir, "FID/"
                         "FID_{epoch:04d}.png"),
            os.path.join(root_dir, "FID/"
                         "FID_{epoch:04d}.pdf")
        ],
        monitor=[
            {
                'metrics': ['FID'],
                'type': 'line',
                'labels': ["FID"],
                'title': "Scores",
                'xlabel': "epoch",
                'ylabel': "score",
            },
        ],
    )
    sample_display = Display(layout=(1, 1),
                             dpi='auto',
                             figsize=(10, 10),
                             freq=1,
                             show=show_figure,
                             block_on_end=block_figure_on_end,
                             filepath=os.path.join(
                                 root_dir, "samples/samples_{epoch:04d}.png"),
                             monitor=[
                                 {
                                     'metrics': ['x_samples'],
                                     'title': "Generated data",
                                     'type': 'img',
                                     'num_samples': 100,
                                     'tile_shape': (10, 10),
                                 },
                             ])

    model = WGAN_GP_ResNet(
        model_name="WGAN_GP_ResNet_CIFAR10",
        num_z=8,  # set to 128 for a full run
        img_size=(32, 32, 3),
        batch_size=64,  # set to 64 for a full run
        g_blocks=('up', 'up', 'up'),
        d_blocks=('down', 'down', None, None),
        num_gen_feature_maps=8,  # set to 128 for a full run
        num_dis_feature_maps=8,  # set to 128 for a full run
        metrics=[
            'd_loss', 'g_loss', 'inception_score', 'inception_score_std', 'FID'
        ],
        callbacks=[
            loss_display, inception_score_display, fid_display, sample_display,
            checkpoints_is, checkpoints_fid
        ],
        num_epochs=2,  # set to 500 for a full run
        inception_metrics=[InceptionScore(),
                           FID(data='cifar10')],
        inception_metrics_freq=1,
        num_inception_samples=100,  # set to 50000 for a full run
        # summary_freq=1,  # uncomment this for a full run
        log_path=os.path.join(root_dir, 'logs'),
        random_state=random_seed(),
        verbose=1)

    model.fit(x_train)
    filepath = os.path.join(root_dir, 'checkpoints_fid/the_best_fid.ckpt')
    print('Reloading the latest model at: {}'.format(filepath))
    model1 = TensorFlowModel.load_model(filepath)
    model1.inception_metrics = InceptionMetricList(
        [InceptionScore(), FID(data='cifar10')])
    model1.num_epochs = 4
    model1.fit(x_train)
    print('Done!')
예제 #12
0
def test_srbm_regression(show_figure=False, block_figure_on_end=False):
    print("========== Test Supervised RBM for Regression ==========")

    from sklearn.metrics import mean_squared_error
    from sklearn.linear_model import LinearRegression

    np.random.seed(random_seed())

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

    x = np.vstack([x_train, x_test])
    y = np.concatenate([y_train, y_test])

    learning_display = Display(
        title="Learning curves",
        dpi='auto',
        layout=(3, 1),
        freq=1,
        show=show_figure,
        block_on_end=block_figure_on_end,
        monitor=[
            {
                'metrics': ['recon_err', 'val_recon_err'],
                'type': 'line',
                'labels': ["training recon error", "validation recon error"],
                'title': "Reconstruction Errors",
                'xlabel': "epoch",
                'ylabel': "error",
            },
            {
                'metrics': ['loss', 'val_loss'],
                'type': 'line',
                'labels': ["training loss", "validation loss"],
                'title': "Learning Losses",
                'xlabel': "epoch",
                'ylabel': "loss",
            },
            {
                'metrics': ['err', 'val_err'],
                'type': 'line',
                'labels': ["training error", "validation error"],
                'title': "Prediction Errors",
                'xlabel': "epoch",
                'ylabel': "error",
            },
            # {'metrics': ['loglik_csl', 'val_loglik_csl'],
            #  'type': 'line',
            #  'labels': ["training loglik (CSL)", "validation loglik (CSL)"],
            #  'title': "Loglikelihoods using CSL",
            #  'xlabel': "epoch",
            #  'ylabel': "loglik",
            #  },
        ])

    filter_display = Display(title="Receptive Fields",
                             dpi='auto',
                             layout=(1, 1),
                             figsize=(8, 8),
                             freq=1,
                             show=show_figure,
                             block_on_end=block_figure_on_end,
                             monitor=[
                                 {
                                     'metrics': ['filters'],
                                     'title': "Receptive Fields",
                                     'type': 'img',
                                     'num_filters': 15,
                                     'disp_dim': (28, 28),
                                     'tile_shape': (3, 5),
                                 },
                             ])

    hidden_display = Display(title="Hidden Activations",
                             dpi='auto',
                             layout=(1, 1),
                             figsize=(8, 8),
                             freq=1,
                             show=show_figure,
                             block_on_end=block_figure_on_end,
                             monitor=[
                                 {
                                     'metrics': ['hidden_activations'],
                                     'title': "Hidden Activations",
                                     'type': 'img',
                                     'data': x_train[:100],
                                 },
                             ])

    early_stopping = EarlyStopping(monitor='val_loss', patience=2, verbose=1)
    filepath = os.path.join(model_dir(),
                            "male/sRBM/mnist_{epoch:04d}_{val_loss:.6f}.pkl")
    checkpoint = ModelCheckpoint(filepath,
                                 mode='min',
                                 monitor='val_loss',
                                 verbose=0,
                                 save_best_only=True)
    model = SupervisedRBM(task='regression',
                          num_hidden=15,
                          num_visible=784,
                          batch_size=100,
                          num_epochs=4,
                          w_init=0.01,
                          learning_rate=0.01,
                          momentum_method='sudden',
                          weight_cost=0.0,
                          inference_engine='variational_inference',
                          approx_method='first_order',
                          metrics=['recon_err', 'loss', 'err'],
                          callbacks=[
                              filter_display, learning_display, hidden_display,
                              early_stopping, checkpoint
                          ],
                          cv=[-1] * x_train.shape[0] + [0] * x_test.shape[0],
                          random_state=random_seed(),
                          verbose=1)

    model.fit(x, y)

    print("Test reconstruction error = %.4f" %
          model.get_reconstruction_error(x_test).mean())
    print("Test loss = %.4f" % model.get_loss(x_test, y_test))

    print("=========== Predicted by sRBM ============")
    print("Train MSE = {0:>1.4f}\tTest MSE = {1:>1.4f}".format(
        -model.score(x_train, y_train), -model.score(x_test, y_test)))

    # fit a Linear Regressor
    lr = LinearRegression()
    lr.fit(x_train, y_train)
    print("=========== Predicted by Linear Regressor ============")
    print("Train MSE = {0:>1.4f}\tTest MSE = {1:>1.4f}".format(
        mean_squared_error(y_train, lr.predict(x_train)),
        mean_squared_error(y_test, lr.predict(x_test))))