svm超参数调整:使用e1071 tune.control进行随机搜索。 random!= NULL在外部函数调用中给出NA / NaN / Inf(arg 10)

时间:2019-05-23 20:19:52

标签: r svm libsvm hyperparameters

我正在尝试使用e1071进行一些简单的(随机搜索)超参数调整。我知道如何使用mlr来完成此任务,但我只想使用e1071。

我能够执行网格搜索以进行超参数调整(这是我使用虹膜数据集放在一起的随机示例,默认情况下在很多地方都给出了

Resources:
  MyFunctionResource:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: MyFunctionName
      CodeUri: ./
      Handler: "lambda_function.lambda_handler"
      MemorySize: 128
      Runtime: python3.7
      Timeout: 3
      Policies:
        - Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Action:
                - "cognito-idp:*"
                - "logs:*"
                ...
              Resource: "*"
        - Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Action: "lambda:InvokeFunction"
              Principal:
                Service: cognito-idp.amazonaws.com
              Resource: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:MyFunctionName"

这两个简单的案例起作用。但是,我想使用随机搜索而不是网格搜索。我想在tune.control()https://rdrr.io/cran/e1071/man/tune.control.html

中使用Random参数

我尝试了以下两个示例,

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 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 )

如果我执行traceback(),我看到参数(伽玛和成本)以NA_real_的形式传递:我在做什么错?我应该如何使用random =?

Error in svm.default(x, y, scale = scale, ..., na.action = na.action) : 
  NA/NaN/Inf in foreign function call (arg 10)

1 个答案:

答案 0 :(得分:0)

这是一个库/ R版本问题。我更新了它们,它起作用了!