Clear Filters
Clear Filters

Create your own kernel function for SVR

7 views (last 30 days)
Steffen Funk
Steffen Funk on 6 Jan 2021
Commented: zhaorong huang on 13 Aug 2023
Hi everyone,
I am trying to write my own kernel function for support vector regression in Matlab.
I have an N x 3 observations matrix X for which I want to calculate the kernel matrix.
I have now interpreted it in such a way that in my case: x_i = x_j = X. Is that correct?
The Linear Kernel Function Seems to agree with the result of the preinstalled Kernle Function 'linear' (commented part).
The gauss kernel differs slightly from the preinstalled 'gaussian'. What could be the reason for this?
function G = mykernel(x_i,x_j)
%G=x_i*x_j'; % Linear kernel
D = pdist2(x_i,x_j,'squaredeuclidean'); % Gaussian kernel
G=exp(-D);
end
Maybe someone can give me an answer?

Answers (1)

Soniya Jain
Soniya Jain on 23 Dec 2022
In the case of the linear kernel, it is correct that x_i and x_j are equal to X. The linear kernel function simply calculates the dot product between two input vectors, which is equivalent to multiplying the transpose of one vector by the other.
For the Gaussian kernel, the calculation of the distance between x_i and x_j using the pdist2 function is correct. However, the exponentiation of the squared Euclidean distance is typically divided by a bandwidth parameter (sigma^2) before being negated. This parameter controls the smoothness of the kernel and can be adjusted to optimize the performance of the support vector regression model.
Without this division, the Gaussian kernel produced by your function may not behave as expected. You may want to try dividing D by sigma^2 and see if it produces results that are more similar to the built-in 'gaussian' kernel.
It's also worth noting that the pdist2 function calculates the pairwise squared Euclidean distance between all rows of x_i and x_j, and returns a matrix of these distances. In order to obtain the kernel matrix, you will need to apply the exponentiation and negation to each element in this distance matrix.
  1 Comment
zhaorong huang
zhaorong huang on 13 Aug 2023
I understand what you're saying, but is there a more detailed instance of calling code?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!