Example #1
0
def main():
    np.random.seed(0)
    n_data = 10
    x = np.random.uniform(size=n_data)
    #x = np.float32(x)
    x = np.sort(x)
    a = 1
    b = 10
    c = .01
    mu = np.zeros(n_data)
    cov = a * np.exp(-b * (x[:, np.newaxis] - x)**2) + c * np.eye(n_data)
    y = np.random.multivariate_normal(mu, cov)

    #data = 'ECG200/11_1000_60_dat.pkl'
    #gp_parms, ts_train, ts_test, l_train, l_test = pickle_load(data)[:5]
    #x_train = np.array([each_ts[0] for each_ts in ts_train])
    #y_train = np.array([each_ts[1] for each_ts in ts_train])
    #eg_id = 0
    #x = x_train[eg_id]
    #y = y_train[eg_id]

    #print x
    #print y
    x_min, x_max = x.min(), x.max()
    #len_u = 2048 + 1
    len_u = 1024 + 1
    #len_u = 50
    #len_u = 128 + 1
    #len_u = 64
    extra_u = 2
    margin = (x_max - x_min) / (len_u - extra_u * 2) * 2
    u = np.linspace(x_min - margin, x_max + margin, len_u)
    x_test = u[1:]
    #x_test = np.linspace(x_min, x_max, 20)

    gp_params = np.array([a, b])
    indep_noise = c

    idx_train, w_train = sparse_w(u, x)
    idx_test, w_test = sparse_w(u, x_test)
    post_gp = PosteriorGP(u, x_test, x, y, kernel, gp_params, indep_noise)

    batch_size = 10
    cov_zs = post_gp.cov_rand_proj(n_sample=batch_size, n_lanczos_basis=10)
    mu = post_gp.mean()
    gp_samples = mu + cov_zs

    #return

    import pylab as pl
    pl.figure()

    for each_sample in gp_samples:
        pl.plot(x_test, each_sample, '-', c='b', alpha=.5)
    pl.plot(x, y, 'o', c='r')
    pl.show()
Example #2
0
def main():
    np.random.seed(0)
    n_data = 10
    x = np.random.uniform(size=n_data)
    #x = np.float32(x)
    x = np.sort(x)
    a = .1
    b = 10
    c = .001
    mu = np.zeros(n_data)
    cov = a * np.exp(-b * (x[:, np.newaxis] - x)**2) + c * np.eye(n_data)
    y = np.random.multivariate_normal(mu, cov)
    #print x
    #print y
    x_min, x_max = x.min(), x.max()
    #len_u = 2048 + 1
    #len_u = 1024 + 1
    len_u = 50
    #len_u = 128 + 1
    #len_u = 64
    extra_u = 2
    margin = (x_max - x_min) / (len_u - extra_u * 2) * 2
    u = np.linspace(x_min - margin, x_max + margin, len_u)
    #x_test = u[1:]
    x_test = u[2:-1]
    #x_test = np.linspace(x_min, x_max, 20)

    idx_train, w_train = sparse_w(u, x)
    idx_test, w_test = sparse_w(u, x_test)
    post_gp = PosteriorGP(u, x_test, kernel, symbolic_kernel)

    post_gp.log_gp_params.set_value(np.log(np.array([a, b])))
    post_gp.log_indep_noise.set_value(np.log(c))

    batch_size = 10
    cov_zs = post_gp.cov_rand_proj(n_sample=batch_size, n_lanczos_basis=10)
    mu = post_gp.mean()
    gp_samples = mu.dimshuffle('x', 0) + cov_zs

    variables = post_gp.data_variables
    gp_samples_fn = theano.function(variables, gp_samples)

    len_test = len(x_test)
    #y_test = np.random.normal(size=(batch_size, len_test))
    gdraws = gp_samples_fn(idx_train, w_train, idx_test, w_test, y)
    print gdraws.shape

    import pylab as pl
    pl.figure()

    for each_sample in gdraws:
        pl.plot(x_test, each_sample, '-', c='b', alpha=.5)
    pl.plot(x, y, 'o', c='r')
    pl.show()
