svm 超参数调整:使用 e1071 tune.control 进行随机搜索。 random != NULL 在外部函数调用中给出 NA/NaN/Inf (arg 10)
Posted
技术标签:
【中文标题】svm 超参数调整:使用 e1071 tune.control 进行随机搜索。 random != NULL 在外部函数调用中给出 NA/NaN/Inf (arg 10)【英文标题】:svm hyperparameter tuning: random search with e1071 tune.control. random != NULL gives NA/NaN/Inf in foreign function call (arg 10) 【发布时间】:2019-10-10 10:49:06 【问题描述】:我正在尝试使用 e1071 进行一些简单的(随机搜索)超参数调整。我知道如何使用 mlr 来完成这项任务,但我只想使用 e1071。
我能够执行网格搜索以进行超参数调整(这是我使用 iris 数据集放在一起的一个随机示例,在许多地方默认给出)
library(e1071)
iris_data <- iris
iris_data <- iris_data[,-5]
#NO TUNE CONTROL
svm_model <- tune(svm , Petal.Width ~ . , data = iris_data, kernel = "radial" , type = "eps-regression",
ranges = list(gamma = c(0.1, 0.001), cost = c(1,10)))
#TUNE CONTROL WITH NORMAL STUFF
tune.ctrl1 <- tune.control(cross = 5, best.model = TRUE,
performances = TRUE, error.fun = NULL)
svm_model1 <- tune(svm , Petal.Width ~ . , data = iris_data, kernel = "radial" , type = "eps-regression",
ranges = list(gamma = c(0.1, 0.001), cost = c(1,10)), tunecontrol = tune.ctrl1 )
这两个简单的案例有效。但是,我想使用随机搜索而不是网格搜索。我想在 tune.control() https://rdrr.io/cran/e1071/man/tune.control.html 中使用 Random 参数
我试过下面这两个例子,
#TUNE CONTROL WITH RANDOM, trial 1
tune.ctrl2 <- tune.control(random = 1)
svm_model2 <- tune(svm , Petal.Width ~ . , data = iris_data, kernel = "radial" , type = "eps-regression",
ranges = list(gamma = c(0.1, 0.001), cost = c(1,10)), tunecontrol = tune.ctrl2 )
#TUNE CONTROL WITH RANDOM, trial 1
tune.ctrl3 <- tune.control(random=1, cross = 5, best.model = TRUE,
performances = TRUE, error.fun = NULL)
svm_model3 <- tune(svm , Petal.Width ~ . , data = iris_data, kernel = "radial" , type = "eps-regression",
ranges = list(gamma = c(0.1, 0.001), cost = c(1,10)), tunecontrol = tune.ctrl3 )
但我不断收到此错误:
Error in svm.default(x, y, scale = scale, ..., na.action = na.action) :
NA/NaN/Inf in foreign function call (arg 10)
如果我执行 traceback() 我看到参数(伽马和成本)作为 NA_real_ 传递:我做错了什么?我应该如何使用 random=?
5: svm.default(x, y, scale = scale, ..., na.action = na.action)
4: svm.formula(Petal.Width ~ ., data = list(Sepal.Length = c(5.1,
4.9, 4.7, 4.6, 5, 5.4, 4.6, 5, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3,
5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5, 5,
5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5, 5.5, 4.9, 4.4, 5.1,
5, 4.5, 4.4, 5, 5.1, 4.8, 5.1, 4.6, 5.3, 5, 7, 6.4, 6.9, 5.5,
6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5, 5.9, 6, 6.1, 5.6, 6.7, 5.6,
5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7, 6, 5.7,
5.5, 5.5, 5.8, 6, 5.4, 6, 6.7, 6.3, 5.6, 5.5, 5.5, 6.1, 5.8,
5, 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3, 6.5, 7.6,
4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5, 7.7, 7.7,
6, 6.9, 5.6, 7.7, 6.3, 6.7, 7.2, 6.2, 6.1, 6.4, 7.2, 7.4, 7.9,
6.4, 6.3, 6.1, 7.7, 6.3, 6.4, 6, 6.9, 6.7, 6.9, 5.8, 6.8, 6.7,
6.7, 6.3, 6.5, 6.2, 5.9), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6,
3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.4, 3, 3, 4, 4.4, 3.9, 3.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3, 2.5, 2.3, 1.9, 2, 2.3, 1.8
)), subset = c(19L, 48L, 123L, 90L, 9L, 72L, 78L, 17L, 120L,
47L, 143L, 92L, 52L, 109L, 34L, 134L, 4L, 39L, 66L, 64L, 140L,
142L, 3L, 137L, 36L, 51L, 49L, 139L, 82L, 43L, 2L, 63L, 95L,
99L, 117L, 119L, 103L, 61L, 10L, 45L, 110L, 7L, 55L, 74L, 146L,
29L, 60L, 71L, 56L, 37L, 73L, 23L, 107L, 87L, 54L, 125L, 118L,
132L, 144L, 127L, 108L, 141L, 41L, 148L, 22L, 147L, 104L, 83L,
111L, 101L, 91L, 24L, 150L, 40L, 76L, 113L, 1L, 67L, 12L, 18L,
59L, 13L, 122L, 100L, 68L, 124L, 26L, 21L, 131L, 149L, 94L, 16L,
57L, 80L, 65L, 112L, 44L, 81L, 46L, 98L, 50L, 102L, 27L, 79L,
35L, 28L, 136L, 69L, 96L, 31L, 97L, 88L, 129L, 62L, 116L, 6L,
30L, 32L, 145L, 70L), gamma = NA_real_, cost = NA_real_, kernel = "radial",
type = "eps-regression")
3: (function (x, ...)
UseMethod("svm"))(Petal.Width ~ ., data = list(Sepal.Length = c(5.1,
4.9, 4.7, 4.6, 5, 5.4, 4.6, 5, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3,
5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5, 5,
5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5, 5.5, 4.9, 4.4, 5.1,
5, 4.5, 4.4, 5, 5.1, 4.8, 5.1, 4.6, 5.3, 5, 7, 6.4, 6.9, 5.5,
6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5, 5.9, 6, 6.1, 5.6, 6.7, 5.6,
5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7, 6, 5.7,
5.5, 5.5, 5.8, 6, 5.4, 6, 6.7, 6.3, 5.6, 5.5, 5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, 1.3, 1.4, 1.5, 1, 1.5, 1.1, 1.8,
1.3, 1.5, 1.2, 1.3, 1.4, 1.4, 1.7, 1.5, 1, 1.1, 1, 1.2, 1.6,
1.5, 1.6, 1.5, 1.3, 1.3, 1.3, 1.2, 1.4, 1.2, 1, 1.3, 1.2, 1.3,
1.3, 1.1, 1.3, 2.5, 1.9, 2.1, 1.8, 2.2, 2.1, 1.7, 1.8, 1.8, 2.5,
2, 1.9, 2.1, 2, 2.4, 2.3, 1.8, 2.2, 2.3, 1.5, 2.3, 2, 2, 1.8,
2.1, 1.8, 1.8, 1.8, 2.1, 1.6, 1.9, 2, 2.2, 1.5, 1.4, 2.3, 2.4,
1.8, 1.8, 2.1, 2.4, 2.3, 1.9, 2.3, 2.5, 2.3, 1.9, 2, 2.3, 1.8
)), subset = c(19L, 48L, 123L, 90L, 9L, 72L, 78L, 17L, 120L,
47L, 143L, 92L, 52L, 109L, 34L, 134L, 4L, 39L, 66L, 64L, 140L,
142L, 3L, 137L, 36L, 51L, 49L, 139L, 82L, 43L, 2L, 63L, 95L,
99L, 117L, 119L, 103L, 61L, 10L, 45L, 110L, 7L, 55L, 74L, 146L,
29L, 60L, 71L, 56L, 37L, 73L, 23L, 107L, 87L, 54L, 125L, 118L,
132L, 144L, 127L, 108L, 141L, 41L, 148L, 22L, 147L, 104L, 83L,
111L, 101L, 91L, 24L, 150L, 40L, 76L, 113L, 1L, 67L, 12L, 18L,
59L, 13L, 122L, 100L, 68L, 124L, 26L, 21L, 131L, 149L, 94L, 16L,
57L, 80L, 65L, 112L, 44L, 81L, 46L, 98L, 50L, 102L, 27L, 79L,
35L, 28L, 136L, 69L, 96L, 31L, 97L, 88L, 129L, 62L, 116L, 6L,
30L, 32L, 145L, 70L), gamma = NA_real_, cost = NA_real_, kernel = "radial",
type = "eps-regression")
2: do.call(method, c(list(train.x, data = data, subset = train.ind[[sample]]),
pars, list(...)))
1: tune(svm, Petal.Width ~ ., data = iris_data, kernel = "radial",
type = "eps-regression", ranges = list(gamma = c(0.1, 0.001),
cost = c(1, 10)), tunecontrol = tune.ctrl3)
【问题讨论】:
【参考方案1】:这是一个库/R 版本问题。我更新了它们,它起作用了!
【讨论】:
以上是关于svm 超参数调整:使用 e1071 tune.control 进行随机搜索。 random != NULL 在外部函数调用中给出 NA/NaN/Inf (arg 10)的主要内容,如果未能解决你的问题,请参考以下文章
R语言使用e1071包中的svm函数构建支持向量机SVM模型使用tune.svm函数基于网格搜索(10折交叉验证)对RBF核函数的gamma参数和cost参数进行参数寻优使用最优参数构建最终模型
R语言使用e1071包中的svm函数构建支持向量机SVM模型使用tune.svm函数基于网格搜索(10折交叉验证)对RBF核函数的gamma参数和cost参数进行参数寻优使用最优参数构建最终模型
R语言e1071包中的支持向量机:螺旋线型线性不可分数据集RBF核函数支持向量机SVM(验证模型在测试集上的表现可视化模型预测的结果添加超平面区域与原始数据标签进行对比分析)
R语言e1071包中的支持向量机:仿真数据(螺旋线性不可分数据集)简单线性核的支持向量机SVM(模型在测试集上的表现可视化模型预测的结果添加超平面区域与原始数据标签进行对比分析)如何改进核函数