Pm.set_data throws error

Hello dear Bayesians!
I was trying to reproduce models from some of the model_builder tests, and I’ve encountered this situation:

with pm.Model() as model:
    x = pm.MutableData('x', data['input'].values)
    y_data = pm.MutableData('y_data', data['output'].values)
    
    a = pm.Normal('a', mu = 0, sigma = 10)
    b = pm.Normal('b', mu = 0, sigma = 10)
    obs_error = pm.HalfNormal('σ_model_fmc', sigma = 2)
    
    result = pm.Normal('y_model', mu = a+b*x, sigma = obs_error, shape = x.shape, observed = y_data)
    idata = pm.sample()

After this I assigned new x’s

x_pred = [1,2,3,4,5]
with model:
    pm.set_data({'x' : x_pred})

But quite fast I’ve realized that the test has different data structure passed as x, so I fixed it to reproduce it 1:1. So after quick fix my new x assignment looked like that:

x_pred2 = np.random.uniform(low=0, high=1, size=100)
prediction_data = pd.DataFrame({'input' : x_pred2})
with model:
    pm.set_data({'x' : prediction_data.input.values})

Which went all right, but when I tried to get predictions:

with model:
    idata = pm.sample_posterior_predictive(idata, predictions = True, extend_inferencedata=True)

I was given this error:


Is this desired behavior, that I can’t assign new values to the mutable data container without rebuilding the model? If I’d pass something of shape of the first x_pred, it works fine, otherwise it throws error

1 Like

Welcome!

If you are running this in a notebook, have you tried restarting the kernel and running everything from scratch? It may be the case that you have group in the idata called “posterior_predictive” and by calling pm.sample_posterior_predictive() with extend_inferencedata=True, it may be trying to extend the existing posterior predictive data (of one shape) with a new posterior predictive (of a new shape). But that’s just a guess.

Ah! Of course. I completely ignored the extend_inferencedata, and was failing why it was complaining about dimensions, I was convinced that maybe I’ve discovered some bug. Thank you!