Search code examples
rregressionr-carettraining-datahyperparameters

How to random search in a specified grid in caret package?


I wonder it is possible to use random search in a predefined grid. For example, my grid has alpha and lambda for glmnet method. alpha is between 0 and 1, and lambda is between -10 to 10. I want to use random search 5 times to randomly try points in this bound. I wrote the following code for grid search and it works fine, but I cannot modify it for random search in a bound.

rand_ctrl <- trainControl(method = "repeatedcv", repeats = 5,
                          search = "random")
grid <- expand.grid(alpha=seq(0,1,0.1),lambda=seq(-10,10,1)) # I think this should be modified
rand_search <- train(Response ~ ., data = train_dat,
                     method = "glmnet",
                     ## Create 20 random parameter values
                     metric = "RMSE",
                     tuneLength = 5,
                     preProc = c("scale"),
                     tuneGrid = grid,
                     trControl = rand_ctrl)

Solution

  • One approach would be to define a grid and use sample to pick several random rows:

    set.seed(1)
    samp <- sample(1:nrow(grid), 5)
    grid[samp,]
    #output
        alpha lambda
    62    0.6     -5
    86    0.8     -3
    132   1.0      1
    208   0.9      8
    46    0.1     -6
    

    and then use this subset as tuneGrid argument

    Another approach would be to use runif which generates random numbers from a uniform distribution defined by lower and upper bound:

    set.seed(1)
    data.frame(alpha = runif(5, 0 , 1),
               lambda = runif(5, -10, 10))
    #output
          alpha    lambda
    1 0.2655087  7.967794
    2 0.3721239  8.893505
    3 0.5728534  3.215956
    4 0.9082078  2.582281
    5 0.2016819 -8.764275
    

    and provide this as tuneGrid argument.

    The second approach does not pick random elements from a grid but rather random numbers between defined minimum and maximum.