$output() in CmdStanVB object - Save the Number of Iterations

Hi there! I run Stan’s variational approximation using $variational() in RStudio. My issue is that I cannot find a way to save the number of iterations at the end.

To be more clear:

  1. I run the variational approximation (e.g. fit_vb = model_stan$variational(...)).
  2. A part of the output that is appeared in my Console is:
  iter             ELBO   delta_ELBO_mean   delta_ELBO_med   notes  
     1          -23.583             1.000            1.000 
     2          -26.642             0.557            1.000 
   ...... (skipped lines)
   538          -10.157             0.019            0.010 
   539          -10.134             0.019            0.010   MEDIAN ELBO CONVERGED 
  1. I need to save the number of iterations (in this case 539). So, I run fit_vb$output() and the same data (as above) are appeared but I cannot find any way to save these data (I used for example as.matrix(fit_vb$output())).

  2. When I run typeof(fit_vb$output()) the result is ‘NULL’, so I guess that’s why I cannot save them.

Do you know any way to save/export the number of iterations?

Thank you in advance!

The content from $output() is printed to the console, so if you want to extract and save the number of iterations you’ll need to capture the output and then parse it:

output_str <- capture.output(fit_vb$output())
init_start <- grep("Begin stochastic gradient ascent", output_str) + 1
init_end <- grep("MEDIAN ELBO CONVERGED", output_str)

init_table <- data.table::fread(text=output_str[init_start:init_end],
                                fill = TRUE, header =TRUE)

Another option is to set save_latent_dynamics=TRUE, then use the $save_latent_dynamics_files() method to save as a csv file. The first column of the last row has the number you’re looking for. @andrjohns’ solution to simply parse the printed output seems easier if you only want this single value. The latent dynamics approach seems better if you want to extract a larger amount of the (meta)data from this part of the fit.

(Note that the RNG seed, eval_elbo, inits, and possibly other arguments that aren’t immediately coming to mind may have a big impact on how many iterations the algorithm takes to converge!)


@wpetry TIL! Thanks for pointing that out, I haven’t used the latent dynamics much so this is great to know

1 Like

Thank you, @andrjohns @wpetry, for your responses! I’ve tried both suggestions, and both of them work perfectly.

Thank you again for your time!

1 Like