コード例 #1
0
def associate_data2dataADBN_Finetune(cache=False, train_further=False):
    print "Testing Associative RBM which tries to learn even-oddness of numbers"
    f = open('adbn_errors.txt', 'w')
    # project set-up
    proj_name = 'ADBN_digits'
    data_manager = store.StorageManager(proj_name, log=True)
    shape = 28
    train_n = 10000
    test_n = 1000
    # Load mnist hand digits, class label is already set to binary
    dataset = m_loader.load_digits(n=[train_n, 0, test_n],
                                   digits=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                                   pre={'binary_label': True})

    tr_x, tr_y = dataset[0]
    te_x, te_y = dataset[2]
    tr_x01 = m_loader.sample_image(tr_y)
    te_x01 = m_loader.sample_image(te_y)
    clf = SimpleClassifier('logistic', te_x01.get_value(), te_y.eval())

    configs = get_adbns()

    for a in xrange(10):
        for n, config in enumerate(configs):
            t = 'l{}_r{}_t{}'.format(config.left_dbn.topology, config.right_dbn.topology, config.n_association)
            f.write('{}:{}:'.format(a, t))
            brain_c = associative_dbn.AssociativeDBN(config=config,
                                                     data_manager=store.StorageManager('{}/{}'.format(proj_name, n),
                                                                                       log=False))
            brain_c.train(tr_x, tr_x01, cache=True, train_further=True)

            recon = brain_c.recall(tr_x,
                                   associate_steps=10,
                                   recall_steps=0,
                                   img_name='{}_{}'.format(a, t))

            error = clf.get_score(recon, tr_y.eval())
            print error
            f.write('{}, '.format(error))

            for i in xrange(0, 10):
                brain_c.fine_tune(tr_x, tr_x01, epochs=1)
                for y_type in ['active_h', 'v_noisy_active_h', 'zero']:
                    recon = brain_c.recall(tr_x,
                                           associate_steps=10,
                                           recall_steps=0,
                                           img_name='{}_{}_{}_{}'.format(a, t, y_type, i),
                                           y_type=y_type)
                    error = clf.get_score(recon, tr_y.eval())
                    print error
                    f.write('{}, '.format(error))
            f.write('\n')
    f.close()
コード例 #2
0
def associate_data2dataADBN(cache=False, train_further=True):
    print "Testing Associative RBM which tries to learn even-oddness of numbers"
    f = open('adbnlog.txt', 'a')
    # project set-up
    data_manager = store.StorageManager('AssociativeDBN_digits', log=False)
    shape = 28
    train_n = 10000
    test_n = 1000
    # Load mnist hand digits, class label is already set to binary
    dataset = m_loader.load_digits(n=[train_n, 100, test_n],
                                   digits=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                                   pre={'binary_label': True})

    tr_x, tr_y = dataset[0]
    te_x, te_y = dataset[2]
    tr_x01 = m_loader.sample_image(tr_y)

    clf = SimpleClassifier('logistic', te_x.get_value(), te_y.eval())

    for dropout in [True, False]:
        for sc in [True, False]:
            for lr in [0.001, 0.0001, 0.00005, 0.00001]:
                for n in [100, 250, 500]:
                    config = get_brain_model_AssociativeDBNConfig(28, data_manager=data_manager)
                    config.top_rbm.train_params.learning_rate = lr
                    config.top_rbm.train_params.sparsity_constraint = sc
                    config.top_rbm.train_params.dropout = dropout
                    config.n_association = n
                    adbn = associative_dbn.AssociativeDBN(config=config, data_manager=data_manager)
                    brain_c = adbn
                    f.write(str(brain_c.association_layer) + "\n")

                    errors = []
                    for i in xrange(0, 5):
                        f.write("Epoch %d \n" % (i * 10))

                        brain_c.train(tr_x, tr_x01,
                                      cache=[[True, True, True], [True, True, True], False],
                                      train_further=[[False, True, True], [False, True, True], True])

                        if i == 0:
                            # Reconstruction
                            recon_right = brain_c.dbn_left.reconstruct(tr_x, k=10, plot_every=1, plot_n=100,
                                                                       img_name='adbn_left_recon_{}'.format(shape))
                            recon_left = brain_c.dbn_right.reconstruct(tr_x01, k=10, plot_every=1, plot_n=100,
                                                                       img_name='adbn_right_recon_{}'.format(shape))

                        for j in [5, 10]:
                            recon_x = brain_c.recall(te_x, associate_steps=j, recall_steps=0,
                                                     img_name='adbn_child_recon_{}1'.format(shape), y_type='active_h')
                            error = clf.get_score(recon_x, te_y.eval())
                            f.write("active_h %f\n" % error)

                            recon_x = brain_c.recall(te_x, associate_steps=j, recall_steps=0,
                                                     img_name='adbn_child_recon_{}2'.format(shape),
                                                     y_type='v_noisy_active_h')
                            error = clf.get_score(recon_x, te_y.eval())
                            f.write("v_noisy_active_h %f\n" % error)

    f.close()
