You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义X轴范围限制的RBF核在高斯过程回归(GPR)拟合时的梯度维度错误问题求助

自定义X轴范围限制的RBF核在高斯过程回归(GPR)拟合时的梯度维度错误问题求助

我最近想要实现一个仅在X轴特定区间内生效的RBF核,于是写了一个继承自Kernel的测试类来验证代码,但在运行高斯过程回归拟合的时候遇到了梯度维度相关的错误。如果直接使用标准的RBF核,代码完全正常;要是关闭优化器(设置optimizer=None),代码能跑但预测误差特别大,所以还是希望能解决这个梯度的问题。

自定义核的实现代码

class RangeLimitedRBFTest(Kernel):
    def __init__(self, length_scale=1.0, length_scale_bounds=(1e-5, 1e5), x_min = 0., x_max = 1.):
        self.length_scale = length_scale
        self.length_scale_bounds = length_scale_bounds
        self.rbf_kernel = RBF(length_scale, length_scale_bounds)
        self.x_min = x_min
        self.x_max = x_max

    def __call__(self, X, Y=None, eval_gradient=False):
        if eval_gradient and Y is not None:
            raise ValueError("Gradient can only be evaluated when Y is None.")
        
        X = np.atleast_2d(X)
        if Y is not None:
            Y = np.atleast_2d(Y)

        print(f"X shape: {X.shape}")
        if Y is not None:
            print(f"Y shape: {Y.shape}")
        else:
            print("Y shape: None")

        K_rbf = self.rbf_kernel(X, Y, eval_gradient=eval_gradient)

        if eval_gradient:
            K, K_grad = K_rbf
            print(f"Kernel matrix shape (K): {K.shape}")
            print(f"Kernel gradient matrix shape (K_grad): {K_grad.shape}")
            return K, K_grad
        else:
            K = K_rbf
            return K

    def diag(self, X):
        return self.rbf_kernel.diag(X)

    def is_stationary(self):
        return self.rbf_kernel.is_stationary()

拟合代码

kernel = 1.0 * RangeLimitedRBFTest(length_scale=0.1, length_scale_bounds=(8e-2, 8e-1), x_min=0., x_max=2.5) + WhiteKernel(noise_level=0.5, noise_level_bounds=(1e-2, 1e1))
gaussian_process = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=1, alpha=1e-5, optimizer='fmin_l_bfgs_b')
gaussian_process.optimizer_kwargs = {"max_iter": 10000} 
gaussian_process.fit(X, T_PMT)

运行时的报错信息

X shape: (6248, 1)
Y shape: None
Kernel matrix shape (K): (6248, 6248)
Kernel gradient matrix shape (K_grad): (6248, 6248, 1)
ValueError: 0-th dimension must be fixed to 2 but got 3


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/tdaq/cremonini/pt100_probe/read_temperatures.py", line 97, in <module>
    gaussian_process.fit(X, T_PMT)
  File "/home/tdaq/.local/lib/python3.10/site-packages/sklearn/base.py", line 1389, in wrapper
    return fit_method(estimator, *args, **kwargs)
  File "/home/tdaq/.local/lib/python3.10/site-packages/sklearn/gaussian_process/_gpr.py", line 308, in fit
    self._constrained_optimization(
  File "/home/tdaq/.local/lib/python3.10/site-packages/sklearn/gaussian_process/_gpr.py", line 653, in _constrained_optimization
    opt_res = scipy.optimize.minimize(
  File "/cvmfs/atlas.cern.ch/repo/sw/software/0.3/StatAnalysis/0.3.1/InstallArea/x86_64-el9-gcc13-opt/lib/python3.10/site-packages/scipy/optimize/_minimize.py", line 713, in minimize
    res = _minimize_lbfgsb(fun, x0, args, jac, bounds,
  File "/cvmfs/atlas.cern.ch/repo/sw/software/0.3/StatAnalysis/0.3.1/InstallArea/x86_64-el9-gcc13-opt/lib/python3.10/site-packages/scipy/optimize/_lbfgsb_py.py", line 360, in _minimize_lbfgsb
    _lbfgsb.setulb(m, x, low_bnd, upper_bnd, nbd, f, g, factr,
ValueError: failed in converting 7th argument `g' of _lbfgsb.setulb to C/Fortran array

有没有大佬能帮我看看这个梯度维度的问题出在哪呀?我看输出里K_grad的形状是(6248,6248,1),是不是这个维度不符合GPR优化器的要求?

备注:内容来源于stack exchange,提问作者crema997

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 17:48:15