Posterior predictive sampling with shared matrix

Hello,
I was trying to predict values for unseen data using a regression model, e.g. Y = \alpha + X \beta with Y, X and \beta being matrices of sizes (n,1), (n,m) and (m,1) respectively. I tried to use a shared variable for X. The model ran fine, however, changing X and using the sample_ppc function to predict values always led to an error. I then tried to reproduce the error with simpler models. The following model which is based on an example from https://docs.pymc.io/advanced_theano.html yields the same error.

x = np.random.randn(100)
y = x > 0

x_shared = theano.shared(x.reshape(-1,1))

with pm.Model() as model:
    coeff = pm.Normal('x', mu=0, sd=1)
    logistic = pm.math.sigmoid(coeff * x_shared)
    pm.Bernoulli('obs', p=logistic, observed=y.reshape(-1,1))

    # fit the model
    trace = pm.sample()

    # Switch out the observations and use `sample_posterior_predictive` to predict
    x_new = np.array([-1, 0, 1.]).reshape(-1,1)
    x_shared.set_value(x_new)
    post_pred = pm.sample_ppc(trace, samples=500)

The error from the sample_ppc is

TypeError: Attempted to generate values with incompatible shapes:
            size: 1
            dist_shape: (3,)
            broadcast_shape: (3, 1)

If I turn both the shared variable and the observed data into vectors by removing the reshape(-1,1) everything works. Am I missing something or is this not possible due to limited support of changing shapes?

Thanks!

Seems to be a bug - could you open an issue on Github?

Issue is now opened: #3180

1 Like