コード例 #3
0
def train_kanade():
    print "Testing RBM"

    data_manager = store.StorageManager('Kanade/SimpleRBMTest')

    # Load mnist hand digits
    datasets = loader.load_kanade(n=500,
                                  set_name='25_25',
                                  emotions=['happy', 'sadness'],
                                  pre={'scale2unit': True})
    train_x, train_y = datasets[0]

    sparsity_constraint = True
    # Initialise the RBM and training parameters
    tr = rbm_config.TrainParam(learning_rate=0.0001,
                               momentum_type=rbm_config.NESTEROV,
                               momentum=0.9,
                               weight_decay=0.001,
                               sparsity_constraint=sparsity_constraint,
                               sparsity_target=0.01,
                               sparsity_cost=1,
                               sparsity_decay=0.9,
                               dropout=True,
                               epochs=100)

    n_visible = train_x.get_value().shape[1]
    n_hidden = 500

    config = rbm_config.RBMConfig(
        v_n=n_visible,
        v2_n=n_visible,
        h_n=n_hidden,
        v_unit=rbm_units.GaussianVisibleUnit,
        associative=False,
        cd_type=rbm_config.CLASSICAL,
        cd_steps=1,
        train_params=tr,
        progress_logger=rbm_logger.ProgressLogger(img_shape=(25, 25)))

    rbm = RBM(config)

    print "... initialised RBM"

    # Train RBM
    rbm.train(train_x)

    # Test RBM
    rbm.reconstruct(train_x, k=5, plot_n=10, plot_every=1)

    # Store Parameters
    data_manager.persist(rbm)
コード例 #4
0
def test_rbm():
    print "Testing RBM"

    data_manager = store.StorageManager('TestRBM')
    # Load Cohn Kanade dataset
    datasets = loader.load_kanade(pre={'scale': True}, n=100, set_name='sharp_equi25_25')
    train_set_x, train_set_y = datasets[0]
    test_set_x, test_set_y = datasets[2]

    # Initilise the RBM and training parameters
    tr = TrainParam(learning_rate=0.0001,
                    momentum_type=NESTEROV,
                    momentum=0.5,
                    weight_decay=0.0001,
                    sparsity_constraint=True,
                    sparsity_target=0.01,
                    sparsity_cost=0.1,
                    sparsity_decay=0.9,
                    dropout=True,
                    dropout_rate=0.5,
                    batch_size=10,
                    epochs=10)

    n_visible = train_set_x.get_value(borrow=True).shape[1]
    n_hidden = 10

    config = RBMConfig()
    config.v_n = n_visible
    config.h_n = n_hidden
    config.v_unit = rbm_units.GaussianVisibleUnit
    # config.h_unit = rbm_units.ReLUnit
    config.progress_logger = ProgressLogger(img_shape=(25, 25))
    config.train_params = tr
    rbm = RBM(config)
    print "... initialised RBM"

    load = store.retrieve_object(str(rbm))
    if load:
        rbm = load

    for i in xrange(0, 1):
        # Train RBM
        rbm.train(train_set_x)
        data_manager.persist(rbm)

        # Test RBM Reconstruction via Linear Classifier
        clf = SimpleClassifier(classifier='logistic', train_x=train_set_x, train_y=train_set_y)
        recon_te = rbm.reconstruct(test_set_x, k=1, plot_n=100, plot_every=1,img_name='recon_te_{}.png'.format(i))

        print 'Original Score: {}'.format(clf.get_score(test_set_x, test_set_y))
        print 'Recon Score:    {}'.format(clf.get_score(recon_te, test_set_y.eval()))
