コード例 #1
0
ファイル: warped_gp.py プロジェクト: luxun1/GPy
    def __init__(self,
                 X,
                 Y,
                 kernel=None,
                 warping_function=None,
                 warping_terms=3,
                 normalize_X=False,
                 normalize_Y=False):

        if kernel is None:
            kernel = kern.rbf(X.shape[1])

        if warping_function == None:
            self.warping_function = TanhWarpingFunction_d(warping_terms)
            self.warping_params = (
                np.random.randn(self.warping_function.n_terms * 3 + 1, ) * 1)

        self.scale_data = False
        if self.scale_data:
            Y = self._scale_data(Y)
        self.has_uncertain_inputs = False
        self.Y_untransformed = Y.copy()
        self.predict_in_warped_space = False
        likelihood = likelihoods.Gaussian(self.transform_data(),
                                          normalize=normalize_Y)

        GP.__init__(self, X, likelihood, kernel, normalize_X=normalize_X)
        self._set_params(self._get_params())
コード例 #2
0
ファイル: warped_gp.py プロジェクト: OwenThomas/GPy
    def __init__(self, X, Y, kernel=None, warping_function=None, warping_terms=3, normalize_X=False, normalize_Y=False):

        if kernel is None:
            kernel = kern.rbf(X.shape[1])

        if warping_function == None:
            self.warping_function = TanhWarpingFunction_d(warping_terms)
            self.warping_params = (np.random.randn(self.warping_function.n_terms * 3 + 1,) * 1)

        Y = self._scale_data(Y)
        self.has_uncertain_inputs = False
        self.Y_untransformed = Y.copy()
        self.predict_in_warped_space = False
        likelihood = likelihoods.Gaussian(self.transform_data(), normalize=normalize_Y)

        GP.__init__(self, X, likelihood, kernel, normalize_X=normalize_X)
        self._set_params(self._get_params())
コード例 #3
0
ファイル: MOHGP_demo.py プロジェクト: Sura82/colvb
import numpy as np
import pylab as pb
import sys
from GPy import kern
from colvb import MOHGP
np.random.seed(1)
pb.close('all')

#cool structed GP demo
Nclust = 20
Nx = 12
Nobs = [np.random.randint(20,21) for i in range(Nclust)]
X = np.random.rand(Nx,1)*5
X.sort(0)

Kf = kern.rbf(1) + kern.white(1, 1e-6)
S = Kf.K(X)
means = np.vstack([np.tile(np.random.multivariate_normal(np.zeros(Nx),S,1),(N,1)) for N in Nobs]) # GP draws for mean of each cluster

#add GP draw for noise
Ky = kern.rbf(1,0.3,1) +  kern.white(1,0.001)
Y = means + np.random.multivariate_normal(np.zeros(Nx),Ky.K(X),means.shape[0])

#construct model
m = MOHGP(X, Kf.copy(), Ky.copy(), Y, K=Nclust)
m.constrain_positive('')

m.optimize()
m.preferred_optimizer='bfgs'
m.systematic_splits()
m.remove_empty_clusters(1e-3)
コード例 #4
0
ファイル: MOHGP_demo.py プロジェクト: Sura82/colvb
import numpy as np
import pylab as pb
import sys
from GPy import kern
from colvb import MOHGP
np.random.seed(1)
pb.close('all')

#cool structed GP demo
Nclust = 20
Nx = 12
Nobs = [np.random.randint(20, 21) for i in range(Nclust)]
X = np.random.rand(Nx, 1) * 5
X.sort(0)

Kf = kern.rbf(1) + kern.white(1, 1e-6)
S = Kf.K(X)
means = np.vstack([
    np.tile(np.random.multivariate_normal(np.zeros(Nx), S, 1), (N, 1))
    for N in Nobs
])  # GP draws for mean of each cluster

#add GP draw for noise
Ky = kern.rbf(1, 0.3, 1) + kern.white(1, 0.001)
Y = means + np.random.multivariate_normal(np.zeros(Nx), Ky.K(X),
                                          means.shape[0])

