如何使用 scikit-learn GaussianProcessRegressor 重现 GPy GPRegression 的结果?

How to reproduce results of GPy GPRegression using scikit-learn GaussianProcessRegressor?

GPRegression (GPy) 和 GaussianProcessRegressor (scikit-learn) 都使用相似的初始值和相同的优化器 (lbfgs)。为什么结果差异很大?

#!pip -qq install pods
#!pip -qq install GPy
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
from sklearn.preprocessing import StandardScaler
import pods
data = pods.datasets.olympic_marathon_men()
X = StandardScaler().fit_transform(data['X'])
y = data['Y']
# scikit-learn
model = GaussianProcessRegressor(C()*RBF(), n_restarts_optimizer=20, random_state=0)
model.fit(X, y)
print(model.kernel_)

# GPy
from GPy.models import GPRegression
from GPy.kern import RBF as GPyRBF
model = GPRegression(X, y, GPyRBF(1))
model.optimize_restarts(20, verbose=0)
print(model.kern)

结果

2.89**2 * RBF(length_scale=0.173)
  rbf.         |               value  |  constraints  |  priors
  variance     |  25.399509298957504  |      +ve      |        
  lengthscale  |   4.279767394389103  |      +ve      |        

使用 GPy RBF() 内核等同于使用 scikit-learn ConstantKernel()*RBF() + WhiteKernel()。因为 GPy 库在内部添加了似然噪声。使用这个我能够在两者中获得可比较的结果。

您可以通过将噪声显式设置为 0 来尝试使用 Gpy 的无噪声版本的 GP,您将获得与 skelarnGpy 相同的超参数调整结果:

# scikit-learn
model = GaussianProcessRegressor(C()*RBF(), n_restarts_optimizer=20, random_state=0) # don't add noise
model.fit(X, y)
print(model.kernel_)
# 2.89**2 * RBF(length_scale=0.173)

# Gpy
model = GPRegression(X, y, GPyRBF(1))
model['.*Gaussian_noise'] = 0 # make noise zero
model['.*noise'].fix()
model.optimize_restarts(20, verbose=0)
print(model.kern)
#  rbf.         |               value  |  constraints  |  priors
#  variance     |   8.343280650322102  |      +ve      |        
#  lengthscale  |  0.1731764533721659  |      +ve      |        

RBF 方差的最佳值 = 2.89**2 = 8.3521 和长度尺度超参数具有大致相同的值,从上面可以看出。

或使用显式白噪声内核 scikit-learn:

# scikit-learn
from sklearn.gaussian_process.kernels import WhiteKernel as W
model = GaussianProcessRegressor(C()*RBF()+W(), n_restarts_optimizer=20, random_state=0) 
model.fit(X, y)
print(model.kernel_)
# 5.04**2 * RBF(length_scale=4.28) + WhiteKernel(noise_level=0.0485)

# GPy
model = GPRegression(X, y, GPyRBF(1))
model.optimize_restarts(20, verbose=0)
print(model.kern)
#  rbf.         |               value  |  constraints  |  priors
#  variance     |    25.3995066661936  |      +ve      |        
#  lengthscale  |  4.2797670212128756  |      +ve      |  

RBF 方差的最佳值 = 5.04**2 = 25.4016 和长度尺度超参数具有大致相同的值,从上面可以看出。