Is it possible to construct gaussian processes custom kernels with built in functions?

I understand the way to construct custom kernels for gaussian processes, e.g.
kfcn = @(XN,XM,theta) exp(theta(1))^2*exp(-pdist2(XN,XM).^2/(2*exp(theta(2))^2))
I am wondering if it is possible to construct kernels from built in function names, e.g.
kfcn = 'exponential' + 'squaredexponential';
If not, does it mean we have to do it from scatch?
Any suggesion would be appreciated.

 채택된 답변

You can use str2func to convert a function name or text expression into a function handle.
kfcns = {'exponential','squaredexponential'};
KFCN = str2func("@(x) " + strjoin(string(kfcns(:)) + "(x)", " + "))
KFCN = function_handle with value:
@(x)exponential(x)+squaredexponential(x)

댓글 수: 5

Thank you for the reply. Just did some tests.
Codes in similar shape works:
gpm1 = fitrgp(X_, y_, 'KernelFunction', 'squaredexponential');
but following with the same initial setup
kfcn = str2func("@(x) squaredexponential(x)");
gpm2 = fitrgp(X_, y_, 'KernelFunction', kfcn);
would get ouput:
So I assume, it does not support function handles from built-in function name.
I see what you mean now.
The function handle needs to accept three parameters.
But No, the options that can be named probably cannot be invoked as functions.
The following talks about the mathematics of some of those https://www.mathworks.com/help/stats/kernel-covariance-function-options.html
Thanks for the information. Wish Matlab can add this functionality as well as kernel function operators etc. in the future release.
The actual functions called are several layers down and obviouslyly not intended to be directly invoked by the user. For example toolbox/stats/classreg/+classreg/+learning/+coder/+gputils/squaredExponentialKfun.m
python scikit learn actually supports it. I feel it is quite convince as it is quite time consusing if constructing all the custom kernels from the scatch.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

질문:

2023년 5월 3일

편집:

2023년 9월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by