def __init__(self, X, Y, kern, Z=None, Zy=None,Zvar = None,mean_function=None, minibatch_size=None, var = 1.0, shuffle=True, trainable_var=True,**kwargs): """ X is a data matrix, size N x D Y is a data matrix, size N x R kern, mean_function are appropriate GPflow objects minibatch_size, if not None, turns on mini-batching with that size. vector_obs_variance if not None (default) is vectorized measurement variance """ Z = DataHolder(Z) if (Z is not None) and (Zy is not None) and (Zvar is not None) else None Zy = DataHolder(Zy) if (Z is not None) and (Zy is not None) and (Zvar is not None) else None Zvar = DataHolder(Zvar) if (Z is not None) and (Zy is not None) and (Zvar is not None) else None if minibatch_size is None: X = DataHolder(X) Y = DataHolder(Y) Y_var = DataHolder(var) else: X = Minibatch(X, batch_size=minibatch_size, shuffle=shuffle, seed=0) Y = Minibatch(Y, batch_size=minibatch_size, shuffle=shuffle, seed=0) Y_var = Minibatch(var, batch_size=minibatch_size, shuffle=shuffle, seed=0) likelihood = Gaussian_v2(var=1.0,trainable=trainable_var) likelihood.relative_variance = Y_var GPModel.__init__(self, X, Y, kern, likelihood, mean_function, **kwargs) self.Z = Z self.Zy = Zy self.Zvar = Zvar
def __init__(self, data: RegressionData, kernel: Kernel, mu_old: Optional[tf.Tensor], Su_old: Optional[tf.Tensor], Kaa_old: Optional[tf.Tensor], Z_old: Optional[tf.Tensor], inducing_variable: Union[InducingPoints, np.ndarray], mean_function=Zero()): """ Z is a matrix of pseudo inputs, size M x D kern, mean_function are appropriate gpflow objects mu_old, Su_old are mean and covariance of old q(u) Z_old is the old inducing inputs This method only works with a Gaussian likelihood. """ X, Y = data self.X = X self.Y = Y likelihood = Gaussian() self.inducing_variable = gpflow.models.util.inducingpoint_wrapper(inducing_variable) GPModel.__init__(self, kernel, likelihood, mean_function, inducing_variable.size) self.num_data = X.shape[0] self.num_latent = Y.shape[1] self.mu_old = gpflow.Parameter(mu_old, trainable=False) self.M_old = Z_old.shape[0] self.Su_old = gpflow.Parameter(Su_old, trainable=False) self.Kaa_old = gpflow.Parameter(Kaa_old, trainable=False) self.Z_old = gpflow.Parameter(Z_old, trainable=False)
def __init__(self, X, Y, W, kern, feat=None, mean_function=None, Z=None, **kwargs): """ X is a data matrix, size N x D Y is a data matrix, size N x R Z is a matrix of pseudo inputs, size M x D kern, mean_function are appropriate GPflow objects This method only works with a Gaussian likelihood. """ X = DataHolder(X) Y = DataHolder(Y) likelihood = likelihoods.Gaussian() GPModel.__init__(self, X, Y, kern, likelihood, mean_function, **kwargs) self.feature = features.inducingpoint_wrapper(feat, Z) self.num_data = X.shape[0] self.W_prior = tf.ones(W.shape, dtype=settings.float_type) / W.shape[1] self.W = Parameter(W) self.num_inducing = Z.shape[0] * W.shape[1]
def plot_boundary(m: GPModel, X: ndarray, y: ndarray, ax=None): x_grid = np.linspace(min(X[:, 0]), max(X[:, 0]), 40) y_grid = np.linspace(min(X[:, 1]), max(X[:, 1]), 40) xx, yy = np.meshgrid(x_grid, y_grid) Xplot = np.vstack((xx.flatten(), yy.flatten())).T mask = y[:, 0] == 1 p, _ = m.predict_y(Xplot) # here we only care about the mean if ax is None: fig, ax = plt.subplots(figsize=(10, 5)) # plt.figure(figsize=(7, 7)) ax.plot(X[mask, 0], X[mask, 1], "oC0", mew=0, alpha=0.5, label="1") ax.plot(X[np.logical_not(mask), 0], X[np.logical_not(mask), 1], "oC1", mew=0, alpha=0.5, label="0") _ = ax.contour( xx, yy, p.numpy().reshape(*xx.shape), [0.5], # plot the p=0.5 contour line only colors="k", linewidths=1.8, zorder=100, ) ax.legend(loc='best') ax.axis("off")
def __init__(self, X, Y, W1, W1_index, W2, W2_index, kern, feat=None, mean_function=None, Z=None, **kwargs): """ X is a data matrix, size N x D Y is a data matrix, size N x R Z is a matrix of pseudo inputs, size M x D W1, size NxK W1_index PxL W2, size NxL W2_index PxL kern, mean_function are appropriate GPflow objects This method only works with a Gaussian likelihood. """ X = DataHolder(X) Y = DataHolder(Y, fix_shape=True) likelihood = likelihoods.Gaussian() GPModel.__init__(self, X, Y, kern, likelihood, mean_function, **kwargs) self.feature = features.inducingpoint_wrapper(feat, Z) self.num_data = X.shape[0] self.W1_prior = Parameter(np.log( np.ones(W1.shape[1], dtype=settings.float_type) / W1.shape[1]), trainable=False) self.W1 = Parameter(W1) self.W1_index = DataHolder(W1_index, dtype=np.int32, fix_shape=True) self.K = W1.shape[1] self.W2_prior = Parameter(np.log( np.ones(W2.shape[1], dtype=settings.float_type) / W2.shape[1]), trainable=False) self.W2 = Parameter(W2) self.W2_index = DataHolder(W2_index, dtype=np.int32, fix_shape=True) self.L = W2.shape[1] self.num_inducing = Z.shape[0]
def __init__(self, X, Y, W, kern, idx=None, feat=None, Z=None, mean_function=None, q_diag=False, whiten=False, q_mu=None, q_sqrt=None, minibatch_size=None, num_latent=None, **kwargs): """ X is a data matrix, size N x D Y is a data matrix, size N x R Z is a matrix of pseudo inputs, size M x D kern, mean_function are appropriate GPflow objects This method only works with a Gaussian likelihood. """ num_data = X.shape[0] if minibatch_size is None: X = DataHolder(X, fix_shape=True) Y = DataHolder(Y, fix_shape=True) else: X = Minibatch(X, batch_size=minibatch_size, seed=0) Y = Minibatch(Y, batch_size=minibatch_size, seed=0) # init the super class likelihood = likelihoods.Gaussian() num_latent = W.shape[1] GPModel.__init__(self, X, Y, kern, likelihood, mean_function, num_latent=num_latent, **kwargs) if minibatch_size is not None: idx = Minibatch(np.arange(num_data), batch_size=minibatch_size, seed=0, dtype=np.int32) self.idx = idx self.W = Parameter(W, trainable=False) self.K = self.W.shape[1] self.W_prior = Parameter(np.ones(self.K) / self.K, trainable=False) self.num_data = num_data self.feature = features.inducingpoint_wrapper(feat, Z) self.minibatch_size = minibatch_size self.q_diag, self.whiten = q_diag, whiten # init variational parameters num_inducing = len(self.feature) self._init_variational_parameters( num_inducing, q_mu, q_sqrt, q_diag)
def __init__(self, X, Y, W1, W2, kern, likelihood, idx=None, W1_idx=None, W2_idx=None, feat=None, mean_function=None, num_latent=None, q_diag=False, whiten=True, minibatch_size=None, Z=None, num_data=None, q_mu=None, q_sqrt=None, **kwargs): """ - X is a data matrix, size N x D - Y is a data matrix, size N x P - kern, likelihood, mean_function are appropriate GPflow objects - Z is a matrix of pseudo inputs, size M x D - num_latent is the number of latent process to use, default to Y.shape[1] - q_diag is a boolean. If True, the covariance is approximated by a diagonal matrix. - whiten is a boolean. If True, we use the whitened representation of the inducing points. - minibatch_size, if not None, turns on mini-batching with that size. - num_data is the total number of observations, default to X.shape[0] (relevant when feeding in external minibatches) """ # sort out the X, Y into MiniBatch objects if required. num_data = X.shape[0] if minibatch_size is None: X = DataHolder(X) Y = DataHolder(Y) if W1_idx is not None: W1_idx = DataHolder(W1_idx, fix_shape=True) if W2_idx is not None: W2_idx = DataHolder(W2_idx, fix_shape=True) else: X = Minibatch(X, batch_size=minibatch_size, seed=0) Y = Minibatch(Y, batch_size=minibatch_size, seed=0) idx = Minibatch(np.arange(num_data), batch_size=minibatch_size, seed=0, dtype=np.int32) if W1_idx is not None: W1_idx = Minibatch( W1_idx, batch_size=minibatch_size, seed=0, dtype=np.int32) if W2_idx is not None: W2_idx = Minibatch( W2_idx, batch_size=minibatch_size, seed=0, dtype=np.int32) # init the super class, accept args num_latent = W1.shape[1] * W2.shape[1] GPModel.__init__(self, X, Y, kern, likelihood, mean_function, num_latent, **kwargs) self.num_data = num_data or X.shape[0] self.q_diag, self.whiten = q_diag, whiten self.feature = features.inducingpoint_wrapper(feat, Z) self.idx = idx self.W1_idx = W1_idx self.W2_idx = W2_idx self.K1 = W1.shape[1] self.W1 = Parameter(W1, trainable=False, dtype=settings.float_type) self.W1_prior = Parameter(np.ones(self.K1) / self.K1, trainable=False) self.K2 = W2.shape[1] self.W2 = Parameter(W2, trainable=False, dtype=settings.float_type) self.W2_prior = Parameter(np.ones(self.K2) / self.K2, trainable=False) # init variational parameters num_inducing = len(self.feature) self._init_variational_parameters(num_inducing, q_mu, q_sqrt, q_diag)
def plot_gp_plotly(model: GPModel, mins: TensorType, maxs: TensorType, grid_density=20) -> go.Figure: """ Plots 2-dimensional plot of a GP model's predictions with mean and 2 standard deviations. :param model: a gpflow model :param mins: list of 2 lower bounds :param maxs: list of 2 upper bounds :param grid_density: integer (grid size) :return: a plotly figure """ mins = to_numpy(mins) maxs = to_numpy(maxs) # Create a regular grid on the parameter space Xplot, xx, yy = create_grid(mins=mins, maxs=maxs, grid_density=grid_density) # Evaluate objective function Fmean, Fvar = model.predict_f(Xplot) n_output = Fmean.shape[1] fig = make_subplots( rows=1, cols=n_output, specs=[np.repeat({ "type": "surface" }, n_output).tolist()]) for k in range(n_output): fmean = Fmean[:, k].numpy() fvar = Fvar[:, k].numpy() lcb = fmean - 2 * np.sqrt(fvar) ucb = fmean + 2 * np.sqrt(fvar) fig = add_surface_plotly(xx, yy, fmean, fig, alpha=1.0, figrow=1, figcol=k + 1) fig = add_surface_plotly(xx, yy, lcb, fig, alpha=0.5, figrow=1, figcol=k + 1) fig = add_surface_plotly(xx, yy, ucb, fig, alpha=0.5, figrow=1, figcol=k + 1) return fig