[R] Re: Writing new kernel option in svm E1071

Chih-Jen Lin lincj at ccms.ntu.edu.tw
Wed Jul 10 02:10:10 CEST 2002


Hi, Adai:

This is how to add/modify a kernel in libsvm 
(from its faq):

   o I would like to use my own kernel but find out that there are
     two subtourines for kernel evaluations: k_function() and
     kernel_function(). Which one should I modify ?
     The reason why we have two functions is as follows: For the
     RBF kernel exp(-g |xi - xj|^2), if we calculate xi - xj first
     and then the norm square, there are 3n operations. Thus we
     consider exp(-g (|xi|^2 - 2dot(xi,xj) +|xj|^2)) and by
     calculating all |xi|^2 in the beginning, the number of
     operations is reduced to 2n. This is for the training. For
     prediction we cannot do this so a regular subrouting using
     that 3n operations is needed. The easiest way to have your
     own kernel is to put the same code in these two subourines by
     replacing any kernel.

Yes, due to performance reason, you don't want to do kernel
evaluations at the R level. 

About computed kernels, that's a complicated issue. 
We have been thinking about topics like on-line or reinforcement
learning where several data are added/removed. Hence kernel
elements should be reused. However, we must carefully write the
code so it's clean. Hence I am sorry 
this won't be available very soon.

Chih-Jen

David Meyer writes:
 > Dear Adai,
 > 
 > unfortunately, you currently can't extend the kernel set at the R level
 > (and due to performance issues this probably will never be possible).
 > 
 > (It's quite simple at the C-level, though).
 > 
 > But I think an option for providing the computed kernel values would be
 > a useful feature easy to add; I cc: this mail to Chih-Jen Lin, the
 > author of LIBSVM, perhaps he agrees.
 > 
 > g.,
 > -d
 > 
 > 
 > 
 > > Adaikalavan Ramasamy wrote:
 > > 
 > > I am trying to use the svm function in E1071 package. It does come
 > > with built in kernel functions like radial, linear etc. I wish to use
 > > the radial basis function but with a constant added to the diagonal of
 > > the kernel (or Gram) matrix. This was the suggested method in a paper
 > > UCSC-CRL-99-09 Brown et al.
 > > 
 > > So I wrote the following code, [which works correctly if I test on two
 > > vectors manually]
 > > 
 > > kernel.new.fn <- function(u,v)
 > > {   exp(-gamma*det(t(u-v)%*%(u-v), method="qr")) +
 > > ifelse(identical(u, v), 0.1 ,0)    }
 > > 
 > > But when I try to run the code, I get the following error:
 > > 
 > > > test.svm  <- svm( X ,  Ysvm, kernel="kernel.new.fn")
 > > Error in svm.default(X.complete, Ysvm[, 3], kernel = "kernel.new.fn")
 > > :
 > >  wrong kernel specification!
 > > 
 > > I am guessing the problem being svm.default does not recognise
 > > "kernel.new.fn".
 > > svm.default calls a C file "svmtrain" to do the calculations.
 > > 
 > > Q1: Is there an exisiting option to add a constant to the diagonal of
 > > the kernel ?
 > 
 > no.
 > 
 > > Q2: Is there a way I can get svm.default to recognise there is new
 > > option called kernel.new.fn externally without meddling with C
 > > file which I am not so familiar with.
 > 
 > no.
 > 
 > >        [OR equivalently is there a way of feeding in the
 > > computed kernel instead - I can do this manually using a loop]
 > 
 > again, no.
 > 
 > > 
 > > This is as much due to my lack of programming as it is to my lack of
 > > experience with svm.
 > > 
 > > Many thanks in advance, Adai.
 > > 
 > > 
 > > 
 > > 
 > > 
 > > 
 > 
 > -- 
 > 	Mag. David Meyer		Wiedner Hauptstrasse 8-10
 > Vienna University of Technology		A-1040 Vienna/AUSTRIA
 >          Department of			Tel.: (+431) 58801/10772
 > Statistics and Probability Theory	mail: david.meyer at ci.tuwien.ac.at
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list