getting error in fitrgp with customized kernal function

조회 수: 7 (최근 30일)
ARPITA MONDAL
ARPITA MONDAL 2021년 7월 2일
편집: sanidhyak 2025년 3월 3일
x=[2.4994,3.1476,5.5532; 2.5086, 3.3894, 5.6874; 2.5315, 3.4528, 5.6087; 2.5383, 3.5921, 5.8071; 2.5638, 3.7242, 5.6367];
y=[0.9251; 0.9205; 0.9151; 0.9151; 0.9093];
n=5;
with this x and output y I have generated one customized kernel function
kfcn = @(sf,sn,l) (((0.5*y'*inv(((sf.^2)*exp(-(cov(x'))/(2*l.^2)+(sn.^2*eye(n,n)))))*y)+(0.5*log(det((sf.^2)*exp(-(cov(x'))/(2*l^2)+(sn.^2*eye(n,n))))))+((n/2)*log(2*pi))));
But whenever i am using the generated customized kernel function in the fitrgp function I am getting error.
I have three hyperparameters(sf,sn,l) of the kernel which I want to optimize using negative log marginal likelihood and for that reason I am using fminsearch.
sf0 = 0.1; sn0=0.1; l0=1;
gprMdl = fitrgp(x,y,'KernelFunction', kfcn, 'Verbose', 1, 'Optimizer','fminsearch','KernelParameters',[sf0,sn0,l0]);
Every matrix dimension I have given right but still I am getting this error that matrix dimension is mismatch
Error using /
Matrix dimensions must agree
Error in
trial>@(sf,sn,l)(((0.5*y'*inv(((sf.^2)*exp(-(cov(x'))/(2*l.^2)+(sn.^2*eye(n,n)))))*y)+(0.5*log(det((sf.^2)*exp(-(cov(x'))/(2*l^2)+(sn.^2*eye(n,n))))))+((n/2)*log(2*pi))))
Error in classreg.learning.gputils.CustomKernel/makeKernelAsFunctionOfTheta/f (line 138)
KNM = customFcn(XN,XM,theta);
Error in classreg.learning.impl.GPImpl/makeNegativeProfiledLogLikelihoodExact/f1 (line 922)
[V,DK] = kfcn(theta);
Error in fminsearch (line 189)
fv(:,1) = funfcn(x,varargin{:});
Error in classreg.learning.impl.GPImpl/doMinimization (line 2886)
[phiHat,fHat,exitFlag] = fminsearch(objFun,phi0,opts);
Error in classreg.learning.impl.GPImpl/estimateThetaHatSigmaHatExact (line 808)
[phiHat,nloglikHat,cause] = doMinimization(this,objFun,phi0,haveGrad);
Error in classreg.learning.impl.GPImpl/doFitMethodExact (line 382)
[this.ThetaHat,this.SigmaHat,this.LogLikelihoodHat] =
estimateThetaHatSigmaHatExact(this,this.X,this.y,this.Beta0,this.Theta0,this.Sigma0);
Error in classreg.learning.impl.GPImpl/doFit (line 322)
this = doFitMethodExact(this);
Error in classreg.learning.impl.GPImpl.make (line 226)
this = doFit(this);
Error in RegressionGP (line 277)
this.Impl = classreg.learning.impl.GPImpl.make(...
Error in classreg.learning.FitTemplate/fit (line 251)
obj = this.MakeFitObject(X,Y,W,this.ModelParams,fitArgs{:});
Error in RegressionGP.fit (line 303)
this = fit(temp,X,Y);
Error in fitrgp (line 380)
obj = RegressionGP.fit(X,Y,varargin{:})
Error in trial (line 14)
gprMdl = fitrgp(x,y,'KernelFunction', kfcn, 'Verbose', 1, 'Optimizer',...
kindly help

답변 (1개)

sanidhyak
sanidhyak 2025년 2월 18일
편집: sanidhyak 2025년 3월 3일
I understand that you are facing a matrix dimension mismatch error while using a customized kernel function with MATLAB's “fitrgp” function.
When reviewing your code, I noticed that the issue arises because the fitrgp function expects the custom kernel function to accept two input matrices (X1 and X2) and a parameter vector theta. The current implementation does not follow this structure, causing the dimension mismatch.
Kindly refer to the following corrected code:
function K = customKernel(X1, X2, theta)
sf = theta(1);
sn = theta(2);
l = theta(3);
D = pdist2(X1, X2, 'euclidean').^2;
K = sf^2 * exp(-D / (2 * l^2));
if isequal(X1, X2)
K = K + sn^2 * eye(size(X1, 1));
end
end
Save this function as customKernel.m.
Then, use the following code in your main script:
x = [2.4994, 3.1476, 5.5532;
2.5086, 3.3894, 5.6874;
2.5315, 3.4528, 5.6087;
2.5383, 3.5921, 5.8071;
2.5638, 3.7242, 5.6367];
y = [0.9251; 0.9205; 0.9151; 0.9151; 0.9093];
initialTheta = [0.1, 0.1, 1];
gprMdl = fitrgp(x, y, ...
'KernelFunction', @(X1, X2, theta) customKernel(X1, X2, theta), ...
'KernelParameters', initialTheta, ...
'Verbose', 1, ...
'Optimizer', 'fminsearch');
This code ensures that your kernel function receives the correct inputs and returns a properly dimensioned covariance matrix, resolving the dimension mismatch error.
For further reference on the “fitrgp” function, kindly refer to the below documentation:
I hope this helps!

카테고리

Help CenterFile Exchange에서 Gaussian Process Regression에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by