Load_trace and sample_posterior_predictive do not work

I have read the threads on how to load a posterior. Accordingly, I am re-initializing my model. But I am then not able to perform posterior predictions with that loaded trace. Am I doing something wrong?

I am sampling a posterior:

with pm.Model() as model:
    posterior = pm.sample(draws=10000, chains=4, cores=1, n_init=2000)
    pm.save_trace(posterior, path)

Then, in a notebook I want to work on visualizations:

with pm.Model() as model:
    posterior = pm.load_trace(path)
    posterior_pred = pm.sample_posterior_predictive(posterior)

This gives me the error TypeError: zip argument #1 must support iteration. I am attaching the full error.

~/.ve/project_test/lib/python3.7/site-packages/pymc3/sampling.py in 
sample_posterior_predictive(trace, samples, model, vars, size, random_seed, progressbar)
   1108                 param = trace[idx % len_trace]
-> 1110             values = draw_values(vars, point=param, size=size)
   1111             for k, v in zip(vars, values):
   1112                 ppc_trace[k.name].append(v)

~/.ve/project_test/lib/python3.7/site-packages/pymc3/distributions/distribution.py in draw_values(params, point, size)
    389                                         point=point,
    390                                         givens=temp_givens,
--> 391                                         size=size)
    392                     givens[next_.name] = (next_, value)
    393                     drawn[(next_, size)] = value

~/.ve/project_test/lib/python3.7/site-packages/pymc3/distributions/distribution.py in _draw_value(param, point, givens, size)
    506                     with _DrawValuesContextBlocker():
    507                         val = np.atleast_1d(dist_tmp.random(point=point,
--> 508                                                             size=None))
    509                     # Sometimes point may change the size of val but not the
    510                     # distribution's shape

~/.ve/project_test/lib/python3.7/site-packages/pymc3/distributions/continuous.py in random(self, point, size)
    452         """
    453         mu, tau, _ = draw_values([self.mu, self.tau, self.sigma],
--> 454                                  point=point, size=size)
    455         return generate_samples(stats.norm.rvs, loc=mu, scale=tau**-0.5,
    456                                 dist_shape=self.shape,

~/.ve/project_test/lib/python3.7/site-packages/pymc3/distributions/distribution.py in draw_values(params, point, size)
    420                                             point=point,
    421                                             givens=givens.values(),
--> 422                                             size=size)
    423                         evaluated[param_idx] = drawn[(param, size)] = value
    424                         givens[param.name] = (param, value)

~/.ve/project_test/lib/python3.7/site-packages/pymc3/distributions/distribution.py in _draw_value(param, point, givens, size)
    545                 not all(var.dshape == getattr(val, 'shape', tuple())
    546                         for var, val in zip(input_vars, input_vals))):
--> 547                 output = np.array([func(*v) for v in zip(*input_vals)])
    548             elif (size is not None and any((val.ndim > var.ndim)
    549                   for var, val in zip(input_vars, input_vals))):

TypeError: zip argument #1 must support iteration


I realize that my data was passed into the model as numpy arrays (not ‘shared’ variables). I accidentally re-initialized the model with differently shaped arrays, which caused this problem. The obvious solution is to make sure to re-initialize with the same shapes (data etc).

1 Like