Prepare models to make sure the coregionalized model with diagonal coregion kernel and with fixed lengthscales is equivalent with normal GP regression.
()
| 57 | |
| 58 | |
| 59 | def _prepare_models() -> Tuple[VGP, VGP, VGP]: |
| 60 | """ |
| 61 | Prepare models to make sure the coregionalized model with diagonal coregion kernel and |
| 62 | with fixed lengthscales is equivalent with normal GP regression. |
| 63 | """ |
| 64 | # 1. Two independent VGPs for two sets of data |
| 65 | k0 = gpflow.kernels.SquaredExponential() |
| 66 | set_trainable(k0.lengthscales, False) |
| 67 | k1 = gpflow.kernels.SquaredExponential() |
| 68 | set_trainable(k1.lengthscales, False) |
| 69 | vgp0 = VGP( |
| 70 | (Datum.X[0], Datum.Y[0]), |
| 71 | kernel=k0, |
| 72 | mean_function=Constant(), |
| 73 | likelihood=gpflow.likelihoods.Gaussian(), |
| 74 | num_latent_gps=1, |
| 75 | ) |
| 76 | vgp1 = VGP( |
| 77 | (Datum.X[1], Datum.Y[1]), |
| 78 | kernel=k1, |
| 79 | mean_function=Constant(), |
| 80 | likelihood=gpflow.likelihoods.Gaussian(), |
| 81 | num_latent_gps=1, |
| 82 | ) |
| 83 | # 2. Coregionalized VGP |
| 84 | kc = gpflow.kernels.SquaredExponential(active_dims=[0, 1]) |
| 85 | set_trainable(kc.lengthscales, False) |
| 86 | set_trainable(kc.variance, False) # variance is handled by the Coregion kernel |
| 87 | coreg = gpflow.kernels.Coregion(output_dim=2, rank=1, active_dims=[2]) |
| 88 | coreg.W.assign(np.zeros((2, 1))) # zero correlation between outputs |
| 89 | set_trainable(coreg.W, False) |
| 90 | lik = gpflow.likelihoods.SwitchedLikelihood( |
| 91 | [gpflow.likelihoods.Gaussian(), gpflow.likelihoods.Gaussian()] |
| 92 | ) |
| 93 | mean_c = gpflow.mean_functions.SwitchedMeanFunction( |
| 94 | [gpflow.mean_functions.Constant(), gpflow.mean_functions.Constant()] |
| 95 | ) |
| 96 | cvgp = VGP( |
| 97 | (Datum.X_augmented, Datum.Y_augmented), |
| 98 | kernel=kc * coreg, |
| 99 | mean_function=mean_c, |
| 100 | likelihood=lik, |
| 101 | num_latent_gps=1, |
| 102 | ) |
| 103 | |
| 104 | # Train them for a small number of iterations |
| 105 | |
| 106 | opt = gpflow.optimizers.Scipy() |
| 107 | opt.minimize( |
| 108 | vgp0.training_loss, |
| 109 | variables=vgp0.trainable_variables, |
| 110 | options=dict(maxiter=1000), |
| 111 | method="BFGS", |
| 112 | ) |
| 113 | opt.minimize( |
| 114 | vgp1.training_loss, |
| 115 | variables=vgp1.trainable_variables, |
| 116 | options=dict(maxiter=1000), |
no test coverage detected
searching dependent graphs…