frankiethull / maize

tidymodels extension package; binding specialty kernels & support vector machines to {parsnip} and {recipes}
Other
7 stars 1 forks source link

a collection of esoteric kernels in python: cossim, cauchy, wavelet, etc. #5

Closed frankiethull closed 1 month ago

frankiethull commented 2 months ago

I am stumbling across some other unique kernels on the interweb.

a project called pykernel with a bunch of kernels to check out. Python repo kernel code: https://github.com/gmum/pykernels/blob/master/pykernels/regular.py """ Collection of regular kernel functions, which are rarely the part of any ML library """

Could be worth trying to replicate a few in R as kernlab kernels then bind to parsnip.

frankiethull commented 2 months ago

interestingly enough, mimicking the kernlab kernels code (https://github.com/cran/kernlab/blob/master/R/kernels.R), does not work. I tried a few different ways with setClass().

What does work is super simple.

Take this example from pykernels:

class Cossim(Kernel):
    """
    Cosine similarity kernel, 

        K(x, y) = <x, y> / (||x|| ||y||)

    """

    def _compute(self, data_1, data_2):
        self._dim = data_1.shape[1]
        norm_1 = np.sqrt((data_1 ** 2).sum(axis=1)).reshape(data_1.shape[0], 1)
        norm_2 = np.sqrt((data_2 ** 2).sum(axis=1)).reshape(data_2.shape[0], 1)
        return data_1.dot(data_2.T) / (norm_1 * norm_2.T)

    def dim(self):
        return self._dim

I simplified the steps and got the kernel working like so:

cossimdot <- function(x, y){
  return(crossprod(x, y) / sqrt(crossprod(x) * crossprod(y)))  
}
class(cossimdot) <- "kernel"
fit <- kernlab::ksvm(type ~ ., data = maize::corn_data, kernel = cossimdot, C = 10)
frankiethull commented 2 months ago