Example #3
0
    def __init__(
            self,
            n_classes,
            inducing_pts,
            t_test,
            update_gp=True,
            init_gp_params=None,  # kernel parameters & noise parameter
            #n_inducing_pts=50,
            #t_min=0,
            #t_max=1,
        n_lanczos_basis=10,
            net_arch='logreg',
            stochastic_train=True,
            stochastic_predict=False,
            n_samples=10,
            n_epochs=100,
            regularize_weight=0,
            optimizer=adadelta,
            optimizer_kwargs={},
            load_params=None,
            random_seed=123):
        '''
        n_samples: number of Monte Carlo samples to estimate the expectation
        n_inducing_pts: number of inducing points
        '''
        lasagne.random.set_rng(np.random.RandomState(seed=random_seed))
        self.rng = RandomStreams(seed=random_seed)

        if load_params:
            (model_params, network_params,
             init_gp_params) = pickle_load(load_params)
            (self.net_arch, self.n_classes, self.inducing_pts, self.idx_test,
             self.w_test, self.gp_output_len) = model_params
        else:
            self.net_arch = net_arch
            self.n_classes = n_classes
            self.inducing_pts = inducing_pts
            self.idx_test, self.w_test = sparse_w(inducing_pts, t_test)
            self.gp_output_len = len(t_test)

        self.n_lanczos_basis = n_lanczos_basis
        self.n_epochs = n_epochs
        self.n_samples = n_samples
        self.post_gp = PosteriorGP(inducing_pts,
                                   t_test,
                                   kernel,
                                   symbolic_kernel,
                                   init_params=init_gp_params)
        self.update_gp = update_gp
        self.regularize_weight = regularize_weight
        self.optimizer = optimizer
        self.optimizer_kwargs = optimizer_kwargs

        # Save stochastic train/predict flags for storing parameters
        self.stochastic_train = stochastic_train
        self.stochastic_predict = stochastic_predict
        self.compile_train_predict(stochastic_train, stochastic_predict)

        if load_params:
            self.load_params(network_params)
Example #4
0
 def predict_proba(self, x, y):
     test_prediction = []
     for each_x, each_y in izip(x, y):
         idx, w = sparse_w(self.inducing_pts, each_x)
         test_prediction.append(
             self.predict_fn(idx, w, self.idx_test, self.w_test, each_y))
     test_prediction = np.vstack(test_prediction)
     return test_prediction
Example #5
0
    def __init__(self,
                 n_classes,
                 inducing_pts,
                 t_test,
                 update_gp=True,
                 init_gp_params=None,   # kernel parameters & noise parameter
                 #n_inducing_pts=50,
                 #t_min=0,
                 #t_max=1,
                 n_lanczos_basis=10,
                 n_samples=1000,
                 n_epochs=100,
                 gamma=5,
                 regularize_weight=0,
                 optimizer=adadelta,
                 optimizer_kwargs={},
                 load_params=None,
                 random_seed=123):
        '''
        n_samples: number of Monte Carlo samples to estimate the expectation
        n_inducing_pts: number of inducing points
        '''
        lasagne.random.set_rng(np.random.RandomState(seed=random_seed))
        W = np.random.normal(0, 1 / gamma**2, size=(n_samples, len(t_test)))
        b = np.random.uniform(0, 2 * np.pi, size=n_samples)
        self.random_weight = theano.shared(W)
        self.random_offset = theano.shared(b)

        if load_params:
            (model_params,
             network_params,
             init_gp_params) = pickle_load(load_params)
            (self.n_classes,
             self.inducing_pts,
             self.idx_test, self.w_test,
             self.gp_output_len) = model_params
        else:
            self.n_classes = n_classes
            self.inducing_pts = inducing_pts
            self.idx_test, self.w_test = sparse_w(inducing_pts, t_test)
            self.gp_output_len = len(t_test)

        self.n_lanczos_basis = n_lanczos_basis
        self.n_epochs = n_epochs
        self.n_samples = n_samples
        self.post_gp = PosteriorGP(inducing_pts, t_test,
                                   kernel, symbolic_kernel,
                                   init_params=init_gp_params)
        self.update_gp = update_gp
        self.regularize_weight = regularize_weight
        self.optimizer = optimizer
        self.optimizer_kwargs = optimizer_kwargs

        self.compile_train_predict()

        if load_params:
            self.load_params(network_params)
Example #6
0
 def __init__(self,
              inducing_pts,
              t_test,
              t_train,
              y_train,
              kernel,
              gp_params,
              indep_noise,
              random_seed=101):
     self.rng = np.random.RandomState(random_seed)
     self.len_test = len(t_test)
     self.gp_params = gp_params
     self.indep_noise = indep_noise
     self.idx_train, self.w_train = sparse_w(inducing_pts, t_train)
     self.idx_test, self.w_test = sparse_w(inducing_pts, t_test)
     self.y_train = y_train
     t_diff = inducing_pts - inducing_pts[0]
     self.u = kernel(t_diff, gp_params)
     self.len_u = len(inducing_pts)
     self.gp_params = gp_params
     self.indep_noise = indep_noise
Example #7
0
    def inspect_train(self,
                      x_train,
                      y_train,
                      l_train,
                      x_valid,
                      y_valid,
                      l_valid,
                      x_test,
                      y_test,
                      l_test,
                      save_params=None):
        idx_w = []
        for each_x in x_train:
            idx_w.append(sparse_w(self.inducing_pts, each_x))
        for epoch in xrange(self.n_epochs):
            history = []
            count = 1
            t1 = time.time()
            for (idx_train,
                 w_train), each_y, each_label in izip(idx_w, y_train, l_train):
                history.append(
                    self.train_fn(idx_train, w_train, self.idx_test,
                                  self.w_test, each_y, [each_label]))
                count += 1
                if count % 20 == 0:
                    sys.stdout.write('.')
                    sys.stdout.flush()
                #for v in self.gp_hyperparams:
                #    print v.get_value()
            t2 = time.time()
            print ' ', t2 - t1
            mean_loss = np.mean(history)

            if self.copy_params:
                all_params = get_all_param_values(self.train_network)
                set_all_param_values(self.test_network, all_params)

            accuracy_train, loss_train = self.evaluate_prediction(
                x_train, y_train, l_train)
            accuracy_valid, loss_valid = self.evaluate_prediction(
                x_valid, y_valid, l_valid)
            accuracy_test, loss_test = self.evaluate_prediction(
                x_test, y_test, l_test)

            print '%4d - %.5f  %.5f  %.5f  %.5f  %.5f  %.5f  %.5f' % (
                epoch, mean_loss, accuracy_train, loss_train, accuracy_valid,
                loss_valid, accuracy_test, loss_test)
            for v in self.gp_hyperparams:
                print v.get_value()

            if save_params:
                self.save_params(save_params + '-%03d' % epoch)
Example #8
0
def approx_gp_sample(x,
                     y,
                     x_test,
                     gp_params,
                     indep_noise,
                     eps,
                     len_u,
                     lanczos_basis=10,
                     return_mean=False):
    x_min, x_max = x.min(), x.max()
    extra_u = 2  # for better interpolation
    margin = (x_max - x_min) / (len_u - extra_u * 2) * 2
    u = np.linspace(x_min - margin, x_max + margin, len_u)

    idx_train, w_train = sparse_w(u, x)
    idx_test, w_test = sparse_w(u, x_test)
    post_gp = PosteriorGP(u, x_test, x, y, kernel, gp_params, indep_noise)
    cov_zs = post_gp.cov_rand_proj(n_lanczos_basis=lanczos_basis, eps=eps)
    mu = post_gp.mean()
    sample = mu + cov_zs
    if return_mean:
        return mu, sample
    return sample
Example #9
0
    def train(self, x, y, label):
        idx_w = []
        for each_x in x:
            idx_w.append(sparse_w(self.inducing_pts, each_x))
        history = [[] for i in xrange(len(x))]
        mean_loss_history = []
        for epoch in xrange(self.n_epochs):
            for i, ((idx_train, w_train), each_y,
                    each_label) in enumerate(izip(idx_w, y, label)):
                history[i].append(
                    self.train_fn(idx_train, w_train, self.idx_test,
                                  self.w_test, each_y, [each_label]))
            mean_loss_history.append(
                np.mean([each_loss[-1] for each_loss in history]))
            print '%4d -' % epoch, mean_loss_history[-1]

        if self.copy_params:
            all_params = get_all_param_values(self.train_network)
            set_all_param_values(self.test_network, all_params)
