How to use svmtrain() with a custom kernel in Matlab?

svmtain() is a function in MATLAB for SVM learning. The help doc is here:
How can I use it with a custom kernel? In the help doc, it says:
------------------------------------------------------------------------------------
@kfun — Function handle to a kernel function. A kernel function must be of the form
function K = kfun(U, V)
The returned value, K, is a matrix of size M-by-N, where U and V have M and N rows respectively. ------------------------------------------------------------------------------------
It mentions nothing about what U and V are and what M and N mean. I just don't know how to use it in the right format. Can anyone tell me what U and V are and what M and N mean? For example, the training data are 5-dimensional vectors and the kernel function is the sum of the length of the vectors. How can I write the kernel function?
Thank you!

Respuestas (1)

Ilya
Ilya el 22 de Dic. de 2012
By convention adopted for svmtrain, observations are in rows and predictors are in columns. The same convention would hold for kfun. This means U is of size M-by-P, and V is of size N-by-P, where P is the number of predictors (P=5 for you). Other functions such as pdist2 in the Statistics Tlbx follow the same convention. If you want your kernel function to be a simple dot product, you would do
kfun = @(U,V) U*V';

5 comentarios

Tom
Tom el 22 de Dic. de 2012
Thank you very much :-) I am a beginner. Can you explain more detailed? If P is the number of dimensions here, what do M and N mean? And what are U and V? From my understanding, a kernel function replaces the inner product of the data vectors. So U and V should be two data vectors, which should be of size 1-by-# of dimension. Why do they have M and N rows?
Ilya
Ilya el 23 de Dic. de 2012
Editada: Ilya el 23 de Dic. de 2012
U is M vectors, each of size 1-by-P, concatenated vertically, and V is, similarly, N such vectors. You could compute dot products between M and N observations by defining a function of two vectors and writing a nested loop. You would go over 1:M indices in the outer loop and over 1:N indices in the inner loop, and compute M*N dot products of two vectors. This could be slow. svmtrain tries to speed up by vectorizing this code. That's why svmtrain wants you to write a vectorized function capable of computing all dot products between M and N observations at once.
I have the same issue. I could not even understand how to call the function. I have a code
xdata=meas(1:end,1:5);
group = species(1:end);
svmStruct = svmtrain(xdata,group,'kernel_function' ,'polynomial', 'ShowPlot',0)
result = svmclassify(svmStruct,newdata,'ShowPlot',0)
which runs perfectly. As far as I understand, I should replace 'polynomial' with @kfun as
svmStruct = svmtrain(xdata,group,'kernel_function' ,@kfun, 'ShowPlot',0)
However it does not run. I thought, the purpose is lack of the inputs. I tried this time
svmStruct = svmtrain(xdata,group,'kernel_function' ,@kfun(U,V), 'ShowPlot',0)
but it gives an error again. What is my mistake? Is it about the function? Or do I miss anything else?
Defne Ozan
Defne Ozan el 31 de Mzo. de 2021
For anyone else having similar problems, writing the kernel function in a separate file (instead of at the bottom of the same file) and then calling it with 'KernelFunction','kernel' worked for me.
jyoti lele
jyoti lele el 22 de Jul. de 2021
can you please give the code of 'kernel' you wrote

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

Tom
el 22 de Dic. de 2012

Comentada:

el 22 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by