如何在gpflow中修复内核长度尺度的某些尺寸? [英] How to fix some dimensions of a kernel lengthscale in gpflow?

查看:80
本文介绍了如何在gpflow中修复内核长度尺度的某些尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2d内核,

k = gpflow.kernels.RBF(lengthscales=[24*5,1e-5])
m = gpflow.models.GPR(data=(X,Y), kernel=k, mean_function=None)

我想将长度比例尺固定在第二维,然后优化另一个.

and I want to fix the lengthscale in the 2nd dimension, and just optimise the other.

我可以使用禁用所有长度比例优化,

I can disable all lengthscale optimisation using,

gpflow.set_trainable(m.kernel.lengthscales, False) 

但是我不能仅将一个维度传递给该方法.

but I can't pass just one dimension to this method.

在GPy中,我们将调用 m.kern.lengthscale [1:].fixed()之类的东西.

In GPy we would call m.kern.lengthscale[1:].fixed() or something.

也许我可以使用转换来大致实现这一目标(例如,此处),但这很复杂.

Maybe I could use a transform to roughly achieve this (e.g. here), but that's quite complicated.

推荐答案

GPflow为每个参数(例如内核的 lengthscales )和TensorFlow使用单个 tf.Variable 仅允许您整体更改变量的 trainable 状态.对于任意维度,每个维度具有单独的参数并不容易实现,但是您可以轻松地对所需的内核进行子类化,并使用如下属性覆盖 lengthscales :

GPflow uses a single tf.Variable for each parameter - such as a kernel's lengthscales - and TensorFlow only allows you to change the trainable status of a Variable as a whole. Having a separate parameter per dimension would not be easy to implement for arbitrary dimensions, but you can easily subclass the kernel you want and override lengthscales with a property as follows:

import gpflow
import tensorflow as tf

class MyKernel(gpflow.kernels.SquaredExponential):  # or whichever kernel you want
    @property
    def lengthscales(self) -> tf.Tensor:
        return tf.stack([self.lengthscale_0, self.lengthscale_1])

    @lengthscales.setter
    def lengthscales(self, value):
        self.lengthscale_0 = gpflow.Parameter(value[0], transform=gpflow.utilities.positive())
        self.lengthscale_1 = value[1]  # fixed

然后,您可以简单地使用 k = MyKernel(lengthscales = [24 * 5,1e-5]).(尽管1e-5的长度比例看起来不正确!但这超出了此问题的范围.)

Then you can simply use k = MyKernel(lengthscales=[24*5, 1e-5]). (Though a lengthscale of 1e-5 doesn't look right! But that's outside the scope of this question.)

之所以可行,是因为超类的 __ init __ (在 gpflow.kernels.Stationary 中)分配了 self.lengthscales = Parameter(lengthscales,transform = positive()),因此在此自定义类中,它改为调用属性设置器,后者又创建了两个单独的属性.然后,属性获取器将它们重新缝合在一起,以获得实际需要二维向量的方法.

This works because the superclass's __init__ (in gpflow.kernels.Stationary) assigns self.lengthscales = Parameter(lengthscales, transform=positive()), so in this custom class this instead calls the property setter which in turn creates the two separate attributes. The property getter then stitches them back together for the methods that actually expect the two-dimensional vector.

这篇关于如何在gpflow中修复内核长度尺度的某些尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