Skip to content

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

Closed
@bdyetton

Description

@bdyetton

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions