7

I need to train a neural network using PSO algorithm in R enviroment. I already know all the R packages about neural networks ( neuralnet, AMORE, etc. ), but no one of these includes PSO training ( only backpropagation ).

Ideas? Thanks for the help.

1
  • 3
    I'm not familiar with that algorithm, but here's a paper that appears to use PSO via R. Also the package hydroPSO exists, which appears to be using this (though specifically for environmental research). Good luck!
    – Hendy
    Commented Jul 12, 2012 at 20:52

1 Answer 1

1

I used PSO to optimise the hyperparameters of a XGBoost model in R in the past. See this link for the article

Code Example below:

# Set parameter settings for search algorithm
max_iter <- 10 # maximum number of iterations
pop_size <- 10 # population size

# Create custom function for assessing solutions
eval_function_XGBoost_Linear <- function(x, data, train_settings) {
  
  x1 <- x[1]; x2 <- x[2]; x3 <- x[3]; x4 <- x[4]
  
suppressWarnings(
  # Create dataframe with proportion of each solid component
  XGBoost_Linear_model <- caret::train(Strength ~., 
                                      data = data,
                                      method = "xgbLinear",
                                      trControl = train_settings,
                                      verbose = FALSE, 
                                      silent = 1,
                                      tuneGrid = expand.grid(
                                                            nrounds = round(x1),
                                                            eta = 10^x2, 
                                                            alpha = 10^x3,
                                                            lambda = 10^x4
                                                            ) 
                                      )
)

    return(XGBoost_Linear_model$results$RMSE) # minimize RMSE

}

# Define minimum and maximum values for each input
nrounds_min_max <- c(10,10^3)
eta_min_max <- c(-5,3)
alpha_min_max <- c(-3,1)
lambda_min_max <- c(-3,1)

set.seed(1)
n_cores <- detectCores()-1

# Run search algorithm
PSO_model_XGBoost_Linear <- pso::psoptim(
  par = rep(NA, 4),
  fn = eval_function_XGBoost_Linear, 
  lower = c(nrounds_min_max[1], eta_min_max[1], alpha_min_max[1], lambda_min_max[1]),
  upper = c(nrounds_min_max[2], eta_min_max[2], alpha_min_max[2], lambda_min_max[2]), 
  control = list(
                trace = 1, 
                maxit = max_iter, 
                REPORT = 1, 
                trace.stats = T,
                s = pop_size, 
                maxit.stagnate = round(0.75*max_iter), 
                vectorize = T,
                type = "SPSO2011"
                ),
  data = training_set,
  train_settings = train_control
  )

Hope this helps!

2
  • 1
    Please provide the core code instead of providing an entire page. It takes much time to understand. Commented Apr 14, 2019 at 11:30
  • I've added the code, have a look and let me know if you have any questions Commented Nov 3, 2022 at 10:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.