首页 文章

Kernlab kraziness:相同问题的结果不一致

提问于
浏览
4

我在kernlab包中发现了一些令人费解的行为:估计数学上相同的SVM会在软件中产生不同的结果 .

为简单起见,此代码段仅获取虹膜数据并使其成为二进制分类问题 . 如您所见,我在两个SVM中都使用线性内核 .

library(kernlab)
library(e1071)

data(iris)
x <- as.matrix(iris[, 1:4])
y <- as.factor(ifelse(iris[, 5] == 'versicolor', 1, -1))
C <- 5.278031643091578

svm1 <- ksvm(x = x, y = y, scaled = FALSE, kernel = 'vanilladot', C = C)

K <- kernelMatrix(vanilladot(), x)
svm2 <- ksvm(x = K, y = y, C = C, kernel = 'matrix')

svm3 <- svm(x = x, y = y, scale = FALSE, kernel = 'linear', cost = C)

但是,svm1和svm2的摘要信息不同:kernlab报告两个模型之间完全不同的支持向量计数,训练错误率和目标函数值 .

> svm1
Support Vector Machine object of class "ksvm" 

SV type: C-svc  (classification) 
 parameter : cost C = 5.27803164309158 

Linear (vanilla) kernel function. 

Number of Support Vectors : 89 

Objective Function Value : -445.7911 
Training error : 0.26 
> svm2
Support Vector Machine object of class "ksvm" 

SV type: C-svc  (classification) 
 parameter : cost C = 5.27803164309158 

[1] " Kernel matrix used as input."

Number of Support Vectors : 59 

Objective Function Value : -292.692 
Training error : 0.333333

为了便于比较,我还使用e1071计算了相同的模型,e1071为libsvm软件包提供了一个R接口 .

svm3

Call:
svm.default(x = x, y = y, scale = FALSE, kernel = "linear", cost = C)


Parameters:
   SVM-Type:  C-classification 
 SVM-Kernel:  linear 
       cost:  5.278032 
      gamma:  0.25 

Number of Support Vectors:  89

It reports 89 support vectors, the same as svm1.

我的问题是kernlab包中是否存在任何可以解释这种异常行为的已知错误 .

(Kernlab for R是一个SVM求解器,允许用户使用几个预先打包的内核函数之一,或用户提供的内核矩阵 . 输出是用户提供的超参数的支持向量机估计 . )

1 回答

  • 2

    回顾一些代码,看来这是违规行:

    https://github.com/cran/kernlab/blob/efd7d91521b439a993efb49cf8e71b57fae5fc5a/src/svm.cpp#L4205

    也就是说,在用户提供的内核矩阵的情况下, ksvm 只是看两个维度,而不是输入的维度 . 这看起来很奇怪,可能是一些测试或其他任何东西的延续 . 使用仅两维数据的线性内核的测试产生相同的结果:在上面用 1:2 替换 1:4 并且输出和预测都是一致的 .

相关问题