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

ValueError: Unexpected type in draw_value: <class 'theano.gof.graph.Constant'> in sample_posterior_predictive #3595

Closed
bdyetton opened this issue Aug 18, 2019 · 2 comments · Fixed by #3599

Comments

@bdyetton
Copy link
Contributor

bdyetton commented Aug 18, 2019

Hi Pymc3 Devs,
I've hit a strange error when trying to sample_ppc.
It appears that somewhere in the theano graph a NoneConst is being created, and sample_posterior_predictive throws an error when it tries to sample from it.

In the example below, the simple mean model has set of mean parameters with a dimension for a factor (x), a dimension for another factor (g), and a dimension for a single y output (although this problem also occurs when y has a cardinality of 2 or more). Both g and x are used to pick a specific parameter as a y datapoint's mean.

I think this is something to do with funny broadcasting, because when I don't include they y dimension in my parameters, there is no issue (see note in code).

It could be something i'm doing wrong, but regardless, the error thrown is very hard to debug, and could maybe be improved.

An example:

    #create some data
    n_d = 500
    n_x = 2
    n_g = 10
    g = np.random.randint(0, n_g, (n_d,))  # group
    x = np.random.randint(0, n_x, (n_d,))  # x factor
    true_x_effect = np.random.normal(0, 10, (n_x,))
    true_g_effect = np.random.normal(0, 10, (n_g,))
    y = np.random.normal(true_x_effect[x]+true_g_effect[g], 1)
    data = pd.DataFrame({'x':x, 'y1':y, 'g':g})
    y_vars = ['y1']
    n_y = len(y_vars)

    with pm.Model() as model:
        multi_dim_rv = pm.Normal('multi_dim_rv', mu=0, sd=1, shape=(n_x, n_g, n_y))
        indexed_rv = multi_dim_rv[data['x'], data['g'], :] #if : is replaced by 0 (effectivly removing y dimention), this works fine
        error = pm.HalfCauchy('error', 10)
        obs_data = data[y_vars].values
        obs = pm.Normal('obs', mu=indexed_rv, sd=error, observed=obs_data)
        print('dist_shape', obs.distribution.logp(data[y_vars].values).tag.test_value.shape) #shapes seem ok
        print('data_shape', obs_data.shape) #shapes seem ok
        trace = pm.sample() #samples fine.
        pm.sample_posterior_predictive(trace) #NoneConst error here.

Trace:

Traceback (most recent call last):
  File "/home/bdyetton/PSleep/src/modeling/run_models.py", line 323, in <module>
    test_ppc_issue()
  File "/home/bdyetton/PSleep/src/modeling/run_models.py", line 282, in test_ppc_issue
    pm.sample_posterior_predictive(trace) #NoneConst error here.
  File "/home/bdyetton/anaconda3/envs/psleep/lib/python3.7/site-packages/pymc3/sampling.py", line 1167, in sample_posterior_predictive
    values = draw_values(vars, point=param, size=size)
  File "/home/bdyetton/anaconda3/envs/psleep/lib/python3.7/site-packages/pymc3/distributions/distribution.py", line 627, in draw_values
    size=size)
  File "/home/bdyetton/anaconda3/envs/psleep/lib/python3.7/site-packages/pymc3/distributions/distribution.py", line 817, in _draw_value
    size=None))
  File "/home/bdyetton/anaconda3/envs/psleep/lib/python3.7/site-packages/pymc3/distributions/continuous.py", line 495, in random
    point=point, size=size)
  File "/home/bdyetton/anaconda3/envs/psleep/lib/python3.7/site-packages/pymc3/distributions/distribution.py", line 627, in draw_values
    size=size)
  File "/home/bdyetton/anaconda3/envs/psleep/lib/python3.7/site-packages/pymc3/distributions/distribution.py", line 851, in _draw_value
    raise ValueError('Unexpected type in draw_value: %s' % type(param))
ValueError: Unexpected type in draw_value: <class 'theano.gof.graph.Constant'>

Versions and main components

  • PyMC3 Version: Master (8/18/19)
  • Theano Version: 1.0.4
  • Python Version: 3.7
  • Operating system: Windows 10
  • How did you install PyMC3: pip

I also asked this question on discourse

@lucianopaz
Copy link
Contributor

Thanks for reporting! The problem is that _draw_value does some type checks but we did not consider theano.gof.graph.Constants. It should be easy to fix but in the mean time, your model can work perfectly if you just drop the colon when you create indexed_rv like this:

indexed_rv = multi_dim_rv[data['x'], data['g']]  # The colon : is implicit

@bdyetton
Copy link
Contributor Author

Ah very cool, thanks!

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

Successfully merging a pull request may close this issue.

2 participants