Skip to content

TypeError: zip argument #1 must support iteration #3330

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
aakhmetz opened this issue Jan 3, 2019 · 4 comments
Closed

TypeError: zip argument #1 must support iteration #3330

aakhmetz opened this issue Jan 3, 2019 · 4 comments

Comments

@aakhmetz
Copy link

aakhmetz commented Jan 3, 2019

Description of your problem

Please provide a minimal, self-contained, and reproducible example.

import theano
from theano.compile.ops import as_op
from theano import shared
import theano.tensor as tt
import pymc3 as pm
import numpy as np
from scipy.special import gammainc

@as_op(itypes=[tt.dscalar, tt.dscalar, tt.lvector], otypes=[tt.dmatrix])
def pgamma_for_projection(delay_mean, delay_sd, taus):
    τs_to_forecast = np.append([0],taus[taus<0])
    
    res = np.array([],dtype=np.float64).reshape(0,τs_to_forecast.shape[0])
    for τ in taus[taus>=0]:
        res0 = gammainc((delay_mean/delay_sd)**2, delay_mean*(τ-τs_to_forecast)/(delay_sd**2))
        res0 = np.r_[np.diff(res0),1-res0[-1]]
        res = np.vstack([res,res0])
    return res

τs = np.arange(17,-28,-7,dtype=np.int64)

with pm.Model() as model:
    delaymean = pm.Uniform('delaymean',13,16)
    delaysd = pm.Uniform('delaysd',7,10)
    
    trace = pm.sample(1000, tune=200)
    
    probs = pm.Deterministic('probs',pgamma_for_projection(delaymean,delaysd,shared(τs)))
    counts = pm.NegativeBinomial('counts',10,probs)
    ttl = pm.Deterministic('ttl',counts)

    ppc = pm.sample_posterior_predictive(trace, vars=[probs,counts,ttl], samples=500)

Please provide the full traceback.

Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [delaysd, delaymean]
Sampling 2 chains: 100%|██████████| 2400/2400 [00:01<00:00, 1706.70draws/s]
  0%|          | 0/500 [00:00<?, ?it/s]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-72-1a6f7c9ddc3b> in <module>
     22     ttl = pm.Deterministic('ttl',counts)
     23 
---> 24     ppc = pm.sample_posterior_predictive(trace, vars=[probs,counts,ttl], samples=500)

~/anaconda3/lib/python3.6/site-packages/pymc3/sampling.py in sample_posterior_predictive(trace, samples, model, vars, size, random_seed, progressbar)
   1102                 param = trace[idx % len_trace]
   1103 
-> 1104             values = draw_values(vars, point=param, size=size)
   1105             for k, v in zip(vars, values):
   1106                 ppc_trace[k.name].append(v)

~/anaconda3/lib/python3.6/site-packages/pymc3/distributions/distribution.py in draw_values(params, point, size)
    394                                             point=point,
    395                                             givens=givens.values(),
--> 396                                             size=size)
    397                         evaluated[param_idx] = drawn[param] = value
    398                         givens[param.name] = (param, value)

~/anaconda3/lib/python3.6/site-packages/pymc3/distributions/distribution.py in _draw_value(param, point, givens, size)
    502                 not all(var.dshape == getattr(val, 'shape', tuple())
    503                         for var, val in zip(variables, values))):
--> 504                 output = np.array([func(*v) for v in zip(*values)])
    505             elif (size is not None and any((val.ndim > var.ndim)
    506                   for var, val in zip(variables, values))):

TypeError: zip argument #1 must support iteration

Please provide any additional information below.

I wanted to sample sums over columns of counts, but sum() didn't work for me and then even simple creation of Deterministic node led to a Type error.

Versions and main components

  • PyMC3 Version: 3.6
  • Theano Version: 1.0.3
  • Python Version: 3.6.6
  • Operating system: osX
  • How did you install PyMC3: (conda/pip) pip with the latest version from github
@twiecki
Copy link
Member

twiecki commented Jan 3, 2019

It seems to have problems with sending samples through the determinstic. Maybe @lucianopaz has an idea?

@lucianopaz
Copy link
Member

I'll look into this in more detail but from a first glance, I think I had run into this also in the PR #3293. The problem I had run into was that the givens dictionary included some variables that were not ancestors of the variable that was going to be compiled and then executed. These non-ancestors could be scalars or things whose shape did not broadcast well with the true ancestors, and that lead to the zip error. If this error ends up being caused by the same thing, then #3293 should fix it.

@lucianopaz
Copy link
Member

I checked the issue more in depth. The problem is caused because counts is not given a shape during its construction. In the present implementation of random variates and distributions done in pymc3, the variable's shape is not inferred through broadcasting of the distribution's parameters. The only solution to explicitly set the variable's shape by hand during its construction like this:

...
    counts = pm.NegativeBinomial('counts', 10, probs,
                                 shape=(np.sum(τs >= 0),
                                        np.sum(τs < 0) + 1))
...

This is a limitation of pymc3's implementation of random variables and distributions. @brandonwillard's ideas of writing random variates as theano.Op's would eventually solve problems like this one, but I think that this issue can be closed.

@twiecki twiecki closed this as completed Jan 5, 2019
@aakhmetz
Copy link
Author

aakhmetz commented Jan 6, 2019

Thanks for checking! Sorry I haven't thought of that shape-problem. Probably, it would be a nice practice of coding just to define the shape parameter everywhere, so that possible errors could be avoided

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

3 participants