コード例 #5
0
def test_generative_dbn():
    manager = store.StorageManager('fine_tune')
    shape = 28
    train_x = get_data(shape)

    # Initialise RBM parameters
    dbn = get_dbn_model(manager, shape)

    print "... initialised dbn"

    print '... pre-training the model'
    start_time = time.clock()

    dbn.pretrain(train_x, cache=[True, True], train_further=[True, True])
    # dbn.pretrain(train_x, cache=False)

    end_time = time.clock()
    print >> sys.stderr, ('The pretraining code for file ' +
                          os.path.split(__file__)[1] + ' ran for %.2fm' %
                          ((end_time - start_time) / 60.))

    # Sample from top layer to generate data
    sample_n = 1000
    sampled = dbn.sample(sample_n, k=100, rand_type='noisy_mean')
    k_loader.save_faces(sampled,
                        tile=(sample_n / 10, 10),
                        img_name="sampled.png",
                        img_shape=(shape, shape))
    dbn.reconstruct(train_x,
                    k=1,
                    plot_every=1,
                    plot_n=100,
                    img_name='dbn_recon')

    for i in xrange(0, 1):
        dbn.fine_tune(train_x, epochs=1)
        sampled = dbn.sample(sample_n, k=100, rand_type='noisy_mean')
        k_loader.save_faces(sampled,
                            tile=(sample_n / 10, 10),
                            img_name=("sampled_fine_tuned%d.png" % i),
                            img_shape=(shape, shape))
        dbn.reconstruct(train_x,
                        k=1,
                        plot_every=1,
                        plot_n=100,
                        img_name=('dbn_recon_fine_tune%d' % i))
コード例 #6
0
def associate_data2data(cache=False, train_further=True):
    print "Testing Associative RBM which tries to learn even-oddness of numbers"
    # project set-up
    data_manager = store.StorageManager('EvenOddP', log=True)
    train_n = 10000
    test_n = 1000
    # Load mnist hand digits, class label is already set to binary
    dataset = m_loader.load_digits(n=[train_n, 100, test_n],
                                   digits=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                                   pre={'binary_label': True})

    tr_x, tr_y = dataset[0]
    te_x, te_y = dataset[2]
    tr_x01 = m_loader.sample_image(tr_y)
    te_x01 = m_loader.sample_image(te_y)
    ones = m_loader.load_digits(n=[test_n, 0, 0], digits=[1])[0][0]
    zeroes = m_loader.load_digits(n=[test_n, 0, 0], digits=[0])[0][0]

    concat1 = theano.function([], T.concatenate([tr_x, tr_x01], axis=1))()
    # concat2 = theano.function([], T.concatenate([tr_x01, tr_x], axis=1))()
    # c = np.concatenate([concat1, concat2], axis=0)
    # np.random.shuffle(c)
    # tr_concat_x = theano.shared(c, name='tr_concat_x')
    tr_concat_x = theano.shared(concat1, name='tr_concat_x')

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

    # Even odd test
    k = 1
    n_visible = 784 * 2
    n_visible2 = 0
    n_hidden = 300
    print "number of hidden nodes: %d" % n_hidden

    config = RBMConfig(v_n=n_visible,
                       v2_n=n_visible2,
                       h_n=n_hidden,
                       cd_type=CLASSICAL,
                       cd_steps=k,
                       train_params=tr,
                       progress_logger=ProgressLogger(img_shape=(28 * 2, 28)))

    rbm = RBM(config=config)

    # Load RBM (test)
    loaded = store.retrieve_object(str(rbm))
    if loaded and cache:
        rbm = loaded
        print "... loaded precomputed rbm"

    errors = []
    for i in xrange(0, 10):
        # Train RBM
        if not loaded or train_further:
            rbm.train(tr_concat_x)

        # Save RBM
        data_manager.persist(rbm)

        # Reconstruct using RBM
        recon_x = rbm.reconstruct_association_opt(te_x, k=10, bit_p=0)
        clf = SimpleClassifier('logistic', te_x.get_value(), te_y.eval())
        orig = te_y.eval()
        error = clf.get_score(recon_x, orig)
        print error
        errors.append(error)

    print errors
