R optim-从单个迭代中获取值 [英] R optim - retrieving values from individual iterations
问题描述
很抱歉重复下面提到的问题.
Sorry for possible duplication of the question mentioned bellow.
我想在optim
函数的不同迭代中检索优化参数的值.目的是检查验证数据集上错误的收敛性.
I want to retirieve values of optimized parameters in different iterations of the optim
function. The purpose is to check convergence of the error on the validation dataset.
我的问题与此问题密切相关,并且我试图在其中实现本应解决问题的代码.但是,i
和$count
的值都表明,优化函数的调用次数比maxit
参数中指定的次数大得多:
My question is closely related to this question and I tried to implement the code therein that I supposed to solve my problem. However, both values of i
and $count
suggest that the optimized function is called much more times than specified in the maxit
parameter:
vals <- list()
f1 <- function(x) {
i <<- i+1
vals[[i]] <<- x
x1 <- x[1]
x2 <- x[2]
x1^2 + 3*x2^2
}
# countBFGS
i <- 0
optim(c(1,1), f1, method="BFGS",control = list(trace=1, maxit = 10))$count
i
# countCG
i <- 0
optim(c(1,1), f1, method="CG",control = list(trace=1, maxit = 10))$count
i
# countSANN
i <- 0
optim(c(1,1), f1, method="SANN",control = list(trace=1, maxit = 10))$count
i
有人建议如何实时捕获优化的参数吗?
Any suggestions how to capture optimized parameters on-the-fly?
推荐答案
观察到的计数差异是由于还调用了目标函数来计算数值导数这一事实.如果我们提供导数,则不会发生,并且计数和i
将对应.在下面的示例中,它们都是24:
The difference in counts that is observed is due to the fact that the objective function will also be called to compute the numeric derivative. If we supply a derivative then that won't happen and the count and i
will correspond. In the example below they are both 24:
vals <- NULL; i <- 0
gr1 <- function(x) c(2, 6) * x # gradient
optim(c(1, 1), f1, gr1, method = "BFGS", control = list(trace = 1))$count
## initial value 4.000000
## final value 0.000000
## converged
## function gradient
## 24 9
i
## [1] 24
此外,如果我们使用的优化方法最初不使用导数(例如Nelder Mead),那么count和i也将对应.试试这个:
Also if we use an optimization method that does not use derivatives in the first place such as Nelder Mead then the count and i will also correspond. Try this:
vals <- NULL; i <- 0
optim(c(1, 1), f1, method = "Nelder", control = list(trace = 1))$count
i
已添加:如果使用maxit
,请尝试跟踪f1
和gr1
函数.将对gr1
进行maxit
次评估,并且在每次gr1
评估之前,对f1
的最后评估可用于监视f1
.
ADDED: If using maxit
then try tracing the f1
and gr1
functions. gr1
will be evaluated maxit
times and the last valuation of f1
prior to each gr1
evaluation could be used to monitor f1
.
vals <- NULL; i <- 0
gr1 <- function(x) c(2, 6) * x # gradient
trace(gr1, exit = quote(print(c(returnValue(), x))))
trace(f1, exit = quote(print(c(i, returnValue(), x))))
optim(c(1, 1), f1, gr1, method = "BFGS", control = list(trace = 10, maxit = 5))$count
untrace(f1)
untrace(gr1)
给予:
Tracing fn(par, ...) on exit
[1] 1 4 1 1
initial value 4.000000
Tracing gr(par, ...) on exit
[1] 2 6 1 1
Tracing fn(par, ...) on exit
[1] 2 76 -1 -5
Tracing fn(par, ...) on exit
[1] 3.00 0.48 0.60 -0.20
Tracing gr(par, ...) on exit
[1] 1.2 -1.2 0.6 -0.2
Tracing fn(par, ...) on exit
[1] 4.00000000 0.55976676 -0.73469388 0.08163265
Tracing fn(par, ...) on exit
[1] 5.0000000 0.1728560 0.3330612 -0.1436735
Tracing gr(par, ...) on exit
[1] 0.6661224 -0.8620408 0.3330612 -0.1436735
Tracing fn(par, ...) on exit
[1] 6.000000e+00 1.207714e-05 1.192941e-03 1.884501e-03
Tracing gr(par, ...) on exit
[1] 0.002385882 0.011307005 0.001192941 0.001884501
Tracing fn(par, ...) on exit
[1] 7.000000e+00 7.788526e-09 -5.338595e-05 -4.057284e-05
Tracing gr(par, ...) on exit
[1] -1.067719e-04 -2.434371e-04 -5.338595e-05 -4.057284e-05
final value 0.000000
stopped after 5 iterations
function gradient
7 5
这篇关于R optim-从单个迭代中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!