Skip to content

IndexError in GLM using NUTS #2076

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

Closed
fonnesbeck opened this issue Apr 25, 2017 · 9 comments
Closed

IndexError in GLM using NUTS #2076

fonnesbeck opened this issue Apr 25, 2017 · 9 comments

Comments

@fonnesbeck
Copy link
Member

Running a GLM that worked in previous versions of PyMC3 now fails when NUTS sampling begins.

with pm.Model() as severity_model:
    
    formula = 'oxygen ~ ' + '+'.join(rsv_vars)
    GLM.from_formula(formula, data=variables, family=families.Binomial())
    severity_trace = pm.sample(2000)

ADVI tuning runs fine, but once sampling begins, an IndexError is raised.

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-63-9a1fc51f9f2d> in <module>()
      6     formula = 'oxygen ~ ' + '+'.join(rsv_vars)
      7     GLM.from_formula(formula, data=variables, family=families.Binomial())
----> 8     severity_trace = pm.sample(2000)

/Users/fonnescj/Repos/pymc3/pymc3/sampling.py in sample(draws, step, init, n_init, start, trace, chain, njobs, tune, nuts_kwargs, step_kwargs, progressbar, model, random_seed, live_plot, **kwargs)
    257         sample_func = _sample
    258 
--> 259     return sample_func(**sample_args)
    260 
    261 

/Users/fonnescj/Repos/pymc3/pymc3/sampling.py in _sample(draws, step, start, trace, chain, tune, progressbar, model, random_seed, live_plot, **kwargs)
    272     try:
    273         strace = None
--> 274         for it, strace in enumerate(sampling):
    275             if live_plot:
    276                 if it >= skip_first:

/Users/fonnescj/anaconda3/envs/dev/lib/python3.6/site-packages/tqdm/_tqdm.py in __iter__(self)
    831 """, fp_write=getattr(self.fp, 'write', sys.stderr.write))
    832 
--> 833             for obj in iterable:
    834                 yield obj
    835                 # Update and print the progressbar.

/Users/fonnescj/Repos/pymc3/pymc3/sampling.py in _iter_sample(draws, step, start, trace, chain, tune, model, random_seed)
    352         _update_start_vals(start, strace.point(-1), model)
    353     else:
--> 354         _update_start_vals(start, model.test_point, model)
    355 
    356     try:

/Users/fonnescj/Repos/pymc3/pymc3/sampling.py in _update_start_vals(a, b, model)
    467         for tname in b:
    468             if tname.startswith(name) and tname!=name:
--> 469                 transform_func = [d.transformation for d in model.deterministics if d.name==name][0]
    470                 b[tname] = transform_func.forward(a[name]).eval()
    471 

IndexError: list index out of range

The model also runs fine with alternative samplers (e.g. Metropolis).

Running current master on macOS and Anaconda Python 3.6.

@denadai2
Copy link
Contributor

I found this error as well and I solved it wrapping a variable I added with Deterministic. Not sure about why.

(I don't know about GLM)

@aseyboldt
Copy link
Member

Sounds like #2046 could be related. Does it also happen if you initialize Metropolis with mu from advi? Maybe it matters if the start value contains (un)transformed keys.

@twiecki
Copy link
Member

twiecki commented Apr 25, 2017

@fonnesbeck Seems like that's code from your recent PR, no?

@fonnesbeck
Copy link
Member Author

Fixed in the above PR.

@aseyboldt
Copy link
Member

@fonnesbeck Nope, still here:

with model:
    b = pm.Normal('beta_raw', mu=0, sd=1, shape=n_counties)
    b = pm.Deterministic('beta', b * 2)
    trace = pm.sample(2000, tune=1000)
...
/home/adr/git/pymc3/pymc3/sampling.py in <listcomp>(.0)
    467         for tname in b:
    468             if tname.startswith(name) and tname!=name:
--> 469                 transform_func = [d.transformation for d in model.deterministics if d.name==name]
    470                 if transform_func:
    471                     b[tname] = transform_func[0].forward(a[name]).eval()

AttributeError: 'TensorVariable' object has no attribute 'transformation'

@aseyboldt aseyboldt reopened this Apr 27, 2017
@ColCarroll
Copy link
Member

I can add a fix later -- line 519 and 998 of model.py set the transform name. we should factor that out into a few tiny helper functions (transform_name, untransform_name, is_transformed_name).

I think it is ok if we leave some edge cases uncovered: right now we use

            transformed_name = "{}_{}_".format(name, transform.name)

maybe adding one more underscore to the end, then have a function is_transformed_name just check for two trailing underscores, and untransform_name be '_'.join(name.split('_')[:-3]). I'm ok not supporting people who name their variables beta__!

@aseyboldt
Copy link
Member

Sounds reasonable to me.

@junpenglao
Copy link
Member

This seems to also relate to #2109, as transform_func[0].forward(a[name]).eval() depends on some parent, and it's not pass to the transform_func during evaluation.

This was referenced Jun 15, 2017
@junpenglao
Copy link
Member

fixed in #2328

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

6 participants