Effacer les filtres
Effacer les filtres

Create your own kernel function for SVR

7 vues (au cours des 30 derniers jours)
Steffen Funk
Steffen Funk le 6 Jan 2021
Commenté : zhaorong huang le 13 Août 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?

Réponses (1)

Soniya Jain
Soniya Jain le 23 Déc 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 commentaire
zhaorong huang
zhaorong huang le 13 Août 2023
I understand what you're saying, but is there a more detailed instance of calling code?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by