Пример #1
0
def get_brain_model_AssociativeDBN(shape,
                                   h_n=250,
                                   h_n2=100,
                                   n_association=100,
                                   dropout=True):
    # initialise AssociativeDBN
    config = associative_dbn.DefaultADBNConfig()

    # Gaussian Input Layer
    bottom_tr = TrainParam(learning_rate=0.0001,
                           momentum_type=NESTEROV,
                           momentum=0.9,
                           weight_decay=0.0001,
                           sparsity_constraint=True,
                           sparsity_decay=0.9,
                           sparsity_cost=0.01,
                           sparsity_target=0.1,
                           dropout=dropout,
                           dropout_rate=0.5,
                           batch_size=10,
                           epochs=5)

    bottom_logger = ProgressLogger(img_shape=(shape, shape))
    bottom_rbm = RBMConfig(v_unit=rbm_units.GaussianVisibleUnit,
                           v_n=shape**2,
                           h_n=h_n,
                           progress_logger=bottom_logger,
                           train_params=bottom_tr)
    h_n_r = 250
    bottom_rbm_r = RBMConfig(v_unit=rbm_units.GaussianVisibleUnit,
                             v_n=shape**2,
                             h_n=h_n_r,
                             progress_logger=bottom_logger,
                             train_params=bottom_tr)

    # Layer 2
    rest_tr = TrainParam(learning_rate=0.0001,
                         momentum_type=CLASSICAL,
                         momentum=0.5,
                         weight_decay=0.0001,
                         sparsity_constraint=True,
                         sparsity_target=0.1,
                         sparsity_cost=0.1,
                         sparsity_decay=0.9,
                         dropout=dropout,
                         dropout_rate=0.5,
                         batch_size=10,
                         epochs=5)

    rest_logger = ProgressLogger()
    rest_rbm = RBMConfig(v_n=h_n,
                         h_n=h_n2,
                         progress_logger=rest_logger,
                         train_params=rest_tr)

    h_n_r2 = 100
    rest_rbm_r = RBMConfig(v_n=h_n,
                           h_n=h_n_r2,
                           progress_logger=rest_logger,
                           train_params=rest_tr)

    # DBN Configs
    # config.left_dbn.rbm_configs = [bottom_rbm]
    # config.right_dbn.rbm_configs = [bottom_rbm_r]
    # config.left_dbn.topology = [shape ** 2, h_n]
    # config.right_dbn.topology = [shape ** 2, h_n_r]
    config.left_dbn.rbm_configs = [bottom_rbm, rest_rbm]
    config.right_dbn.rbm_configs = [bottom_rbm_r, rest_rbm_r]
    config.left_dbn.topology = [shape**2, h_n, h_n2]
    config.right_dbn.topology = [shape**2, h_n_r, h_n_r2]
    config.reuse_dbn = False

    # Association Layer
    top_tr = TrainParam(learning_rate=0.0001,
                        momentum_type=NESTEROV,
                        momentum=0.5,
                        weight_decay=0.0001,
                        sparsity_constraint=True,
                        sparsity_target=0.1,
                        sparsity_decay=0.9,
                        sparsity_cost=0.01,
                        dropout=dropout,
                        dropout_rate=0.5,
                        batch_size=10,
                        epochs=5)

    config.top_rbm.train_params = top_tr
    config.n_association = n_association

    print '... initialised associative DBN'
    return config
