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

access to iteration number and chain id within model #1166

Open
bob-carpenter opened this issue Dec 10, 2014 · 11 comments
Open

access to iteration number and chain id within model #1166

bob-carpenter opened this issue Dec 10, 2014 · 11 comments

Comments

@bob-carpenter
Copy link
Contributor

Sean O'Riordain suggested on stan-users that it would be useful to be able to access the iteration number in a Stan program so that you could write something like this to print theta every 100th iteration:

if (mod(iteration.count,100) == 0) print(theta);

It would also be nice to get the chain_id in case things are running in parallel.

This brings up the issue of what the values should be when, for instance, we're running diagnostics, just evaluating the log probability function, running optimization, etc. Maybe just -1 values everywhere?

There is also the issue of which blocks this should work in. Should prints in the transformed data block get a value of iteration=0 as the value? The chain_id would still work.

@betanalpha
Copy link
Contributor

The way the models are abstracted from the sampling code I
can’t think of any way to get at this information any time log_prob
is called.

On Dec 10, 2014, at 1:01 PM, Bob Carpenter notifications@github.com wrote:

Sean O'Riordain suggested on stan-users that it would be useful to be able to access the iteration number in a Stan program so that you could write something like this to print theta every 100th iteration:

if (mod(iteration.count,100) == 0) print(theta);
It would also be nice to get the chain_id in case things are running in parallel.

This brings up the issue of what the values should be when, for instance, we're running diagnostics, just evaluating the log probability function, running optimization, etc. Maybe just -1 values everywhere?

There is also the issue of which blocks this should work in. Should prints in the transformed data block get a value of iteration=0 as the value? The chain_id would still work.


Reply to this email directly or view it on GitHub.

@bob-carpenter
Copy link
Contributor Author

It would need to be a separate argument passed through.

I'm torn, because on the one hand, I agree that it'd be useful
to have on the model, but on the other hand, I agree it breaks the
standalone log density abstraction.

Given that it would also complicate the code, which is always
a big negative, I haven't brought this up before (though I've thought
about it many times for exactly the same reason that Sean O'Riordain
brought up).

  • Bob

On Dec 10, 2014, at 1:04 PM, Michael Betancourt notifications@github.com wrote:

The way the models are abstracted from the sampling code I
can’t think of any way to get at this information any time log_prob
is called.

On Dec 10, 2014, at 1:01 PM, Bob Carpenter notifications@github.com wrote:

Sean O'Riordain suggested on stan-users that it would be useful to be able to access the iteration number in a Stan program so that you could write something like this to print theta every 100th iteration:

if (mod(iteration.count,100) == 0) print(theta);
It would also be nice to get the chain_id in case things are running in parallel.

This brings up the issue of what the values should be when, for instance, we're running diagnostics, just evaluating the log probability function, running optimization, etc. Maybe just -1 values everywhere?

There is also the issue of which blocks this should work in. Should prints in the transformed data block get a value of iteration=0 as the value? The chain_id would still work.


Reply to this email directly or view it on GitHub.


Reply to this email directly or view it on GitHub.

@bob-carpenter
Copy link
Contributor Author

If we did do this, we'd probably want to do it via functions:

  int meta_iteration_num();
  int meta_chain_id();
  int meta_is_warming_up();
  int meta_is_sampling();

I'd want to print a warning with any use of such a meta function at the very least. And probably call it something other than "meta", which tends to get overused.

@bob-carpenter
Copy link
Contributor Author

Joshua N Pritikin points out on stan-dev:

Maybe a mcmc_ prefit would be more descriptive since Stan also does
BGFS-esque optimization.

@bob-carpenter
Copy link
Contributor Author

If iteration number is really just for print control every N-th iteration, we could control that from the outside by passing in a NULL ostream.

And maybe we should always print the chain ID before any printed output?

@peleschramm
Copy link

peleschramm commented May 20, 2017

I would also like access to iteration number, but for hacking purposes (some Simulated Annealing may be possible this way, for example).

For just printing values, the solution I use is to have Stan periodically update the output csv file (including warmup), and run a script that periodically reads the csv file and plots the samples for whatever I'm interested in. That way you can view the entire trajectory as stan samples. If using matlabstan, the function "mstan.read_stan_csv" is helpful for this.

@bob-carpenter
Copy link
Contributor Author

I just looked back through this issue. We've gone back and forth on whether we should allow this.

@betanalpha and I don't like it because it breaks the nice abstraction of an instantiated model as an immutable log density function. Making things immutable is powerful for reasoning about program behavior and writing correct code.

We could break this abstraction by either (1) storing iteration and chain number in special mutable variables, or (2) treating them like data and allowing data to be non-constant (mutable) in general. I'm inclined toward the latter because we may want to do this for data streaming or parallel algorithms like stochastic gradient descent for optimization (penalized MLE) or variational inference (VB) or data-parallel expectation propagation (EP) or gradient-based marginal optimization (penalized MML).

@syclik
Copy link
Member

syclik commented May 20, 2017 via email

@bob-carpenter
Copy link
Contributor Author

bob-carpenter commented May 22, 2017 via email

@syclik
Copy link
Member

syclik commented May 22, 2017 via email

@betanalpha
Copy link
Contributor

betanalpha commented May 22, 2017 via email

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

No branches or pull requests

4 participants