#construct model
m = MOHGP(X, Kf.copy(), Ky.copy(), Y, K=Nclust)
m.constrain_positive('')
コード例 #5
0
ground_truth_phi = utilities.blockdiag([np.ones((n,1)) for n in Nobs])
alpha = 2. #(should give approximately 10 clusters)

freqs = 2*np.pi + 0.3*(np.random.rand(Nclust)-.5)
phases = 2*np.pi*np.random.rand(Nclust)
means = np.vstack([np.tile(np.sin(f*X+p).T,(Ni,1)) for f,p,Ni in zip(freqs,phases,Nobs)])

#add a lower freq sin for the noise
freqs = .4*np.pi + 0.01*(np.random.rand(means.shape[0])-.5)
phases = 2*np.pi*np.random.rand(means.shape[0])
offsets = 0.3*np.vstack([np.sin(f*X+p).T for f,p in zip(freqs,phases)])
Y = means + offsets + np.random.randn(*means.shape)*0.05


#construct full model
Kf = kern.rbf(1,0.01,0.001)
Ky1 = kern.rbf(1,0.1,0.001)
Ky2 = kern.white(1,0.01)
Ky = Ky1 + Ky2
m = MOHGP(X,Kf,Ky,Y, K=Nclust, prior_Z = 'DP', alpha=alpha)
m.ensure_default_constraints()
m.checkgrad(verbose=1)

m.randomize()
m.optimize()
m.systematic_splits()
m.systematic_splits()
m.plot(1,1,1,0,0,1)

#construct model without structure
#give it a fighting chance by normalising signals first
コード例 #6
0
ファイル: MOHGP_sine_demo.py プロジェクト: Sura82/colvb
freqs = 2 * np.pi + 0.3 * (np.random.rand(Nclust) - .5)
phases = 2 * np.pi * np.random.rand(Nclust)
means = np.vstack([
    np.tile(np.sin(f * X + p).T, (Ni, 1))
    for f, p, Ni in zip(freqs, phases, Nobs)
])

#add a lower freq sin for the noise
freqs = .4 * np.pi + 0.01 * (np.random.rand(means.shape[0]) - .5)
phases = 2 * np.pi * np.random.rand(means.shape[0])
offsets = 0.3 * np.vstack([np.sin(f * X + p).T for f, p in zip(freqs, phases)])
Y = means + offsets + np.random.randn(*means.shape) * 0.05

#construct full model
Kf = kern.rbf(1, 0.01, 0.001)
Ky1 = kern.rbf(1, 0.1, 0.001)
Ky2 = kern.white(1, 0.01)
Ky = Ky1 + Ky2
m = MOHGP(X, Kf, Ky, Y, K=Nclust, prior_Z='DP', alpha=alpha)
m.ensure_default_constraints()
m.checkgrad(verbose=1)

m.randomize()
m.optimize()
m.systematic_splits()
m.systematic_splits()
m.plot(1, 1, 1, 0, 0, 1)