Пример #2
0
def associate_data2dataDBN(cache=False):
    print "Testing Joint DBN which tries to learn even-oddness of numbers"
    # project set-up
    data_manager = store.StorageManager('associative_dbn_test', log=True)


    # Load mnist hand digits, class label is already set to binary
    train, valid, test = m_loader.load_digits(n=[500, 100, 100], digits=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                                              pre={'binary_label': True})
    train_x, train_y = train
    test_x, test_y = test
    train_x01 = m_loader.sample_image(train_y)

    dataset01 = m_loader.load_digits(n=[500, 100, 100], digits=[0, 1])

    # Initialise RBM parameters
    # fixed base train param
    base_tr = RBM.TrainParam(learning_rate=0.01,
                             momentum_type=RBM.CLASSICAL,
                             momentum=0.5,
                             weight_decay=0.0005,
                             sparsity_constraint=False,
                             epochs=20)

    # top layer parameters
    tr = RBM.TrainParam(learning_rate=0.1,
                        find_learning_rate=True,
                        momentum_type=RBM.NESTEROV,
                        momentum=0.5,
                        weight_decay=0.001,
                        sparsity_constraint=False,
                        epochs=20)

    tr_top = RBM.TrainParam(learning_rate=0.1,
                            find_learning_rate=True,
                            momentum_type=RBM.CLASSICAL,
                            momentum=0.5,
                            weight_decay=0.001,
                            sparsity_constraint=False,
                            epochs=20)


    # Layer 1
    # Layer 2
    # Layer 3
    topology = [784, 500, 500, 100]

    config = associative_dbn.DefaultADBNConfig()
    config.topology_left = [784, 500, 500, 100]
    config.topology_right = [784, 500, 500, 100]
    config.reuse_dbn = False
    config.top_rbm_params = tr_top
    config.base_rbm_params = [base_tr, tr, tr]

    for cd_type in [RBM.CLASSICAL, RBM.PERSISTENT]:
        for n_ass in [100, 250, 500, 750, 1000]:
            config.n_association = n_ass
            config.top_cd_type = cd_type

            # Construct DBN
            assoc_dbn = associative_dbn.AssociativeDBN(config=config, data_manager=data_manager)

            # Train
            assoc_dbn.train(train_x, train_x01, cache=cache, optimise=True)

            for n_recall in [1, 3, 5, 7, 10]:
                for n_think in [0, 1, 3, 5, 7, 10]:  # 1, 3, 5, 7, 10]:
                    # Reconstruct
                    sampled = assoc_dbn.recall(test_x, n_recall, n_think)

                    # Sample from top layer to generate data
                    sample_n = 100
                    utils.save_images(sampled, image_name='reconstruced_{}_{}_{}.png'.format(n_ass, n_recall, n_think),
                                      shape=(sample_n / 10, 10))

                    dataset01[2] = (theano.shared(sampled), test_y)
Пример #3
0
def associate_data2dataDBN(cache=False):
    print "Testing Associative DBN which tries to learn even-oddness of numbers"
    # project set-up
    data_manager = store.StorageManager('Kanade/associative_dbn_test',
                                        log=True)

    # Load mnist hand digits, class label is already set to binary
    dataset = loader.load_kanade(n=500,
                                 emotions=['anger', 'sadness', 'happy'],
                                 pre={'scale2unit': True})
    train_x, train_y = dataset
    train_x01 = loader.sample_image(train_y)

    dataset01 = loader.load_kanade(n=500)

    # Initialise RBM parameters
    # fixed base train param
    base_tr = RBM.TrainParam(learning_rate=0.001,
                             momentum_type=RBM.CLASSICAL,
                             momentum=0.5,
                             weight_decay=0.0005,
                             sparsity_constraint=False,
                             epochs=20)

    # top layer parameters
    tr = RBM.TrainParam(
        learning_rate=0.001,
        # find_learning_rate=True,
        momentum_type=RBM.NESTEROV,
        momentum=0.5,
        weight_decay=0.001,
        sparsity_constraint=False,
        epochs=20)

    tr_top = RBM.TrainParam(
        learning_rate=0.001,
        # find_learning_rate=True,
        momentum_type=RBM.CLASSICAL,
        momentum=0.5,
        weight_decay=0.001,
        sparsity_constraint=False,
        epochs=20)

    # Layer 1
    # Layer 2
    # Layer 3
    # topology = [784, 500, 500, 100]

    config = associative_dbn.DefaultADBNConfig()
    config.topology_left = [625, 500, 500, 100]
    config.topology_right = [625, 500, 500, 100]
    config.reuse_dbn = False
    config.top_rbm_params = tr_top
    config.base_rbm_params = [base_tr, tr, tr]

    count = 0
    for cd_type in [RBM.CLASSICAL, RBM.PERSISTENT]:
        for n_ass in [100, 250, 500, 750, 1000]:
            config.n_association = n_ass
            config.top_cd_type = cd_type

            # Construct DBN
            ass_dbn = associative_dbn.AssociativeDBN(config=config,
                                                     data_manager=data_manager)

            # Train
            for trainN in xrange(0, 5):
                ass_dbn.train(train_x, train_x01, cache=cache)

                for n_recall in [1, 3, 10]:
                    for n_think in [0, 1, 3, 5, 10]:  # 1, 3, 5, 7, 10]:
                        # Reconstruct
                        sampled = ass_dbn.recall(train_x, n_recall, n_think)

                        # Sample from top layer to generate data
                        sample_n = 100
                        utils.save_images(
                            sampled,
                            image_name='{}_reconstruced_{}_{}_{}.png'.format(
                                count, n_ass, n_recall, n_think),
                            shape=(sample_n / 10, 10),
                            img_shape=(25, 25))
                        count += 1
