stan-dev/pystan2

Unable to save intermediate steps of optimizing

daeh opened this issue · 3 comments

daeh commented

Summary:

I'm unable to save variables at each optimizing step. Also, the docs seem to indicate that multiple chains are supported but I'm having trouble using multiple optimizing chains.

Description:

When I run optimizing with save_iterations=True and with a sample_file provided, I see output printed in the command line, but the variables returned in the dictionary only represent the terminal values and the sample file does not contain the intermediate values (example sample file below).

Re chains, based on the docs at https://pystan.readthedocs.io/en/latest/_modules/pystan/model.html#StanModel.optimizing, it seems like I should be able to pass optimizing() a chains parameter, but both chains and n_jobs raises an unrecognized flag error.

Reproducible Steps:

ocode = """
data {
    int<lower=1> N;
    real y[N];
}
parameters {
    real mu;
}
model {
    y ~ normal(mu, 1);
}
"""
sm = pystan.StanModel(model_code=ocode)
y2 = np.random.normal(size=20)
np.mean(y2)
op = sm.optimizing(data=dict(y=y2, N=len(y2)), iter=100, verbose=True, save_iterations=True, sample_file='test.txt')

Current Output:

shape of op['mu'] is () (single value)

# Point Estimate Generated by Stan
# stan_version_major=2
# stan_version_minor=19
# stan_version_patch=1
# init=random
# enable_random_init=0
# seed=1580683796
# chain_id=1
# iter=100
# refresh=1
# save_iterations=1
# algorithm=LBFGS
# init_alpha=0.001
# tol_obj=1e-12
# tol_grad=1e-08
# tol_param=1e-08
# tol_rel_obj=10000
# tol_rel_grad=1e+07
# history_size=5
# sample_file=test.txt
# append_samples=0
#

Expected Output:

I expect the shape of op['mu'] should be (100,) and for the values to be written to the sample_file

PyStan Version:

2.19.1.1

Python Version:

3.8.1

Operating System:

centos-release-7-7.1908.0.el7.centos.x86_64

Hi,

I'm not sure if this is even possible currently or not. Any ideas if CmdStan does this correctly?

It's also not saved in RStan. I would check CmdStan, but develop is broken. It looks like we're short some downstream tests from somewhere.

daeh commented

CmdStan does save the iteration step values correctly (output from bernoulli example attached).

examples/bernoulli/bernoulli optimize iter=1000 save_iterations=1 data file=examples/bernoulli/bernoulli.data.R output file=output.csv diagnostic_file=diagnostics.txt

output.zip