Is there a way to extract the parameters and the objective function for each iteration in R optimx

Suppose that I have a problem using optimization R, optimx. Is there a way to retrieve the parameters and values ​​of the objective function over time?

f<-function(x){
  return(sum(abs(x)))
}

gr<-function(x){
  return(sign(x))
}
opt=optimx::optimx(runif(2),f,gr,method="BFGS")

The goal is trying to make such a plot:

enter image description here

I think we can do it manually using Gradient Decent using the following code, but how can I do it in optimx?

x=c(0.5,1.5)
alpha=0.1
max_iter=20
x_trace=matrix(rep(0,max_iter*2),ncol=2)

for (i in 1:max_iter){
  x=x-alpha*gr(x)
  x_trace[i,]=x
}
f_trace=apply(x_trace,1,f)
+4
source share
2 answers

Create a side effect:

f<-function(x){
  .GlobalEnv$i <- get("i", envir = .GlobalEnv) + 1
  .GlobalEnv$log[get("i", envir = .GlobalEnv),] <- x
  return(sum(abs(x)))
}

gr<-function(x){
  return(sign(x))
}

library(optimx)
i <- 0
log <- matrix(numeric(100 * 2), ncol = 2)

opt <- optimx(c(0.8, -0.9),f,gr,method="BFGS")
log <- log[seq_len(i), ]

plot(log, type = "l", xlim = c(-2, 2), ylim = c(-1.2, 1.2))

final schedule

, , , . control = list(trace = TRUE, REPORT = 1) optimx , capture.output , log.

optimx, , . , , , , , .

+5

, method="L-BFGS-B" . , .

library(optimx); library(dplyr)

cap <- capture.output(optimx(runif(2), f, gr, method="L-BFGS-B", 
                             control=list(trace=6, REPORT=1)))
temp <- cap[grep("X =|X0 =", cap)]
d <- gsub("X0 = |X = |Cauchy X =  ", "", temp) %>% strsplit(" ") %>% 
  unlist() %>% as.numeric() %>% matrix(ncol=2, byrow=T)

plot(-2:2,-2:2, type="n", ann=F)
for(i in c(1,2,4)) polygon(c(-0.5,0,0.5,0, -0.5)*i, c(0, 0.5, 0, -0.5, 0)*i)
points(d, pch=letters[1:nrow(d)])

[]
, (opt/lbfgs_bcm.shar) (@ , !). method="L-BFGS-B", , control=list(trace=6, REPORT=1) .

enter image description here

+2

Source: https://habr.com/ru/post/1651995/


All Articles