コード例 #7
0
def associate_data2dataJDBN(cache=False, train_further=False):
    print "Testing Associative RBM which tries to learn even-oddness of numbers"
    f = open('errors.txt', 'w')
    # project set-up
    proj_name = 'JDBN_digits'
    data_manager = store.StorageManager(proj_name, log=False)
    shape = 28
    train_n = 10000
    test_n = 1000
    # Load mnist hand digits, class label is already set to binary
    dataset = m_loader.load_digits(n=[train_n, 0, test_n],
                                   digits=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                                   pre={'binary_label': True})

    tr_x, tr_y = dataset[0]
    te_x, te_y = dataset[2]
    tr_x01 = m_loader.sample_image(tr_y)
    te_x01 = m_loader.sample_image(te_y)
    ones = m_loader.load_digits(n=[test_n, 0, 0], digits=[1])[0][0]
    zeroes = m_loader.load_digits(n=[test_n, 0, 0], digits=[0])[0][0]
    tr_X = theano.shared(np.concatenate([tr_x.get_value(), tr_x01.get_value()], axis=1))
    initial_y = np.random.binomial(n=1, p=0.0, size=te_x.get_value(True).shape).astype(t_float_x)
    initial_y_uni = np.random.uniform(low=0, high=1, size=te_x.get_value(True).shape).astype(t_float_x)
    initial_y_bi001 = np.random.binomial(n=1, p=0.01, size=te_x.get_value(True).shape).astype(t_float_x)
    initial_y_bi01 = np.random.binomial(n=1, p=0.1, size=te_x.get_value(True).shape).astype(t_float_x)
    te_X = theano.shared(np.concatenate([te_x.get_value(), initial_y], axis=1))
    te_X2 = theano.shared(np.concatenate([te_x.get_value(), initial_y_uni], axis=1))
    te_X3 = theano.shared(np.concatenate([te_x.get_value(), initial_y_bi01], axis=1))
    te_X4 = theano.shared(np.concatenate([te_x.get_value(), initial_y_bi001], axis=1))
    clf = SimpleClassifier('logistic', te_x01.get_value(), te_y.eval())

    configs = []

    for h_n in [100, 250, 500]:
        config1 = get_brain_model_JointDBNConfig(shape, data_manager)
        config1.topology = [784 * 2, h_n, h_n]
        config1.rbm_configs[0].h_n = h_n
        config1.rbm_configs[1].v_n = h_n
        config1.rbm_configs[1].h_n = h_n
        configs.append(config1)

    for h_n in [100, 250, 500]:
        config1 = get_brain_model_JointDBNConfig(shape, data_manager)
        config1.topology = [784 * 2, h_n, h_n * 2]
        config1.rbm_configs[0].h_n = h_n
        config1.rbm_configs[1].v_n = h_n
        config1.rbm_configs[1].h_n = h_n * 2
        configs.append(config1)

    for h_n in [100, 250, 500]:
        config1 = get_brain_model_JointDBNConfig(shape, data_manager)
        config1.topology = [784 * 2, h_n * 2, h_n]
        config1.rbm_configs[0].h_n = h_n * 2
        config1.rbm_configs[1].v_n = h_n * 2
        config1.rbm_configs[1].h_n = h_n
        configs.append(config1)

    for a in xrange(10):
        for config in configs:
            f.write('{}:{}:'.format(a, config.topology))
            brain_c = DBN.DBN(config=config)
            brain_c.pretrain(tr_X, cache=[True, True], train_further=[True, True])

            recon_x = brain_c.reconstruct(te_X, k=1, plot_n=100, img_name='{}_{}_recon'.format(a, config.topology))
            recon = recon_x[:, 784:]
            error = clf.get_score(recon, te_y.eval())
            print error
            f.write('{}, '.format(error))

            for i in xrange(0, 5):
                brain_c.fine_tune(tr_X)
                recon_x = brain_c.reconstruct(te_X, k=1, plot_n=100,
                                              img_name=('{}_{}_recon_fine_tune{}'.format(a, config.topology, i)))
                recon = recon_x[:, 784:]
                error = clf.get_score(recon, te_y.eval())
                print error
                f.write('{}, '.format(error))
                recon_x = brain_c.reconstruct(te_X4, k=1, plot_n=100,
                                              img_name=('{}_{}_recon_fine_tune_2_{}'.format(a, config.topology, i)))
                recon = recon_x[:, 784:]
                error = clf.get_score(recon, te_y.eval())
                print error
                f.write('{}, '.format(error))
            f.write('\n')
    f.close()