Пример #4
0
def get_brain_model_AssociativeDBNConfig(shape):
    # initialise AssociativeDBN
    config = associative_dbn.DefaultADBNConfig()

    # Gaussian Input Layer
    bottom_tr = TrainParam(learning_rate=0.001,
                           momentum_type=NESTEROV,
                           momentum=0.9,
                           weight_decay=0.0001,
                           sparsity_constraint=True,
                           sparsity_target=0.1,
                           sparsity_decay=0.9,
                           sparsity_cost=0.1,
                           dropout=True,
                           dropout_rate=0.5,
                           epochs=10)

    bottom_tr_r = TrainParam(learning_rate=0.001,
                             momentum_type=NESTEROV,
                             momentum=0.5,
                             weight_decay=0.0001,
                             sparsity_constraint=True,
                             sparsity_target=0.1,
                             sparsity_decay=0.9,
                             sparsity_cost=0.1,
                             dropout=True,
                             dropout_rate=0.5,
                             epochs=10)

    rest_tr = TrainParam(learning_rate=0.0001,
                         momentum_type=NESTEROV,
                         momentum=0.5,
                         weight_decay=0.0001,
                         dropout=True,
                         dropout_rate=0.8,
                         epochs=10,
                         batch_size=20)

    h_n = 300
    h_n_r = 50
    bottom_logger = ProgressLogger(img_shape=(shape, shape))
    bottom_rbm = RBMConfig(v_n=shape ** 2,
                           h_n=h_n,
                           progress_logger=bottom_logger,
                           train_params=bottom_tr)

    bottom_rbm_r = RBMConfig(v_n=shape ** 2,
                             h_n=h_n_r,
                             progress_logger=bottom_logger,
                             train_params=bottom_tr_r)

    rest_logger = ProgressLogger()
    rest_rbm = RBMConfig(v_n=250,
                         h_n=250,
                         progress_logger=rest_logger,
                         train_params=rest_tr)

    config.left_dbn.rbm_configs = [bottom_rbm]  # , rest_rbm]
    config.right_dbn.rbm_configs = [bottom_rbm_r]  # , rest_rbm]
    config.left_dbn.topology = [shape ** 2, h_n]  # , 250]
    config.right_dbn.topology = [shape ** 2, h_n_r]  # , 250]

    top_tr = TrainParam(learning_rate=0.00001,
                        momentum_type=NESTEROV,
                        momentum=0.9,
                        weight_decay=0.0001,
                        sparsity_constraint=False,
                        sparsity_target=0.1,
                        sparsity_decay=0.9,
                        sparsity_cost=0.1,
                        dropout=True,
                        dropout_rate=0.5,
                        batch_size=10,
                        epochs=10
                        )

    config.top_rbm.train_params = top_tr
    config.n_association = 300
    config.reuse_dbn = False
    return config