Example #10
0
def main():
    from fast_gp import sparse_w
    np.random.seed(0)
    n_data = 10
    x = np.random.uniform(size=n_data)
    #x = np.float32(x)
    x = np.sort(x)
    a = .1
    b = 10
    c = .001
    mu = np.zeros(n_data)
    cov = a * np.exp(-b * (x[:, np.newaxis] - x)**2) + c * np.eye(n_data)
    y = np.random.multivariate_normal(mu, cov)
    #print x
    #print y
    x_min, x_max = x.min(), x.max()
    #len_u = 2048 + 1
    #len_u = 1024 + 1
    len_u = 64
    extra_u = 2
    margin = (x_max - x_min) / (len_u - extra_u * 2) * 2
    u = np.linspace(x_min - margin, x_max + margin, len_u)
    #x_test = u[1:]
    x_test = np.linspace(x_min, x_max, 20)
    idx_train, w_train = sparse_w(u, x)
    idx_test, w_test = sparse_w(u, x_test)
    len_test = len(x_test)
    y_test = np.random.uniform(size=(2, len_test))
    #y_test = np.random.uniform(size=(1, len_test))

    post_mean = PosteriorMean(u, kernel, symbolic_kernel)

    def sub_mean(t_gp_params, t_indep_noise):
        return post_mean(idx_train, w_train, idx_test, w_test,
                         t_gp_params, t_indep_noise, y * 1)

    print 'verify grad mean'
    theano.tests.unittest_tools.verify_grad(sub_mean, [(a, b), c],
                                            n_tests=5, eps=1.0e-7,
                                            abs_tol=0.001, rel_tol=0.001)

    #return

    cov_vec = CovVec(u, kernel, symbolic_kernel)

    t_idx_train = theano.tensor.imatrix()
    t_w_train = theano.tensor.matrix()
    t_idx_test = theano.tensor.imatrix()
    t_w_test = theano.tensor.matrix()
    t_gp_params = theano.tensor.vector()
    t_indep_noise = theano.tensor.scalar()
    t_ys = theano.tensor.matrix()
    t_y = theano.tensor.vector()

    v = cov_vec(t_idx_train, t_w_train, t_idx_test, t_w_test,
                t_gp_params, t_indep_noise, t_ys)
    ys1 = t_ys - v
    v = cov_vec(t_idx_train, t_w_train, t_idx_test, t_w_test,
                t_gp_params, t_indep_noise, ys1)
    v_fn = theano.function([t_idx_train, t_w_train, t_idx_test, t_w_test,
                            t_gp_params, t_indep_noise, t_ys], v)

    def vf(t_gp_params, t_indep_noise):
        v = cov_vec(idx_train, w_train, idx_test, w_test,
                    t_gp_params, t_indep_noise, y_test)
        ys1 = -y_test + v
        v = cov_vec(idx_train, w_train, idx_test, w_test,
                    t_gp_params, t_indep_noise, ys1)
        ys1 = y_test - v
        v = cov_vec(idx_train, w_train, idx_test, w_test,
                    t_gp_params, t_indep_noise, ys1)
        return v

    print 'verify grad ##'
    theano.tests.unittest_tools.verify_grad(vf, [(a, b), c],
                                            n_tests=10, eps=1.0e-6,
                                            #abs_tol=0.001, rel_tol=0.001)
                                            abs_tol=0.01, rel_tol=0.01)
    print '###'
    vsum = v.sum()

    vsum_fn = theano.function([t_idx_train, t_w_train,
                               t_idx_test, t_w_test,
                               t_gp_params, t_indep_noise, t_ys],
                               vsum)
    v_ = vsum_fn(idx_train, w_train, idx_test, w_test, (a, b), c, y_test)
    print v_

    grad_vsum = theano.grad(vsum, [t_gp_params, t_indep_noise])
    grad_vsum_fn = theano.function([t_idx_train,
                                    t_w_train,
                                    t_idx_test,
                                    t_w_test,
                                    t_gp_params,
                                    t_indep_noise,
                                    t_ys
                                    ],
                                   grad_vsum,
                                   #on_unused_input='ignore'
                                   )
    grad_v =  grad_vsum_fn(idx_train, w_train, idx_test, w_test, (a, b), c,
                           y_test)
    print 'grad v'
    print grad_v

    def sub_cov_vec(t_gp_params, t_indep_noise):
        return cov_vec(idx_train, w_train, idx_test, w_test,
                       t_gp_params, t_indep_noise, y_test)

    print 'verify grad'
    theano.tests.unittest_tools.verify_grad(sub_cov_vec, [(a, b), c],
                                            n_tests=5, eps=1.0e-5,
                                            abs_tol=0.001, rel_tol=0.001)

    v_fn = theano.function([t_idx_train, t_w_train, t_idx_test, t_w_test,
                            t_gp_params, t_indep_noise, t_ys], v)

    v_ = v_fn(idx_train, w_train, idx_test, w_test, (a, b), c, y_test)
    print 'v'
    print v_
    #return
    var = v_fn(idx_train, w_train, idx_test, w_test, (a, b), c,
               np.eye(len_test))
    var = np.diag(var)

    post_mean = PosteriorMean(u, kernel, symbolic_kernel)
    pmean = post_mean(t_idx_train, t_w_train, t_idx_test, t_w_test,
                   t_gp_params, t_indep_noise, t_y)
    pmean_fn = theano.function([t_idx_train, t_w_train, t_idx_test, t_w_test,
                             t_gp_params, t_indep_noise, t_y], pmean)
    pmu = pmean_fn(idx_train, w_train, idx_test, w_test, (a, b), c, y)

    #print var

    import pylab as pl
    pl.figure()
    std2 = np.sqrt(var) * 2

    color = 'b'
    pl.fill_between(x_test, pmu - std2, pmu + std2, color=color,
                    edgecolor='none', alpha=.3)
    pl.plot(x_test, pmu, '-', c=color)
    pl.plot(x, y, 'o', c=color)
    pl.show()
