优化Scikit中自定义高斯过程核的网格搜索

我在使用高斯过程时,遇到使用scikit-learn的GP模块创建和优化自定义核的问题,特别是在使用gridsearchcv时。最能描述这个问题的例子是使用经典的Mauna Loa例子,在这个例子中,合适的核是通过组合已定义的核如RBFRationalQuadratic来构建的。在这个例子中,自定义核的参数没有被优化,而是被视为给定的。如果我想运行一个更一般的案例,通过交叉验证来估计这些超参数,该怎么做呢?我应该如何构建自定义核,然后构建相应的param_grid对象进行网格搜索呢?

以一种非常简单的方式,我可以像这样构建一个自定义核:

def custom_kernel(a,ls,l,alpha,nl):    kernel = a*RBF(length_scale=ls) \    + b*RationalQuadratic(length_scale=l,alpha=alpha) \    + WhiteKernel(noise_level=nl)    return kernel

然而,这个函数当然不能通过gridsearchcv调用,例如GaussianProcessRegressor(kernel=custom_kernel(a,ls,l,alpha,nl))

一种可能的解决方案在这个StackOverflow问题中被提出,但是我想知道是否有更简单的方法解决这个问题,而不是从头开始编写核(连同其超参数),因为我希望使用标准核的组合,并且还有可能想要混合使用它们。


回答:

所以这就是我目前的进展。这回答了问题,但对于Mauna Loa的例子来说,速度非常慢,不过这可能是一个难处理的数据集:

from sklearn.gaussian_process import GaussianProcessRegressorfrom sklearn.model_selection import GridSearchCVfrom sklearn.gaussian_process.kernels import ConstantKernel,RBF,WhiteKernel,RationalQuadratic,ExpSineSquaredimport numpy as npfrom sklearn.datasets import fetch_openml# from https://scikit-learn.org/stable/auto_examples/gaussian_process/plot_gpr_co2.htmldef load_mauna_loa_atmospheric_co2():    ml_data = fetch_openml(data_id=41187)    months = []    ppmv_sums = []    counts = []    y = ml_data.data[:, 0]    m = ml_data.data[:, 1]    month_float = y + (m - 1) / 12    ppmvs = ml_data.target    for month, ppmv in zip(month_float, ppmvs):        if not months or month != months[-1]:            months.append(month)            ppmv_sums.append(ppmv)            counts.append(1)        else:            # aggregate monthly sum to produce average            ppmv_sums[-1] += ppmv            counts[-1] += 1    months = np.asarray(months).reshape(-1, 1)    avg_ppmvs = np.asarray(ppmv_sums) / counts    return months, avg_ppmvsX, y = load_mauna_loa_atmospheric_co2()# Kernel with parameters given in GPML bookk1 = ConstantKernel(constant_value=66.0**2) * RBF(length_scale=67.0)  # long term smooth rising trendk2 = ConstantKernel(constant_value=2.4**2) * RBF(length_scale=90.0) \    * ExpSineSquared(length_scale=1.3, periodicity=1.0)  # seasonal component# medium term irregularityk3 = ConstantKernel(constant_value=0.66**2) \    * RationalQuadratic(length_scale=1.2, alpha=0.78)k4 = ConstantKernel(constant_value=0.18**2) * RBF(length_scale=0.134) \    + WhiteKernel(noise_level=0.19**2)  # noise termskernel_gpml = k1 + k2 + k3 + k4gp = GaussianProcessRegressor(kernel=kernel_gpml)# print parametersprint(gp.get_params())param_grid = {'alpha': np.logspace(-2, 4, 5),              'kernel__k1__k1__k1__k1__constant_value': np.logspace(-2, 4, 5),              'kernel__k1__k1__k1__k2__length_scale': np.logspace(-2, 2, 5),              'kernel__k2__k2__noise_level':np.logspace(-2, 1, 5)              }grid_gp = GridSearchCV(gp,cv=5,param_grid=param_grid,n_jobs=4)grid_gp.fit(X, y)

对我有帮助的是首先将模型初始化为gp = GaussianProcessRegressor(kernel=kernel_gpml),然后使用get_params属性来获取模型超参数的列表。

最后,我注意到在他们的中,Rasmussen和Williams似乎使用了留一法交叉验证来调整超参数。

Related Posts

多维度Top-k评分

例如,在机器学习中的自然语言处理中,通常使用波束搜索来…

R – Caret train() “错误:停止” 与 “在newdata中未找到对象中使用的所有变量名”

我正在尝试为蘑菇数据构建一个简单的朴素贝叶斯分类器。我…

创建训练和测试数据集分割,数据嵌套在多个文件夹中

我正在准备数据以训练一个图像识别模型。目前我有一个文件…

我的神经网络预测出现错误:IndexError: list index out of range

我正在进行一个简单的垃圾邮件/非垃圾邮件文本分类。我的…

python 给出数组是一维的,但索引了两个维度错误

我已经为 miniBatchGradientDesce…

TensorFlow自定义训练步骤使用不同的损失函数

背景 根据TensorFlow文档,可以使用以下方式执…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注