Sklearn:使用预训练的超参数高斯过程回归
Sklearn: Using pretrained hyperparameters Gaussian Process Regression
在我的数据上安装 GPR 需要几个小时,因此,我想重用我的预训练 GausianProcessRegressor
我想我找到了一个解决方法,它似乎产生了相同的结果,但我想知道是否有更好的解决方案,因为这有点 hack。
kernel = ConstantKernel(0.25, (1e-3, 1e3)) * RBF(hyper_params_rbf, (1e-3, 1e4)) + WhiteKernel(0.0002, (1e-23, 1e3))
gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=30)
#normalize the data
train = False
if train:
print('Fitting')
gp.fit(X, y)
else:
gp.kernel_= kernel
gp.X_train_ = X
gp.y_train_ = y
gp._y_train_mean = np.zeros(1) #unuse, as Y is not normalized in Regressor
# Precompute quantities required for predictions which are independent of actual query points
K = gp.kernel_(gp.X_train_)
K[np.diag_indices_from(K)] += gp.alpha
gp.L_ = cholesky(K, lower=True)
gp.alpha_ = cho_solve((gp.L_, True), gp.y_train_)
y_pred, sigma = gp.predict(x, return_std=True)
您应该使用 pickle
或 joblib
库序列化您的 GaussianProcessRegressor
模型。
from sklearn.externals import joblib
if train:
print('Fitting')
gp.fit(X, y)
joblib.dump(gp, 'filename.pkl')
else:
gp = joblib.load('filename.pkl')
查看 scikit-learn 的帮助 here
在我的数据上安装 GPR 需要几个小时,因此,我想重用我的预训练 GausianProcessRegressor
我想我找到了一个解决方法,它似乎产生了相同的结果,但我想知道是否有更好的解决方案,因为这有点 hack。
kernel = ConstantKernel(0.25, (1e-3, 1e3)) * RBF(hyper_params_rbf, (1e-3, 1e4)) + WhiteKernel(0.0002, (1e-23, 1e3))
gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=30)
#normalize the data
train = False
if train:
print('Fitting')
gp.fit(X, y)
else:
gp.kernel_= kernel
gp.X_train_ = X
gp.y_train_ = y
gp._y_train_mean = np.zeros(1) #unuse, as Y is not normalized in Regressor
# Precompute quantities required for predictions which are independent of actual query points
K = gp.kernel_(gp.X_train_)
K[np.diag_indices_from(K)] += gp.alpha
gp.L_ = cholesky(K, lower=True)
gp.alpha_ = cho_solve((gp.L_, True), gp.y_train_)
y_pred, sigma = gp.predict(x, return_std=True)
您应该使用 pickle
或 joblib
库序列化您的 GaussianProcessRegressor
模型。
from sklearn.externals import joblib
if train:
print('Fitting')
gp.fit(X, y)
joblib.dump(gp, 'filename.pkl')
else:
gp = joblib.load('filename.pkl')
查看 scikit-learn 的帮助 here