예제 #1
0
import gzip
import numpy as np
import cPickle as pkl
import nn_utils as nnu
import theano
from VMGNet import VMGNet
floatX = theano.config.floatX

f = gzip.open('data/mnist.pkl.gz', 'rb')
(xtrain, ytrain), (xvalid, yvalid), (xtest, ytest) = pkl.load(f)
f.close()

n_y = np.unique(ytrain).shape[0]
idx = nnu.prng.permutation(range(xtrain.shape[0]))
xtrain, ytrain = xtrain[idx], ytrain[idx]
xtrain, xtest = np.cast[floatX](xtrain), np.cast[floatX](xtest)

nn = VMGNet(xtrain.shape[0], xtrain.shape[1], n_y, batch_size=100, dimh=(150, 150, 150), n_iter=100,
            logtxt='vmgnet.txt', type_init='he2', n_inducing=50, ind_noise_lvl=0.01, task_type='classification')

output_nn = nn.fit(xtrain, ytrain, xvalid=xvalid, yvalid=yvalid, xtest=xtest, ytest=ytest, sampling_rounds=1)

preds = nn.predict(xtest, samples=1)
print 'Mean Test error:', 100. * ((preds != ytest).sum() / (1. * ytest.shape[0]))
예제 #2
0
    """Return the Avg. Test Log-Likelihood
    """
    if y.shape[1] == 1:
        y = y.ravel()
    sample_ll = np.zeros((sample_preds.shape[1], sample_preds.shape[0]))
    a, b = np.exp(nn.extra_inf['a1'].get_value()), np.exp(nn.extra_inf['b1'].get_value())
    etau, elogtau = (a / b).astype(np.float32), (psi(a) - np.log(b)).astype(np.float32)
    for sample in xrange(sample_preds.shape[0]):
        ypred = sample_preds[sample].astype(np.float32)
        if len(y.shape) > 1:
            sll = -.5 * np.sum(etau * (y - ypred)**2, axis=1)
        else:
            sll = -.5 * etau * (y - ypred)**2
        sample_ll[:, sample] = sll

    return np.mean(logsumexp(sample_ll, axis=1) - np.log(sample_preds.shape[0]) - .5 * np.log(2*np.pi) + .5 * elogtau.sum())


xtrain, xtest, ytrain, ytest = cv.train_test_split(x, y, train_size=0.9, random_state=1)
std_scx = StandardScaler().fit(xtrain)
xtrain, xtest = std_scx.transform(xtrain), std_scx.transform(xtest)
nn = VMGNet(xtrain.shape[0], xtrain.shape[1], ytrain.shape[1], batch_size=5, dimh=(50,), n_iter=2000,
            logtxt='vmgnet.txt', seed=3, task_type='regression', sampling_pred=True, type_init='he2', n_inducing=5,
            ind_noise_lvl=0.005)
_, _ = nn.fit(xtrain.astype(np.float32), ytrain.astype(np.float32), xtest=xtest, ytest=ytest, print_every=200,
              llf=loglike, n_samples=2)

ypred, sample_preds = nn.predict(xtest, samples=100)
rmse = np.sqrt(np.mean(np.sum((ytest - ypred) ** 2, axis=1)))
mean_ll = loglike(nn, sample_preds, ytest)
print 'RMSE:', rmse, 'mean_ll:', mean_ll