#construct model without structure
#give it a fighting chance by normalising signals first
コード例 #7
0
    def __init__(self, name=None, hypers=None):
        super(GPChannel, self).__init__(name=name)

        # Specify the number of parameters
        self.D = hypers['D']
        # Channels are expected to have a n_x variable
        self.n_x = self.D

        # Set the hyperparameters
        self.g = hypers['g_gp']
        self.E = hypers['E_gp']
        self.a0 = hypers['alpha_0']
        self.b0 = hypers['beta_0']
        self.sigmas = (self.a0 / self.b0) * np.ones(self.D)

        # Create a GP Object for the dynamics model
        # This is a function Z x V -> dZ, i.e. a 2D
        # Gaussian process regression.
        # Lay out a grid of inducing points for a sparse GP
        self.grid = 10
        self.z_min = sigma_inv(0.001)
        self.z_max = sigma_inv(0.999)
        self.V_min = -80.
        self.V_max = 50.
        length = 5.0
        self.Z = np.array(
            list(
                itertools.product(*([
                    np.linspace(self.z_min, self.z_max, self.grid)
                    for _ in range(self.D)
                ] + [np.linspace(self.V_min, self.V_max, self.grid)]))))

        # Create independent RBF kernels over Z and V
        kernel_z_hyps = {
            'input_dim': 1,
            'variance': hypers['sigma_kernel'],
            'lengthscale': (self.z_max - self.z_min) / length
        }

        self.kernel_z = rbf(**kernel_z_hyps)
        for n in range(1, self.D):
            self.kernel_z = self.kernel_z.prod(rbf(**kernel_z_hyps),
                                               tensor=True)

        kernel_V_hyps = {
            'input_dim': 1,
            'variance': hypers['sigma_kernel'],
            'lengthscale': (self.V_max - self.V_min) / length
        }
        self.kernel_V = rbf(**kernel_V_hyps)

        # Combine the kernel for z and V
        self.kernel = self.kernel_z.prod(self.kernel_V, tensor=True)

        # Initialize with a random sample from the prior
        self.model_dzdt = True
        if self.model_dzdt:
            m = np.zeros(self.Z.shape[0])
        else:
            m = self.Z[:, 0]
        C = self.kernel.K(self.Z)

        self.hs = []
        self.gps = []

        for d in range(self.D):
            # Sample the function value at the inducing points
            self.hs.append(np.random.multivariate_normal(m, C, 1).T)
            # self.hs.append(np.zeros((self.Z.shape[0],1)))

            # Create a sparse GP model with the sampled function h
            # This will be used for prediction
            self.gps.append(
                SparseGPWithVariance(self.Z,
                                     self.hs[d],
                                     self.kernel,
                                     self.sigmas[d],
                                     num_inducing=25))
コード例 #8
0
ファイル: gpchannel.py プロジェクト: HIPS/optofit
    def __init__(self, name=None, hypers=None):
        super(GPChannel, self).__init__(name=name)

        # Specify the number of parameters
        self.D = hypers['D']
        # Channels are expected to have a n_x variable
        self.n_x = self.D

        # Set the hyperparameters
        self.g = hypers['g_gp']
        self.E = hypers['E_gp']
        self.a0 = hypers['alpha_0']
        self.b0 = hypers['beta_0']
        self.sigmas = (self.a0/self.b0) * np.ones(self.D)

        # Create a GP Object for the dynamics model
        # This is a function Z x V -> dZ, i.e. a 2D
        # Gaussian process regression.
        # Lay out a grid of inducing points for a sparse GP
        self.grid = 10
        self.z_min = sigma_inv(0.001)
        self.z_max = sigma_inv(0.999)
        self.V_min = -80.
        self.V_max = 50.
        length = 5.0
        self.Z = np.array(list(
                      itertools.product(*([np.linspace(self.z_min, self.z_max, self.grid) for _ in range(self.D)]
                                        + [np.linspace(self.V_min, self.V_max, self.grid)]))))

        # Create independent RBF kernels over Z and V
        kernel_z_hyps = { 'input_dim' : 1,
                          'variance' : hypers['sigma_kernel'],
                          'lengthscale' : (self.z_max-self.z_min)/length}

        self.kernel_z = rbf(**kernel_z_hyps)
        for n in range(1, self.D):
            self.kernel_z = self.kernel_z.prod(rbf(**kernel_z_hyps), tensor=True)

        kernel_V_hyps = { 'input_dim' : 1,
                          'variance' : hypers['sigma_kernel'],
                          'lengthscale' : (self.V_max-self.V_min)/length}
        self.kernel_V = rbf(**kernel_V_hyps)

        # Combine the kernel for z and V
        self.kernel = self.kernel_z.prod(self.kernel_V, tensor=True)

        # Initialize with a random sample from the prior
        self.model_dzdt = True
        if self.model_dzdt:
            m = np.zeros(self.Z.shape[0])
        else:
            m = self.Z[:,0]
        C = self.kernel.K(self.Z)

        self.hs = []
        self.gps = []

        for d in range(self.D):
            # Sample the function value at the inducing points
            self.hs.append(np.random.multivariate_normal(m, C, 1).T)
            # self.hs.append(np.zeros((self.Z.shape[0],1)))

            # Create a sparse GP model with the sampled function h
            # This will be used for prediction
            self.gps.append(SparseGPWithVariance(self.Z, self.hs[d], self.kernel, self.sigmas[d], num_inducing=25))