【问题标题】:How to optimize parameters using genetic algorithms如何使用遗传算法优化参数
【发布时间】:2015-11-08 16:13:51
【问题描述】:

我想在 R 中使用 GA 优化 eps-regression (SVR) 中的三个参数(gamma、cost 和 epsilon)。这就是我所做的。

library(e1071)
data(Ozone, package="mlbench")
a<-na.omit(Ozone)
index<-sample(1:nrow(a), trunc(nrow(a)/3))
trainset<-a[index,]
testset<-a[-index,]
model<-svm(V4 ~ .,data=trainset, cost=0.1, gamma=0.1, epsilon=0.1, type="eps-regression", kernel="radial")
error<-model$residuals
rmse <- function(error) #root mean sqaured error
{
  sqrt(mean(error^2))
}
rmse(error)

在这里,我将 cost、gamma 和 epsilon 分别设置为 0.1,但我认为它们不是最佳值。所以,我想使用遗传算法来优化这些参数。

GA <- ga(type = "real-valued", fitness = rmse,
         min = c(0.1,3), max = c(0.1,3),
         popSize = 50, maxiter = 100)

在这里,我使用 RMSE 作为适应度函数。但我认为适应度函数必须包含要优化的参数。但是,在 SVR 中,目标函数太复杂了,无法用 R 代码写出来,我试图找到很长时间但无济于事。同时了解SVR和GA的人,有使用GA优化SVR参数经验的人,请帮助我。请。

【问题讨论】:

    标签: r optimization genetic-algorithm


    【解决方案1】:

    在这样的应用程序中,将要优化其值的参数(在您的情况下为costgammaepsilon)作为适应度函数的参数传递,然后运行模型拟合+评估函数并使用模型性能的度量作为适应度的度量。因此,目标函数的显式形式并不直接相关。

    在下面的实现中,我使用 5 折交叉验证来估计给定参数集的 RMSE。特别是,由于包GA 最大化了适应度函数,我将给定参数值的适应度值写为减去交叉验证数据集的平均 rmse。因此,可以达到的最大适应度为零。

    这里是:

    library(e1071)
    library(GA)
    
    data(Ozone, package="mlbench")
    Data <- na.omit(Ozone)
    
    # Setup the data for cross-validation
    K = 5 # 5-fold cross-validation
    fold_inds <- sample(1:K, nrow(Data), replace = TRUE)
    lst_CV_data <- lapply(1:K, function(i) list(
        train_data = Data[fold_inds != i, , drop = FALSE], 
        test_data = Data[fold_inds == i, , drop = FALSE]))
    
    # Given the values of parameters 'cost', 'gamma' and 'epsilon', return the rmse of the model over the test data
    evalParams <- function(train_data, test_data, cost, gamma, epsilon) {
        # Train
        model <- svm(V4 ~ ., data = train_data, cost = cost, gamma = gamma, epsilon = epsilon, type = "eps-regression", kernel = "radial")
        # Test
        rmse <- mean((predict(model, newdata = test_data) - test_data$V4) ^ 2)
        return (rmse)
    }
    
    # Fitness function (to be maximized)
    # Parameter vector x is: (cost, gamma, epsilon)
    fitnessFunc <- function(x, Lst_CV_Data) {
        # Retrieve the SVM parameters
        cost_val <- x[1]
        gamma_val <- x[2]
        epsilon_val <- x[3]
    
        # Use cross-validation to estimate the RMSE for each split of the dataset
        rmse_vals <- sapply(Lst_CV_Data, function(in_data) with(in_data, 
            evalParams(train_data, test_data, cost_val, gamma_val, epsilon_val)))
    
        # As fitness measure, return minus the average rmse (over the cross-validation folds), 
        # so that by maximizing fitness we are minimizing the rmse
        return (-mean(rmse_vals))
    }
    
    # Range of the parameter values to be tested
    # Parameters are: (cost, gamma, epsilon)
    theta_min <- c(cost = 1e-4, gamma = 1e-3, epsilon = 1e-2)
    theta_max <- c(cost = 10, gamma = 2, epsilon = 2)
    
    # Run the genetic algorithm
    results <- ga(type = "real-valued", fitness = fitnessFunc, lst_CV_data, 
        names = names(theta_min), 
        min = theta_min, max = theta_max,
        popSize = 50, maxiter = 10)
    
    summary(results)
    

    产生结果(对于我指定的参数值范围,可能需要根据数据进行微调):

    GA results: 
    Iterations             = 100 
    Fitness function value = -14.66315 
    Solution               = 
             cost      gamma    epsilon
    [1,] 2.643109 0.07910103 0.09864132
    

    【讨论】:

    • 非常感谢!该代码适用于臭氧数据。但是,如果我从臭氧数据中删除了一些行,或者如果我更改了特定列中的数字,它就不起作用,并给出“predict.svm(ret, xhold, decision.values = TRUE) 中的错误:模型为空! "错误。我该如何解决这个问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 2015-02-18
    • 2013-07-02
    • 2016-10-04
    • 1970-01-01
    相关资源
    最近更新 更多