コード例 #8
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)
コード例 #9
0
def KanadeAssociativeRBM(cache=False, train_further=False):
    print "Testing Associative RBM which tries to learn the ID map "
    # print "Testing Associative RBM which tries to learn the following mapping: {anger, saddness, disgust} -> {sadness}, {contempt, happy, surprise} -> {happy}"
    # project set-up
    data_manager = store.StorageManager('Kanade/OptMFSparse0.01RBMTest',
                                        log=True)
    # data_manager = store.StorageManager('Kanade/OptAssociativeRBMTest', log=True)
    shape = 25
    dataset_name = 'sharp_equi{}_{}'.format(shape, shape)

    # Load kanade database
    mapping = None  # id map
    # mapping = {'anger': 'sadness', 'contempt': 'happy', 'disgust': 'sadness', 'fear': 'sadness', 'happy': 'happy',
    #            'sadness': 'sadness', 'surprise': 'happy'}
    train, valid, test = loader.load_kanade(pre={'scale': True},
                                            set_name=dataset_name)
    train_x, train_y = train
    test_x, test_y = test

    # Sample associated image
    train_x_mapped, train_y_mapped = loader.sample_image(train_y,
                                                         mapping=mapping,
                                                         pre={'scale': True},
                                                         set_name=dataset_name)
    test_x_mapped, test_y_mapped = loader.sample_image(test_y,
                                                       mapping=mapping,
                                                       pre={'scale': True},
                                                       set_name=dataset_name)

    # Concatenate images
    concat1 = T.concatenate([train_x, train_x_mapped], axis=1)
    # concat2 = T.concatenate([train_x_mapped, train_x], axis=1)
    # concat = T.concatenate([concat1, concat2], axis=0)
    # train_tX = theano.function([], concat)()
    train_tX = theano.function([], concat1)()
    train_X = theano.shared(train_tX)

    # Train classifier to be used for classifying reconstruction associated image layer
    # mapped_data = loader.load_kanade(#emotions=['sadness', 'happy'],
    #                                  pre={'scale': True},
    #                                  set_name=dataset_name)  # Target Image
    # clf_orig = SimpleClassifier('logistic', mapped_data[0][0], mapped_data[0][1])
    clf_orig = SimpleClassifier('logistic', train_x, train_y)

    # Initialise RBM
    tr = rbm_config.TrainParam(learning_rate=0.0001,
                               momentum_type=rbm_config.NESTEROV,
                               momentum=0.9,
                               weight_decay=0.0001,
                               sparsity_constraint=True,
                               sparsity_target=0.01,
                               sparsity_cost=100,
                               sparsity_decay=0.9,
                               batch_size=10,
                               epochs=10)

    n_visible = shape * shape * 2
    n_hidden = 500

    config = rbm_config.RBMConfig()
    config.v_n = n_visible
    config.h_n = n_hidden
    config.v_unit = rbm_units.GaussianVisibleUnit
    # config.h_unit = rbm_units.ReLUnit
    config.progress_logger = rbm_logger.ProgressLogger(img_shape=(shape * 2,
                                                                  shape))
    config.train_params = tr
    rbm = RBM(config)
    print "... initialised RBM"

    # Load RBM (test)
    loaded = data_manager.retrieve(str(rbm))
    if loaded:
        rbm = loaded
    else:
        rbm.set_initial_hidden_bias()
        rbm.set_hidden_mean_activity(train_X)

    # Train RBM - learn joint distribution
    # rbm.pretrain_lr(train_x, train_x01)
    for i in xrange(0, 10):
        if not cache or train_further:
            rbm.train(train_X)

        data_manager.persist(rbm)

        print "... reconstruction of associated images"
        # Get reconstruction with train data to get 'mapped' images to train classifiers on
        reconstruction = rbm.reconstruct(train_X,
                                         1,
                                         plot_n=100,
                                         plot_every=1,
                                         img_name='recon_train')
        reconstruct_assoc_part = reconstruction[:, (shape**2):]

        # Get associated images of test data
        nsamples = np.random.normal(0, 1,
                                    test_x.get_value(True).shape).astype(
                                        np.float32)
        initial_y = theano.shared(nsamples, name='initial_y')
        utils.save_images(nsamples[0:100], 'initialisation.png', (10, 10),
                          (25, 25))

        test_x_associated = rbm.reconstruct_association_opt(
            test_x,
            initial_y,
            5,
            0.,
            plot_n=100,
            plot_every=1,
            img_name='recon_test_gibbs')

        mf_recon = rbm.mean_field_inference_opt(test_x,
                                                y=initial_y,
                                                sample=False,
                                                k=10,
                                                img_name='recon_test_mf_raw')

        # Concatenate images
        test_MFX = theano.function([], T.concatenate([test_x, mf_recon],
                                                     axis=1))()
        test_MF = theano.shared(test_MFX)
        reconstruction = rbm.reconstruct(test_MF,
                                         1,
                                         plot_n=100,
                                         plot_every=1,
                                         img_name='recon_test_mf_recon')
        mf_recon = reconstruction[:, (shape**2):]

        print "... reconstructed"

        # Classify the reconstructions

        # 1. Train classifier on original images
        score_orig = clf_orig.get_score(test_x_associated,
                                        test_y_mapped.eval())
        score_orig_mf = clf_orig.get_score(test_x_associated,
                                           test_y_mapped.eval())

        # 2. Train classifier on reconstructed images
        clf_recon = SimpleClassifier('logistic', reconstruct_assoc_part,
                                     train_y_mapped.eval())
        score_retrain = clf_recon.get_score(test_x_associated,
                                            test_y_mapped.eval())
        score_retrain_mf = clf_recon.get_score(mf_recon, test_y_mapped.eval())

        out_msg = '{} (orig, retrain):{},{}'.format(rbm, score_orig,
                                                    score_retrain)
        out_msg2 = '{} (orig, retrain):{},{}'.format(rbm, score_orig_mf,
                                                     score_retrain_mf)
        print out_msg
        print out_msg2