Пример #5
0
def KanadeAssociativeDBN(cache=False):
    print "Testing Associative RBM which tries to learn the following mapping: " \
          "ID"
    # "{anger, saddness, disgust} -> {sadness}, {contempt, happy, surprise} -> {happy}"
    # project set-up
    data_manager = store.StorageManager('Kanade/AssociativeDBNTest', log=True)
    shape = 25
    dataset_name = 'sharp_equi{}_{}'.format(shape, shape)
    preprocessing = {'scale': True}

    # Load kanade database
    mapping = None
    # mapping = {'anger': 'sadness',
    #            'contempt': 'happy',
    #            'disgust': 'sadness',
    #            'fear': 'sadness',
    #            'happy': 'happy',
    #            'sadness': 'sadness',
    #            'surprise': 'happy'}

    dataset = loader.load_kanade(n=100,
                                 pre=preprocessing,
                                 set_name=dataset_name)

    mapped_dataset = loader.load_kanade(
        n=100,
        # emotions=['sadness', 'happy'],
        pre=preprocessing,
        set_name=dataset_name)  # Target Image
    train, valid, test = dataset
    train_x, train_y = train
    test_x, test_y = test

    # Sample associated image
    train_x_ass, train_y_ass = loader.sample_image(train_y,
                                                   mapping=mapping,
                                                   pre=preprocessing,
                                                   set_name=dataset_name)
    test_x_ass, test_y_ass = loader.sample_image(test_y,
                                                 mapping=mapping,
                                                 pre=preprocessing,
                                                 set_name=dataset_name)

    # initialise AssociativeDBN
    config = associative_dbn.DefaultADBNConfig()

    # Gaussian Input Layer
    bottom_tr = rbm_config.TrainParam(learning_rate=0.0001,
                                      momentum_type=rbm_config.NESTEROV,
                                      momentum=0.9,
                                      weight_decay=0.0001,
                                      epochs=20,
                                      batch_size=10)
    h_n = 150
    bottom_logger = rbm_logger.ProgressLogger(img_shape=(shape, shape))
    bottom_rbm = rbm_config.RBMConfig(v_unit=rbm_units.GaussianVisibleUnit,
                                      v_n=shape**2,
                                      h_n=h_n,
                                      progress_logger=bottom_logger,
                                      train_params=bottom_tr)

    config.left_dbn.rbm_configs[0] = bottom_rbm
    config.right_dbn.rbm_configs[0] = bottom_rbm
    config.left_dbn.topology = [shape**2, h_n]
    config.right_dbn.topology = [shape**2, h_n]
    config.top_rbm.train_params.epochs = 20
    config.top_rbm.train_params.batch_size = 10
    config.n_association = 1000
    config.reuse_dbn = True
    adbn = associative_dbn.AssociativeDBN(config=config,
                                          data_manager=data_manager)

    # Plot sample
    loader.save_faces(
        train_x.get_value(borrow=True)[1:50],
        tile=(10, 10),
        img_name='n_orig.png',
    )
    loader.save_faces(train_x_ass.get_value(borrow=True)[1:50],
                      tile=(10, 10),
                      img_name='n_ass.png')

    # Train classifier to be used for classifying reconstruction associated image layer
    clf_orig = SimpleClassifier('knn', mapped_dataset[0][0],
                                mapped_dataset[0][1])

    # Test DBN Performance
    for i in xrange(0, 5):
        # Train DBN - learn joint distribution
        cache_left = [True]
        cache_right = [True]
        cache_top = False
        cache = [cache_left, cache_right, cache_top]
        adbn.train(train_x, train_x_ass, cache=cache)
        print "... trained associative DBN"

        # Reconstruct images
        test_x_recon = adbn.recall(test_x, associate_steps=500, recall_steps=0)
        print "... reconstructed images"

        # Classify the reconstructions

        # 1. Train classifier on original images
        score_orig = clf_orig.get_score(test_x_recon, test_y_ass.eval())

        # 2. Train classifier on reconstructed images - reconstruction obtained by right dbn
        right_dbn = adbn.dbn_right
        mapped_train_recon = right_dbn.reconstruct(
            mapped_dataset[0][0],
            k=1,
            plot_n=100,
            plot_every=1,
            img_name='right_dbn_reconstruction')
        clf_recon = SimpleClassifier('knn', mapped_train_recon,
                                     mapped_dataset[0][1].eval())
        score_retrain = clf_recon.get_score(test_x_recon, test_y_ass.eval())

        out_msg = '{} (orig, retrain):{},{}'.format(adbn, score_orig,
                                                    score_retrain)
        print out_msg