n_hidden = [500, 500]
n_epochs = 200
x_dist = 'Bernoulli'
temp_epochs, start_temp = None, 0.0
l2_reg, initVar, alpha = float(sys.argv[2]), -10., 1.1
batchnorm, mc_samps = True, int(sys.argv[6])
eval_samps = 1000
binarize, logging, verbose = True, False, 2

test_ll, test_ac = [], []
for betaNum, beta in enumerate(betaset):
    print("beta = {:5.3f}".format(beta))
    for run in range(runs):
        seed = seeds[run]
        print("Starting work on run: {}".format(run))
        data = mnist(target, threshold=threshold)
        data.create_semisupervised(num_labeled)
        epoch2steps, epoch_decay = data.n_train / u_bs, 50
        Data = SSL_DATA(data.x_unlabeled,
                        data.y_unlabeled,
                        x_test=data.x_test,
                        y_test=data.y_test,
                        x_labeled=data.x_labeled,
                        y_labeled=data.y_labeled,
                        dataset='mnist',
                        seed=seed)
        n_x, n_y = Data.INPUT_DIM, Data.NUM_CLASSES

        if modelName == 'm2':
            # standard M2: Kingma et al. (2014)
            model = m2(n_x,
Beispiel #2
0
def load_dataset(dataset_name, preproc=True, threshold=0.1, greyscale=False):
    """ load up data, either mnist, cifar10, cifar100 or svhn
    from mnist, optional arguments:
    'threshold' keeps only elements of data where over the dataset their
     variance > threshold. This is to prevent perfect matching to
     pixels in e.g. the corners of the image that are always =0

    for svhn, optional arguments:
    'preproc' to do optional PCA extraction of data
    'greyscale' to convert to greyscale images
    """

    if dataset_name == 'mnist':
        target = '/jmain01/home/JAD017/sjr01/mxw35-sjr01/Projects/generativeSSL-master/data/mnist.pkl.gz'
        data = mnist(target, threshold=threshold)

        x_train, y_train = data.x_train, data.y_train
        x_test, y_test = data.x_test, data.y_test

        binarize = True
        x_dist = 'Bernoulli'
        n_y = 10
        f_enc, f_dec = lambda x: x, lambda x: x
        n_x = x_train.shape[1]

    elif dataset_name == 'cifar10':

        (x_train, y_train), (x_test, y_test) = cifar10.load_data()

        binarize = False
        x_dist = 'Gaussian'
        n_y = 10
        f_enc, f_dec = lambda x: x, lambda x: x
        n_x = x_train.shape[1]

    elif dataset_name == 'cifar100':
        (x_train, y_train), (x_test, y_test) = cifar100.load_data()

        binarize = False
        x_dist = 'Gaussian'
        n_y = 100
        f_enc, f_dec = lambda x: x, lambda x: x
        n_x = x_train.shape[1]

    elif dataset_name == 'svhn':

        train = scipy.io.loadmat('/jmain01/home/JAD017/sjr01/mxw35-sjr01/SVHN/train_32x32.mat')
        train_x = train['X'].swapaxes(0, 1).T.reshape((train['X'].shape[3], -1)).T
        train_y = train['y'].reshape((-1)) - 1

        test = scipy.io.loadmat('/jmain01/home/JAD017/sjr01/mxw35-sjr01/SVHN/test_32x32.mat')
        test_x = test['X'].swapaxes(0, 1).T.reshape((test['X'].shape[3], -1)).T
        test_y = test['y'].reshape((-1)) - 1

        extra = scipy.io.loadmat('/jmain01/home/JAD017/sjr01/mxw35-sjr01/SVHN/extra_32x32.mat')
        extra_x = extra['X'].swapaxes(0, 1).T.reshape((extra['X'].shape[3], -1)).T
        extra_y = extra['y'].reshape((-1)) - 1

        train_x = train_x.astype(np.float32) / 256.
        test_x = test_x.astype(np.float32) / 256.
        extra_x = extra_x.astype(np.float32) / 256.

        y_train = np_utils.to_categorical(train_y)
        y_test = np_utils.to_categorical(test_y)
        y_extra = np_utils.to_categorical(extra_y)

        x_train = np.hstack((train_x, extra_x))
        y_train = np.vstack((y_train, y_extra))

        if greyscale is False:
            binarize = False
            x_dist = 'Gaussian'
        elif greyscale is True:
            x_train = rgb2gray(x_train).astype(np.float32)
            test_x = rgb2gray(test_x).astype(np.float32)

        n_y = 10

        if preproc is True:
            f_enc, f_dec, pca_params = pp.PCA(train_x[:, :10000],
                                              cutoff=1000, toFloat=False)
            x_train = f_enc(x_train).astype(np.float32).T
            x_test = f_enc(test_x).astype(np.float32).T
        else:
            x_train = np.transpose(x_train)
            x_test = np.transpose(test_x)
            f_enc, f_dec = lambda x: x, lambda x: x

        n_x = x_train.shape[1]

    return x_train, y_train, x_test, y_test, binarize, x_dist, n_y, n_x, f_enc, f_dec
Beispiel #3
0
from collections import Counter
from matplotlib import rc
import pickle, sys, pdb, gzip, cPickle
import numpy as np
from sklearn.metrics import log_loss
import tensorflow as tf
from data.Container import Container as Data
from data.mnist import mnist
from models.bnn import bnn
### Script to run a BNN experiment over the moons data

seed = np.random.randint(1, 23523452)
threshold = 0.1

## load data
data = mnist(threshold=threshold)
data = Data(data.x_train,
            data.y_train,
            x_test=data.x_test,
            y_test=data.y_test,
            dataset='mnist',
            seed=seed)

n_x, n_y = data.INPUT_DIM, data.NUM_CLASSES

## Specify model parameters
lr = (3e-4, )
n_hidden = [512, 512]
n_epochs, batchsize = 30, 64
initVar, eval_samps = -10.0, None
batchnorm = 'standard'