我在内核包中发现了一些令人费解的行为:估计在数学上相同的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的总结信息有很大的不同:核实验室的报告完全不同,支持向量计数、训练错误率和目标函数值在两种模型之间完全不同。
> 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计算了相同的模型,它为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.
我的问题是,内核包中是否有任何已知的bug可以解释这种不寻常的行为。
(Kernlab for R是一个支持向量机求解器,它允许使用几个预先打包的内核函数中的一个,或者用户提供的内核矩阵。输出是用户提供的超参数的支持向量机的估计。)
发布于 2015-12-01 18:00:27
回顾其中的一些代码,这似乎是一条冒犯的行:
https://github.com/cran/kernlab/blob/efd7d91521b439a993efb49cf8e71b57fae5fc5a/src/svm.cpp#L4205
也就是说,在用户提供的内核矩阵的情况下,ksvm
只是查看两个维度,而不是输入的维度。这似乎很奇怪,而且很可能是一些测试或其他测试的结果。对只有二维数据的线性核进行测试,得到相同的结果:在上面用1:4
代替1:2
,输出和预测都是一致的。
https://stackoverflow.com/questions/33813972
复制相似问题