Example #11
0
def main():
    from fast_gp import sparse_w
    np.random.seed(0)
    n_data = 10
    x = np.random.uniform(size=n_data)
    #x = np.float32(x)
    x = np.sort(x)
    a = .1
    b = 10
    c = .001
    mu = np.zeros(n_data)
    cov = a * np.exp(-b * (x[:, np.newaxis] - x)**2) + c * np.eye(n_data)
    y = np.random.multivariate_normal(mu, cov)
    #print x
    #print y
    x_min, x_max = x.min(), x.max()
    #len_u = 2048 + 1
    len_u = 1024 + 1
    #len_u = 128 + 1
    #len_u = 64
    extra_u = 2
    margin = (x_max - x_min) / (len_u - extra_u * 2) * 2
    u = np.linspace(x_min - margin, x_max + margin, len_u)
    x_test = u[1:]
    #x_test = np.linspace(x_min, x_max, 20)
    idx_train, w_train = sparse_w(u, x)
    idx_test, w_test = sparse_w(u, x_test)

    t_idx_train = T.imatrix()
    t_w_train = T.matrix()
    t_idx_test = T.imatrix()
    t_w_test = T.matrix()
    t_gp_params = T.vector()
    t_indep_noise = T.scalar()
    t_ys = T.matrix()
    t_y = T.vector()

    cov_vec = CovVec(u, kernel, symbolic_kernel)

    def linear_op(zs):
        return cov_vec(t_idx_train, t_w_train, t_idx_test, t_w_test,
                       t_gp_params, t_indep_noise, zs)

    n_lanczos_basis = 10
    batch_size = 10
    cov_zs = lanczos(linear_op, t_ys, n_lanczos_basis, batch_size)

    post_mean = PosteriorMean(u, kernel, symbolic_kernel)
    mu = post_mean(t_idx_train, t_w_train, t_idx_test, t_w_test, t_gp_params,
                   t_indep_noise, t_y)

    gp_samples = mu.dimshuffle('x', 0) + cov_zs

    gp_samples_fn = theano.function([
        t_idx_train, t_w_train, t_idx_test, t_w_test, t_gp_params,
        t_indep_noise, t_y, t_ys
    ], gp_samples)

    len_test = len(x_test)
    y_test = np.random.normal(size=(batch_size, len_test))
    gdraws = gp_samples_fn(idx_train, w_train, idx_test, w_test, (a, b), c, y,
                           y_test)
    print gdraws.shape

    t_random_proj = T.matrix()
    val = (gp_samples * t_random_proj).sum()

    val_fn = theano.function([
        t_idx_train, t_w_train, t_idx_test, t_w_test, t_gp_params,
        t_indep_noise, t_y, t_ys, t_random_proj
    ], val)

    grad_val_fn = theano.function([
        t_idx_train, t_w_train, t_idx_test, t_w_test, t_gp_params,
        t_indep_noise, t_y, t_ys, t_random_proj
    ],
                                  theano.grad(val,
                                              wrt=[t_gp_params, t_indep_noise],
                                              consider_constant=[
                                                  t_idx_train, t_w_train,
                                                  t_idx_test, t_w_test, t_y,
                                                  t_ys, t_random_proj
                                              ]))

    grad_val_fn1 = theano.function([
        t_idx_train, t_w_train, t_idx_test, t_w_test, t_gp_params,
        t_indep_noise, t_y, t_ys, t_random_proj
    ],
                                   theano.grad(val,
                                               wrt=[t_random_proj],
                                               consider_constant=[
                                                   t_idx_train, t_w_train,
                                                   t_idx_test, t_w_test, t_y,
                                                   t_ys
                                               ]))

    random_proj = np.random.rand(batch_size, len_test)
    t1 = time.time()
    for _ in xrange(10):
        grad_val_fn(idx_train, w_train, idx_test, w_test, (a, b), c, y, y_test,
                    random_proj)
    t2 = time.time()
    print t2 - t1
    t1 = time.time()
    for _ in xrange(10):
        grad_val_fn1(idx_train, w_train, idx_test, w_test, (a, b), c, y,
                     y_test, random_proj)
    t2 = time.time()
    print t2 - t1
    return

    n_test = 10
    for _ in xrange(n_test):
        random_proj = np.random.rand(batch_size, len_test)
        print 'test grad'
        print val_fn(idx_train, w_train, idx_test, w_test, (a, b), c, y,
                     y_test, random_proj)
        print grad_val_fn(idx_train, w_train, idx_test, w_test, (a, b), c, y,
                          y_test, random_proj)

        def val_fn1(x):
            a, b, c = x
            return val_fn(idx_train, w_train, idx_test, w_test, (a, b), c, y,
                          y_test, random_proj)

        def grad_val_fn1(x):
            a, b, c = x
            [a, b], c = grad_val_fn(idx_train, w_train, idx_test, w_test,
                                    (a, b), c, y, y_test, random_proj)
            return np.array([a, b, c])

        print scipy.optimize.check_grad(val_fn1, grad_val_fn1,
                                        np.array([a, b, c]))

    return
    import pylab as pl
    pl.figure()

    for each_sample in gdraws:
        pl.plot(x_test, each_sample, '-', c='b', alpha=.5)
    pl.plot(x, y, 'o', c='r')
    pl.show()
