def setData(self, x, y, value_per_axis=5): '''Set training data and derive deault inducing_points. :param x: training data :param y: training target :param int value_per_axis: number of value in each dimension when using a uni-distant default inducing points''' if x.ndim == 1: x = np.reshape(x, (x.shape[0], 1)) if y.ndim == 1: y = np.reshape(y, (y.shape[0], 1)) self.x = x self.y = y if self.usingDefaultMean: c = np.mean(y) self.meanfunc = mean.Const( c) # adapt default prior mean wrt. training labels # get range of x in each dimension # 5 uniformally selected value for each dimension gridAxis = [] for d in xrange(x.shape[1]): column = x[:, d] mini = np.min(column) maxi = np.max(column) axis = np.linspace(mini, maxi, value_per_axis) gridAxis.append(axis) # default inducing points-> a grid if self.u == None: self.u = np.array(list(itertools.product(*gridAxis))) self.covfunc = self.covfunc.fitc(self.u)
def setData(self, x, y): ''' Set training inputs and traning labels to model. :param x: training inputs in shape (n,D) :param y: training labels in shape (n,1) Note this method will transform x, y to correct shape if x, y is given in 1d array. ''' # check wether the number of inputs and labels match assert x.shape[0] == y.shape[ 0], "number of inputs and labels does not match" # check the shape of inputs # transform to the correct shape if x.ndim == 1: x = np.reshape(x, (x.shape[0], 1)) if y.ndim == 1: y = np.reshape(y, (y.shape[0], 1)) self.x = x self.y = y if self.usingDefaultMean: c = np.mean(y) self.meanfunc = mean.Const( c) # adapt default prior mean wrt. training labels
def optimize(self, x=None, y=None): ''' Train optimal hyperparameters based on training data, adjust new hyperparameters to all mean/cov/lik functions ''' if x != None: if x.ndim == 1: x = np.reshape(x, (x.shape[0], 1)) self.x = x if y != None: if y.ndim == 1: y = np.reshape(y, (y.shape[0], 1)) self.y = y if self.usingDefaultMean and self.meanfunc == None: c = np.mean(y) self.meanfunc = mean.Const( c) # adapt default prior mean wrt. training labels # optimize optimalHyp, optimalNlZ = self.optimizer.findMin(self.x, self.y) self.nlZ = optimalNlZ # apply optimal hyp to all mean/cov/lik functions here self.optimizer._apply_in_objects(optimalHyp) self.fit()
def setData(self, x, y): self.x = x self.y = y if self.usingDefaultMean: c = np.mean(y) self.meanfunc = mean.Const( c) # adapt default prior mean wrt. training labels
def setData(self, x, y, value_per_axis=5): '''set Data and derive deault inducing_points value_per_axis is number of value in each dimension... ...when using a default inducing point grid''' self.x = x self.y = y if self.usingDefaultMean: c = np.mean(y) self.meanfunc = mean.Const( c) # adapt default prior mean wrt. training labels # get range of x in each dimension # 5 uniformally selected value for each dimension gridAxis = [] for d in xrange(x.shape[1]): column = x[:, d] mini = np.min(column) maxi = np.max(column) axis = np.linspace(mini, maxi, value_per_axis) gridAxis.append(axis) # default inducing points-> a grid if self.u == None: self.u = np.array(list(itertools.product(*gridAxis))) self.covfunc = self.covfunc.fitc(self.u)
def __init__(self, *data, **kwargs): super(GPR, self).__init__(*data, **kwargs) self.mean = kwargs.get('mean', mean.Const(self.samples.y.mean())) self.cov = kwargs.get('cov', cov.RBF()) self.lik = kwargs.get('lik', lik.Gauss()) self.inf = kwargs.get('inf', inf.Exact()) self.optimizer = kwargs.get('optimizer', opt.Minimize)(self)
def setData(self, x, y): '''Pass training data and traning labels to model.''' if x.ndim == 1: x = np.reshape(x, (x.shape[0], 1)) if y.ndim == 1: y = np.reshape(y, (y.shape[0], 1)) self.x = x self.y = y if self.usingDefaultMean: c = np.mean(y) self.meanfunc = mean.Const( c) # adapt default prior mean wrt. training labels
def fit(self, x=None, y=None, der=True): ''' Fit the training data. Update negative log marginal likelihood(nlZ), partial derivatives of nlZ w.r.t. each hyperparameter(dnlZ), and struct representation of the (approximate) posterior(post), which consists of post.alpha, post.L, post.sW. ''' if x != None: if x.ndim == 1: x = np.reshape(x, (x.shape[0], 1)) self.x = x if y != None: if y.ndim == 1: y = np.reshape(y, (y.shape[0], 1)) self.y = y if self.usingDefaultMean and self.meanfunc == None: c = np.mean(y) self.meanfunc = mean.Const( c) # adapt default prior mean wrt. training labels # call inference method if isinstance(self.likfunc, lik.Erf): #or is instance(self.likfunc, lik.Logistic): uy = unique(self.y) ind = (uy != 1) if any(uy[ind] != -1): raise Exception( 'You attempt classification using labels different from {+1,-1}' ) if not der: post, nlZ = self.inffunc.evaluate(self.meanfunc, self.covfunc, self.likfunc, self.x, self.y, self.ScalePrior, 2) self.nlZ = nlZ self.posterior = deepcopy(post) return nlZ, post else: if self.ScalePrior: post, nlZ, dnlZ, dscale = self.inffunc.evaluate( self.meanfunc, self.covfunc, self.likfunc, self.x, self.y, self.ScalePrior, 3) else: post, nlZ, dnlZ = self.inffunc.evaluate( self.meanfunc, self.covfunc, self.likfunc, self.x, self.y, self.ScalePrior, 3) dscale = None self.nlZ = nlZ self.dnlZ = deepcopy(dnlZ) self.dscale = dscale self.posterior = deepcopy(post) return nlZ, dnlZ, post
def setData(self, x, y, value_per_axis=5): ''' Set training inputs and traning labels to model and derive deault inducing_points.. :param x: training inputs in shape (n,D) :param y: training labels in shape (n,1) :param int value_per_axis: number of value in each dimension when using a uni-distant default inducing points Note this method will transform x, y to correct shape if x, y is given in 1d array. ''' # check wether the number of inputs and labels match assert x.shape[0] == y.shape[ 0], "number of inputs and labels does not match" # check dimension of inputs # transform to correct shape if neccessary if x.ndim == 1: x = np.reshape(x, (x.shape[0], 1)) if y.ndim == 1: y = np.reshape(y, (y.shape[0], 1)) self.x = x self.y = y if self.usingDefaultMean: c = np.mean(y) self.meanfunc = mean.Const( c) # adapt default prior mean wrt. training labels # get range of x in each dimension # 5 uniformally selected value for each dimension gridAxis = [] for d in xrange(x.shape[1]): column = x[:, d] mini = np.min(column) maxi = np.max(column) axis = np.linspace(mini, maxi, value_per_axis) gridAxis.append(axis) # default inducing points-> a grid if self.u is None: self.u = np.array(list(itertools.product(*gridAxis))) self.covfunc = self.covfunc.fitc(self.u)
def optimize(self, x=None, y=None, numIterations=40): ''' Train optimal hyperparameters based on training data, adjust new hyperparameters to all mean/cov/lik functions. :param x: training inputs in shape (n,D) :param y: training labels in shape (n,1) ''' # check wether the number of inputs and labels match if x is not None and y is not None: assert x.shape[0] == y.shape[ 0], "number of inputs and labels does not match" # check the shape of inputs # transform to the correct shape if not x is None: if x.ndim == 1: x = np.reshape(x, (x.shape[0], 1)) self.x = x if not y is None: if y.ndim == 1: y = np.reshape(y, (y.shape[0], 1)) self.y = y if self.usingDefaultMean and self.meanfunc is None: c = np.mean(y) self.meanfunc = mean.Const( c) # adapt default prior mean wrt. training labels # optimize optimalHyp, optimalNlZ = self.optimizer.findMin(self.x, self.y, numIters=numIterations) self.nlZ = optimalNlZ # apply optimal hyp to all mean/cov/lik functions here self.optimizer._apply_in_objects(optimalHyp) self.getPosterior()
def getPosterior(self, x=None, y=None, der=True): ''' Fit the training data. Update negative log marginal likelihood(nlZ), partial derivatives of nlZ w.r.t. each hyperparameter(dnlZ), and struct representation of the (approximate) posterior(post), which consists of post.alpha, post.L, post.sW. nlZ, dnlZ, post = getPosterior(x, y, der=True)\n nlZ, post = getPosterior(x, y, der=False ) :param x: training inputs in shape (n,D) :param y: training labels in shape (n,1) :param boolean der: flag for whether to compute derivatives :return: negative log marginal likelihood (nlZ), derivatives of nlZ (dnlZ), posterior structure(post) You can print post to see descriptions of posterior. or see pyGPs.Core.inf for details. ''' # check wether the number of inputs and labels match if x is not None and y is not None: assert x.shape[0] == y.shape[ 0], "number of inputs and labels does not match" # check the shape of inputs # transform to the correct shape if not x is None: if x.ndim == 1: x = np.reshape(x, (x.shape[0], 1)) self.x = x if not y is None: if y.ndim == 1: y = np.reshape(y, (y.shape[0], 1)) self.y = y if self.usingDefaultMean and self.meanfunc is None: c = np.mean(y) self.meanfunc = mean.Const( c) # adapt default prior mean wrt. training labels # call inference method if isinstance(self.likfunc, lik.Erf): #or is instance(self.likfunc, lik.Logistic): uy = unique(self.y) ind = (uy != 1) if any(uy[ind] != -1): raise Exception( 'You attempt classification using labels different from {+1,-1}' ) if not der: post, nlZ = self.inffunc.evaluate(self.meanfunc, self.covfunc, self.likfunc, self.x, self.y, 2) self.nlZ = nlZ self.posterior = deepcopy(post) return nlZ, post else: post, nlZ, dnlZ = self.inffunc.evaluate(self.meanfunc, self.covfunc, self.likfunc, self.x, self.y, 3) self.nlZ = nlZ self.dnlZ = deepcopy(dnlZ) self.posterior = deepcopy(post) return nlZ, dnlZ, post