The KernelFunction argument accepts custom function. You can create a function for matern kernel, and pass it to KernelFunction argument. However, OptimizeHyperparameters only selects one of the inbuilt Kernel functions that are provided. So it's not possible to provide a custom function to OptimizeHyperparameters.
load carsmall.mat
X = [Horsepower,Weight];
Y = MPG;
% This is how you pass custom KernelFunction
mdl = fitrsvm(X, Y, 'KernelFunction', 'myKernel');
% List out the functions you want to test
kernel = optimizableVariable('kernel', {'myKernel', 'myKernel2'}, 'Type', 'categorical');
% Define the function to optimize. Typically this will be
% the loss function on test dataset. For simplicity, I have passed the training data
fun = @(x)loss(fitrsvm(X, Y, 'KernelFunction', char(x.kernel)), X, Y);
% Run bayesian optimization
results = bayesopt(fun, [kernel]);
myKernel and myKernel2 are defined separately. You can define the required functions.
function G = myKernel(U,V)
[m, ~] = size(U);
[n, ~] = size(V);
G = ones([m,n]);
end
And,
function G = myKernel2(U,V)
[m, ~] = size(U);
[n, ~] = size(V);
G = zeros([m,n]);
end
Note that we define multiple kernel functions as kernel function itself does not take arguments except U and V.
Best Answer