gpflow:非独立多输出内核

2024-09-29 19:26:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在gpflow中实现我自己的多输出内核(MOK), 然而,我被(Kernel, Inducing Variable)组合的多重分派所困扰

根据docs,组合MultioutputKernelInducingPoints的回退方法应该是调用fully_correlated_conditional(通过gpf.conditionals.multioutput.inducing_point_conditional

然而,我不能让任何MOK处理非独立的诱导变量,即使是预实现的变量。 下面是SharedIndependent的一个最低限度不起作用的示例:

######################## toy data
d = 1
X = np.random.normal(0, 10, (100, d))
xx = np.linspace(-10, 10, 200).reshape(-1, 1)
f = lambda x: x ** 2
f_ = lambda x: 2 * x
Y = f(X)
Y_ = f_(X)

Y_combined = np.hstack((Y, Y_))
data = (X, Y_combined)

######################### gpflow stuff
kernel = gpf.kernels.RBF(lengthscales=[1.0] * d)
Z = X.copy()
# create multi-output inducing variables from Z
iv = gpf.inducing_variables.InducingPoints(Z)

MOK_K = gpf.kernels.SharedIndependent(kernel, output_dim=2)

m = gpf.models.SVGP(likelihood=gpf.likelihoods.Gaussian(), kernel=MOK_K, num_latent_gps=2,
                    inducing_variable=iv)

optimizer = gpf.optimizers.Scipy()
optimizer.minimize(
    m.training_loss_closure(data),
    variables=m.trainable_variables,
    method="l-bfgs-b",
    options={"disp": True, "maxiter": 1000},
)

这是不起作用的,除非你用

iv = gpf.inducing_variables.SharedIndependentInducingVariables(
    gpf.inducing_variables.InducingPoints(Z)
)

但是对于我的自定义非独立内核,我需要完全相关的条件。 我得到的错误是

ValueError: base_conditional() arguments [Note that this check verifies the shape of an alternative representation of Kmn. See the docs for the actual expected shape.]

inducing_point_conditional方法中,它似乎试图将内核矩阵“展平”(到2d)到经典的多输出表示,然后再将其传递到base_conditional。然而,我不明白哪里出了问题,因为形状应该是好的。它们与文档中的定义相同

为了让它运行在完全相关的条件下,我需要做什么更改


Tags: the方法docsdatanpvariableskernel内核
1条回答
网友
1楼 · 发布于 2024-09-29 19:26:50

问题是变分分布参数的维数,q_muq_sqrt

必须事先手动定义它们,以便它们分别具有[N*P, 1][1, N*P, N*P]的形状,其中p是输出维度(在我的示例中为2)

请注意,这与文档略有不同,它们说q_mu必须是[1, N*P]

我的问题代码在以下情况下正确运行:

# [200, 1]    
q_mu = tf.zeros([200, 1], dtype=gpf.config.default_float())

# [1, 200, 200]
q_sqrt = tf.eye(200, dtype=gpf.config.default_float())[tf.newaxis, ...]

m = gpf.models.SVGP(likelihood=gpf.likelihoods.Gaussian(), kernel=MOK_K, num_latent_gps=2,
                    inducing_variable=iv, q_mu=q_mu, q_sqrt=q_sqrt)

相关问题 更多 >

    热门问题