Example #12
0
def norm_diff():
    n_data = 1000
    x = np.random.uniform(size=n_data)
    #x = np.float32(x)
    x = np.sort(x)
    a = 1
    b = 1000.
    c = .01
    mu = np.zeros(n_data)
    cov = a * np.exp(-b * (x[:, np.newaxis] - x)**2) + c * np.eye(n_data)
    y = np.random.multivariate_normal(mu, cov)

    gp_params = np.array([a, b])
    indep_noise = c

    #data = 'ECG200/11_1000_60_dat.pkl'
    #gp_parms, ts_train, ts_test, l_train, l_test = pickle_load(data)[:5]
    #x_train = np.array([each_ts[0] for each_ts in ts_train])
    #y_train = np.array([each_ts[1] for each_ts in ts_train])
    #eg_id = 0
    #x = x_train[eg_id]
    #y = y_train[eg_id]

    #print x
    #print y
    x_min, x_max = x.min(), x.max()
    len_u = 4096
    #for t in xrange(3):

    ####################
    extra_u = 2
    margin = (x_max - x_min) / (len_u - extra_u * 2) * 2
    u = np.linspace(x_min - margin, x_max + margin, len_u)
    x_test = u[1:]

    idx_train, w_train = sparse_w(u, x)
    idx_test, w_test = sparse_w(u, x_test)
    post_gp = PosteriorGP(u, x_test, x, y, kernel, gp_params, indep_noise)

    batch_size = 10
    eps = np.random.normal(size=(batch_size, len(x_test)))
    '''
    exact_samples = exact_gp_sample(x, y, x_test,
                                    gp_params, indep_noise, eps)

    for n_basis in xrange(2, 20 + 1):

        print n_basis
        cov_zs = post_gp.cov_rand_proj(n_sample=batch_size,
                                       n_lanczos_basis=n_basis, eps=eps)
        mu = post_gp.mean()
        gp_samples = mu + cov_zs
        print mu

        #sor_samples = sor_gp_sample(x, y, x_test, u,
        #                            gp_params, indep_noise, eps)
        #print np.linalg.norm(gp_samples - sor_samples)
        #print np.linalg.norm(sor_samples - exact_samples)

        print np.linalg.norm(gp_samples - exact_samples)
        print

    print '-' * 30
    '''

    x_test = np.linspace(x_min, x_max, 512)
    #x_test = np.linspace(x_min, x_max, 1024)
    eps = np.random.normal(size=(batch_size, len(x_test)))
    exact_mu, exact_samples = exact_gp_sample(x,
                                              y,
                                              x_test,
                                              gp_params,
                                              indep_noise,
                                              eps,
                                              return_mean=True)

    import pylab as pl
    pl.figure()
    pl.ion()
    pl.plot(x, y, '.-')
    pl.plot(x_test, exact_mu, '-')
    pl.pause(0.001)

    for log_len_u in xrange(3, 12):
        len_u = 2**log_len_u
        print len_u

        mu, gp_samples = approx_gp_sample(x,
                                          y,
                                          x_test,
                                          gp_params,
                                          indep_noise,
                                          eps,
                                          len_u,
                                          lanczos_basis=10,
                                          return_mean=True)
        pl.plot(x_test, mu, '-')
        pl.pause(0.001)

        print np.linalg.norm(gp_samples - exact_samples)
        print
    pl.ioff()
    pl.show()