コード例 #10
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
コード例 #11
0
def KanadeJointDBN(cache=False):
    print "Testing JointDBN which tries to learn id map association"

    # project set-up
    data_manager = store.StorageManager('Kanade/JointDBN', 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=3000,
        pre=preprocessing, set_name=dataset_name)

    mapped_dataset = loader.load_kanade(  # n=3000,
        # 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 RBM parameters
    base_tr = rbm_config.TrainParam(learning_rate=0.0001,
                                    momentum_type=rbm_config.NESTEROV,
                                    momentum=0.9,
                                    weight_decay=0.0001,
                                    sparsity_constraint=False,
                                    sparsity_target=0.00001,
                                    sparsity_decay=0.9,
                                    sparsity_cost=10000,
                                    epochs=100,
                                    batch_size=10)

    rest_tr = rbm_config.TrainParam(learning_rate=0.0001,
                                    momentum_type=rbm_config.CLASSICAL,
                                    momentum=0.5,
                                    weight_decay=0.01,
                                    epochs=100,
                                    batch_size=10)

    # Layer 1
    # Layer 2
    # Layer 3
    topology = [2 * (shape**2), 100, 100]
    # batch_size = 10
    first_progress_logger = rbm_logger.ProgressLogger(img_shape=(shape * 2,
                                                                 shape))
    rest_progress_logger = rbm_logger.ProgressLogger()

    first_rbm_config = rbm_config.RBMConfig(
        train_params=base_tr, progress_logger=first_progress_logger)
    first_rbm_config.v_unit = rbm_units.GaussianVisibleUnit
    rest_rbm_config = rbm_config.RBMConfig(
        train_params=rest_tr, progress_logger=rest_progress_logger)
    rbm_configs = [first_rbm_config, rest_rbm_config, rest_rbm_config]

    config = DBN.DBNConfig(topology=topology,
                           training_parameters=base_tr,
                           rbm_configs=rbm_configs,
                           data_manager=data_manager)

    # construct the Deep Belief Network
    dbn = DBN.DBN(config)

    # Train DBN on concatenated images
    train_tX = theano.function([], T.concatenate([train_x, train_x_ass],
                                                 axis=1))()
    train_X = theano.shared(train_tX)
    test_tX = theano.function([], T.concatenate([test_x, test_x_ass],
                                                axis=1))()
    test_X = theano.shared(test_tX)
    test_tX2 = theano.function([],
                               T.concatenate(
                                   [test_x, T.zeros_like(test_x)], axis=1))()
    test_X2 = theano.shared(test_tX2)

    origs = []
    recons = []
    recons2 = []

    # Train DBN
    dbn.pretrain(train_X,
                 cache=[True, True, False],
                 train_further=[True, True, True])

    recon = dbn.reconstruct(train_X,
                            k=1,
                            plot_n=20,
                            img_name='stackedRBM_train_recon_{}_{}'.format(
                                topology, 0))
    train_x_ass_recon = recon[:, shape**2:]

    recon = dbn.reconstruct(test_X,
                            k=1,
                            plot_n=20,
                            img_name='stackedRBM_test_recon_{}_{}'.format(
                                topology, 0))
    test_x_ass_recon = recon[:, shape**2:]

    recon = dbn.reconstruct(test_X2,
                            k=2,
                            plot_n=20,
                            img_name='stackedRBM_test_zero_recon_{}_{}'.format(
                                topology, 0))
    test_x_ass_recon2 = recon[:, shape**2:]

    clf_recon = SimpleClassifier('logistic', train_x, train_y)
    score_orig = clf_recon.get_score(test_x_ass_recon, test_y_ass.eval())

    clf_recon.retrain(train_x_ass_recon, train_y_ass.eval())
    score_recon = clf_recon.get_score(test_x_ass_recon, test_y_ass.eval())
    score_recon2 = clf_recon.get_score(test_x_ass_recon2, test_y_ass.eval())

    print 'classification rate: {}, {}, {}'.format(score_orig, score_recon,
                                                   score_recon2)
    origs.append(score_orig)
    recons.append(score_recon)
    recons2.append(score_recon2)
コード例 #12
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