当我尝试使用 LinearCoregionalization 内核计算 GPR 模型的对数边际似然时,发生了 InvalidArgumentError,尽管可以在将内核插入 SVGP 模型时计算 ELBO。
据我(我不得不说的一个新手)可以看到,这是因为 GPR 模型不检查模型是多输出还是单输出,而只是以相同的方式对待它们。如何在不将多输出模型重塑为单输出模型的情况下方便地计算它?提前致谢。
import gpflow as gpf
import numpy as np
L = 2
P = 5
N = 100
t = np.linspace(0, 1, N)[:, np.newaxis]
noise_variance = 0.01
latent_kernels = [gpf.kernels.SquaredExponential(lengthscales = 0.5), gpf.kernels.Matern52(lengthscales = 0.01)]
x = np.vstack([np.random.multivariate_normal(np.zeros((N,)), kernel.K(t)) for kernel in latent_kernels]).T
W = np.random.uniform(size = (P, L))
y = x@W.T+np.random.normal(0, noise_variance, size = (N, P))
coregion_kernel = gpf.kernels.LinearCoregionalization(kernels = latent_kernels, W = W)
model = gpf.models.GPR((t, y), coregion_kernel, noise_variance = noise_variance)
model.log_marginal_likelihood()
错误信息
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-4-df8cbb66c4ca> in <module>
13 coregion_kernel = gpf.kernels.LinearCoregionalization(kernels = latent_kernels, W = W)
14 model = gpf.models.GPR((t, y), coregion_kernel, noise_variance = noise_variance)
---> 15 model.log_marginal_likelihood()
~/anaconda3/envs/gpflow/lib/python3.7/site-packages/gpflow/models/gpr.py in log_marginal_likelihood(self)
68 k_diag = tf.linalg.diag_part(K)
69 s_diag = tf.fill([num_data], self.likelihood.variance)
---> 70 ks = tf.linalg.set_diag(K, k_diag + s_diag)
71 L = tf.linalg.cholesky(ks)
72 m = self.mean_function(X)
~/anaconda3/envs/gpflow/lib/python3.7/site-packages/tensorflow_core/python/ops/math_ops.py in binary_op_wrapper(x, y)
900 with ops.name_scope(None, op_name, [x, y]) as name:
901 if isinstance(x, ops.Tensor) and isinstance(y, ops.Tensor):
--> 902 return func(x, y, name=name)
903 elif not isinstance(y, sparse_tensor.SparseTensor):
904 try:
~/anaconda3/envs/gpflow/lib/python3.7/site-packages/tensorflow_core/python/ops/math_ops.py in _add_dispatch(x, y, name)
1192 return gen_math_ops.add(x, y, name=name)
1193 else:
-> 1194 return gen_math_ops.add_v2(x, y, name=name)
1195
1196
~/anaconda3/envs/gpflow/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_math_ops.py in add_v2(x, y, name)
478 pass # Add nodes to the TensorFlow graph.
479 except _core._NotOkStatusException as e:
--> 480 _ops.raise_from_not_ok_status(e, name)
481 # Add nodes to the TensorFlow graph.
482 _, _, _op, _outputs = _op_def_library._apply_op_helper(
~/anaconda3/envs/gpflow/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py in raise_from_not_ok_status(e, name)
6604 message = e.message + (" name: " + name if name is not None else "")
6605 # pylint: disable=protected-access
-> 6606 six.raise_from(core._status_to_exception(e.code, message), None)
6607 # pylint: enable=protected-access
6608
~/anaconda3/envs/gpflow/lib/python3.7/site-packages/six.py in raise_from(value, from_value)
InvalidArgumentError: Incompatible shapes: [100,5] vs. [100] [Op:AddV2] name: add/