1

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      |        
4

2 回答 2

0

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

于 2020-11-05T07:45:43.473 回答
0

您可以通过将噪声显式设置为 0 来尝试 GP 的无噪声版本,您将获得与和Gpy相同的超参数调整结果: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      |        

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

或使用显式白噪声内核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      |  

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

于 2021-12-15T07:45:07.213 回答