Example #13
0
def time_approx(x,
                y,
                x_test,
                gp_params,
                indep_noise,
                len_u,
                proj_1d,
                proj_2d,
                lanczos_basis=5,
                batch_size=1):
    extra_u = 2
    x_min, x_max = x.min(), x.max()
    margin = (x_max - x_min) / (len_u - extra_u * 2) * 2
    u = np.linspace(x_min - margin, x_max + margin, len_u)

    t_proj_1d = T.vector()
    t_proj_2d = T.matrix()

    idx_train, w_train = sparse_w(u, x)
    idx_test, w_test = sparse_w(u, x_test)
    post_gp = PosteriorGP(u, x_test, kernel, symbolic_kernel)
    post_gp.log_gp_params.set_value(np.log(gp_params))
    post_gp.log_indep_noise.set_value(np.log(indep_noise))

    mu = post_gp.mean()
    cov_zs = post_gp.cov_rand_proj(n_sample=batch_size,
                                   n_lanczos_basis=lanczos_basis)
    approx_gp_samples = mu.dimshuffle('x', 0) + cov_zs

    approx_fn = theano.function([
        post_gp.t_idx_train, post_gp.t_w_train, post_gp.t_idx_test,
        post_gp.t_w_test, post_gp.t_y_train
    ], approx_gp_samples)

    approx_bp = theano.grad((approx_gp_samples * t_proj_2d).sum(),
                            post_gp.params)

    approx_bp_fn = theano.function([
        post_gp.t_idx_train, post_gp.t_w_train, post_gp.t_idx_test,
        post_gp.t_w_test, post_gp.t_y_train, t_proj_2d
    ], approx_bp)

    approx_mu_fn = theano.function([
        post_gp.t_idx_train, post_gp.t_w_train, post_gp.t_idx_test,
        post_gp.t_w_test, post_gp.t_y_train
    ], mu)

    approx_mu_bp_fn = theano.function([
        post_gp.t_idx_train, post_gp.t_w_train, post_gp.t_idx_test,
        post_gp.t_w_test, post_gp.t_y_train, t_proj_1d
    ], theano.grad(mu.dot(t_proj_1d), post_gp.params))

    t1 = time.time()
    approx_fn(idx_train, w_train, idx_test, w_test, y)
    t2 = time.time()
    print_label('approx sample')
    print t2 - t1
    sample_time = t2 - t1

    t1 = time.time()
    approx_bp_fn(idx_train, w_train, idx_test, w_test, y, proj_2d)
    t2 = time.time()
    print_label('approx sample grad')
    print t2 - t1
    grad_sample_time = t2 - t1

    t1 = time.time()
    approx_mu_fn(idx_train, w_train, idx_test, w_test, y)
    t2 = time.time()
    print_label('approx mean')
    print t2 - t1
    mean_time = t2 - t1

    t1 = time.time()
    approx_mu_bp_fn(idx_train, w_train, idx_test, w_test, y, proj_1d)
    t2 = time.time()
    print_label('approx mean grad')
    print t2 - t1
    grad_mean_time = t2 - t1

    return sample_time, grad_sample_time, mean_time, grad_mean_time
Example #14
0
def test_gp():
    np.random.seed(0)
    n_data = 10
    x = np.random.uniform(size=n_data)
    #x = np.float32(x)
    x = np.sort(x)
    a = .1
    b = 20
    c = .001
    mu = np.zeros(n_data)
    cov = a * np.exp(-b * (x[:, np.newaxis] - x)**2) + c * np.eye(n_data)
    y = np.random.multivariate_normal(mu, cov)
    #print x
    #print y
    x_min, x_max = x.min(), x.max()
    #len_u = 2048 + 1
    #len_u = 1024 + 1
    len_u = 50
    extra_u = 2
    margin = (x_max - x_min) / (len_u - extra_u * 2) * 2
    u = np.linspace(x_min - margin, x_max + margin, len_u)
    x_test = u[1:]
    #x_test = u
    #x_test = u[2:-1]
    idx_train, w_train = sparse_w(u, x)
    idx_test, w_test = sparse_w(u, x_test)

    ku = covmat_col0(u, (a, b))
    #print 'ku'
    #print ku
    #print (cov_fn(u, u, (a, b)) + np.eye(len_u))[:, 0]
    len_test = len(x_test)
    import time

    #return
    #mu = post_mean(idx_train, w_train, idx_test, w_test, ku, len_u, c, y)

    t1 = time.time()
    var1 = post_cov_ys(idx_train, w_train, idx_test, w_test, ku, len_u, c,
                       np.eye(len_test))
    t2 = time.time()
    print t2 - t1

    t1 = time.time()
    var2 = post_cov_ys_block(idx_train, w_train, idx_test, w_test, ku, len_u,
                             c, np.eye(len_test))
    t2 = time.time()
    print t2 - t1
    print np.allclose(var1, var2)

    grad_u = np.random.normal(size=(len_u, 2))
    gz = np.ones((len_test, len_test))

    t1 = time.time()
    g1 = grad_cov_ys(idx_train, w_train, idx_test, w_test, ku, (a, b), c,
                     np.eye(len_test), grad_u, gz)
    t2 = time.time()
    print t2 - t1

    t1 = time.time()
    g2 = grad_cov_ys_block(idx_train, w_train, idx_test, w_test, ku, (a, b), c,
                           np.eye(len_test), grad_u, gz)
    t2 = time.time()
    print t2 - t1
    for a, b in zip(g1, g2):
        print np.allclose(a, b)