Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output of optimizing does not return the number of used iterations or reason why the optimization was terminated #1137

Open
helske opened this issue Sep 12, 2024 · 2 comments

Comments

@helske
Copy link

helske commented Sep 12, 2024

It would be great if the output of optimizing contained the number of iterations the optimizer ran. This could be then to check whether the optimization terminated because of hitting the maximum number of iterations. Even better solution would be to get the actual reason for the termination from the output, which is currently only available in the print when verbose = TRUE, but I understand that this is likely more difficult due to the way rstan works.

Example

library(rstan)
m <- stan_model(
  model_code = "
  transformed data {
    vector[2] y = [5, 10.2]';
  } 
  parameters {
    real x;
  } 
  model {
    y ~ normal(x, 1);
  }")
f <- optimizing(m, iter = 2, verbose = TRUE)
str(f)
Chain 1: Initial log joint probability = -38.2441
Chain 1:     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes 
Chain 1:        1      -26.9098       1.12221       8.97772         0.1       0.001        4   
Chain 1:     Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes 
Chain 1:        2         -6.76       4.48886   7.10543e-15           1           1        5   
Chain 1: Optimization terminated normally: 
Chain 1:   Convergence detected: gradient norm is below tolerance

str(f)
List of 4
 $ par        : Named num 7.6
  ..- attr(*, "names")= chr "x"
 $ value      : num -6.76
 $ return_code: int 0
 $ theta_tilde: num [1, 1] 7.6
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr "x"

RStan Version:

2.32.6

R Version:

4.3.0

Operating System:

Windows 10

@helske
Copy link
Author

helske commented Sep 12, 2024

Actually I think the same issue is also present in cmdstanr, at least I don't see any information about the number of used iterations there either. Note also that cmdstan (and thus rstan and cmdstanr) output return code 0 (success) in case of hitting the maximum number of iterations, so checking that does not help. There is also an old issue about these return codes: stan-dev/stan#2197

@jgabry
Copy link
Member

jgabry commented Sep 20, 2024

Yeah this is unfortunate. For cmdstanr it should be possible to expose the save_iterations argument to CmdStan's optimize method (not currently an option in cmdstanr). This would result in all iterations being written to csv, which has downsides of course, but it would then allow the user to check how many iterations were ultimately used. Even better would be if CmdStan could provide just the number of iterations used and we could let the user access it, but I don't think it currently does that anywhere except in the console output. If I'm wrong about that and there's an easy way to get that info from CmdStan, I'd also love to know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants