我使用的是带有nnet方法的插入符号包。当我将maxit参数从300更改为500时,我得到了不同的结果。我的理解是,如果maxit增加,模型将经历最大"n“次迭代来找到局部最小值。
在我的例子中,当我将maxit设置为300时,我得到了很好的结果,而当它是500时,我得到了很好的结果。
注:种子值、tune_grid、折叠数在两个模型中是相同的。
1)我得到了不同的结果,因为在神经网络优化中有很多局部极小值?
2) maxit越高,模型越好-对还是错?(潜在的假设是,如果模型不是随着300次迭代而收敛,那么当迭代次数增加时,它将收敛)
3)如何调整maxit参数?
发布于 2019-01-17 05:18:52
您指定的nnet方法正在使用迭代优化(基数R中optim()函数的BFGS方法)来估计模型1的参数。优化应在收敛时停止。如果maxit设置得太低,则模型将无法收敛。
BFGS方法不能保证对所有优化问题都收敛。尽管如此,它被认为是一种很好的优化方法。优化表面是依赖于数据的,所以我不会为你的情况评论最小值的数量或性质。您可能在300次迭代时达到了局部最小值,但nnet()函数(设置随机权重)中存在一些随机性,因此即使所有nnet()参数都相同,后续运行也可能不同。请注意使用相同参数运行的两个后续nnet()之间的差异- 4.115351与100次迭代时的2.112400。
library(nnet)
data(iris)
set.seed(42)
nnet(Species ~ ., data=iris, size=10)
# weights: 83
initial value 262.654300
iter 10 value 72.296066
iter 20 value 10.287034
iter 30 value 6.341659
iter 40 value 5.814649
iter 50 value 5.187836
iter 60 value 4.199448
iter 70 value 4.150082
iter 80 value 4.122058
iter 90 value 4.117969
iter 100 value 4.115351
final value 4.115351
stopped after 100 iterations
a 4-10-3 network with 83 weights
inputs: Sepal.Length Sepal.Width Petal.Length Petal.Width
output(s): Species
options were - softmax modelling
# Deliberately not setting seed value before second nnet run
nnet(Species ~ ., data=iris, size=10)
# weights: 83
initial value 201.869745
iter 10 value 67.631035
iter 20 value 11.863275
iter 30 value 6.542750
iter 40 value 5.758701
iter 50 value 5.355368
iter 60 value 3.970210
iter 70 value 2.835171
iter 80 value 2.414463
iter 90 value 2.226375
iter 100 value 2.112400
final value 2.112400
stopped after 100 iterations
a 4-10-3 network with 83 weights
inputs: Sepal.Length Sepal.Width Petal.Length Petal.Width
output(s): Species
options were - softmax modelling还要注意,上面运行的两个nnet()都没有收敛。以下是一个融合模型的示例:
set.seed(42)
nnet(Species ~ ., data=iris, size=10, maxit=500)
# weights: 83
initial value 262.654300
iter 10 value 72.296066
iter 20 value 10.287034
# I've truncated the output here
iter 360 value 0.000277
iter 370 value 0.000117
final value 0.000097
converged
a 4-10-3 network with 83 weights
inputs: Sepal.Length Sepal.Width Petal.Length Petal.Width
output(s): Species
options were - softmax modelling请注意,上面输出中的"converged“。
不幸的是,不能使用插入符号train函数的tune_grid选项来调优maxit参数。在train调用中为maxit设置一个较高的值可能是合理的,但我不建议使用一个值,因为它同样依赖于数据。对于虹膜数据,我会尝试一个比收敛的最大迭代次数高一个数量级或两个数量级的值。或者,您可以遍历maxit的值
num.it <- 500 # max number of training iterations
fit.dat <- matrix(ncol=1, nrow=num.it) # fitting criterion values
for(i in 1:num.it) {
# to monitor progress
cat(i,'\n')
flush.console()
# to ensure same set of random starting weights are used each time
set.seed(42)
# temporary nnet model
mod.tmp <- nnet(Species ~ ., data=iris, size=10, maxit=i, trace=F)
# append fitting criterion value
fit.dat[i,] <- mod.tmp$value
}
# extract convergence values
which.min(fit.dat)
[1] 375
fit.dat[which.min(fit.dat)]
[1] 9.654717e-05
# plot fitting values
plot(fit.dat, type='l')上面的循环调整了maxit,但没有考虑过拟合。更好的方法是将插入符号train()函数与当前的tune_grid和交叉验证设置一起使用。您还必须检查插入符号train()函数的输出是否收敛。
此外,插入符号和其他包可能会在set.seed()中出现令人惊讶的重现性问题:R: set.seed() results don't match if caret package loaded
最后,这不太可能有帮助,但是看看插入trainControl()函数的seeds选项可能会很有趣。正如文档所说,它可能只有在运行并行作业时才有用。
https://stackoverflow.com/questions/54222113
复制相似问题