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)

我正在尝试使用 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(),我会看到参数(gamma 和成本)作为 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.5, 3.8, 
   3.8, 3.4, 3.7, 3.6, 3.3, 3.4, 3, 3.4, 3.5, 3.4, 3.2, 3.1, 3.4, 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 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, 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, 
   0.4, 0.3, 0.2, 0.2, 0.2, 0.2, 1.4, 1.5, 1.5, 1.3, 1.5, 1.3, 1.6, 
   1, 1.3, 1.4, 1, 1.5, 1, 1.4, 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)

这是一个library/R版本问题。我更新了它们并且成功了!