Problem with Gaussian processes in v5

I understand how gaussian processes work in v3, but know I try to run it on v5,I got error, TypeError: sample_posterior_predictive() got an unexpected keyword argument 'vars' the problem seems comes from here,

with model_reg:
f_pred = gp.conditional(‘f_pred’, X_new)
with model_reg:
pred_samples = pm.sample_posterior_predictive(trace_reg, vars=[f_pred],samples=82)
_, ax = plt.subplots(figsize=(12,5))
ax.plot(X_new, pred_samples[‘f_pred’].T, ‘C1-’, alpha=0.3)
ax.plot(X, y, ‘ko’)
ax.set_xlabel(‘X’)

what should I do to transform them to v5:

np.random.seed(42)
x = np.random.uniform(0, 10, size=15)
y = np.random.normal(np.sin(x), 0.1)
plt.plot(x, y, 'o')
true_x = np.linspace(0, 10, 200)
plt.plot(true_x, np.sin(true_x), 'k--')
plt.xlabel('x')
plt.ylabel('f(x)', rotation=0)
#%%
# A one dimensional column vector of inputs.
X = x[:, None]

with pm.Model() as model_reg:
    # hyperprior for lengthscale kernel parameter
    ℓ = pm.Gamma('ℓ', 2, 0.5)
    # instanciate a covariance function
    cov = pm.gp.cov.ExpQuad(1, ls=ℓ)
    # instanciate a GP prior
    gp = pm.gp.Marginal(cov_func=cov)
    # prior
    ϵ = pm.HalfNormal('ϵ', 25)
    # likelihood
    y_pred = gp.marginal_likelihood('y_pred', X=X, y=y, sigma=ϵ)
    trace_reg = pm.sample(2000)
az.plot_trace(trace_reg)
X_new = np.linspace(np.floor(x.min()), np.ceil(x.max()), 100)[:,None]
with model_reg:
    f_pred = gp.conditional('f_pred', X_new)
with model_reg:
    pred_samples = pm.sample_posterior_predictive(trace_reg, vars=[f_pred],samples=82)
_, ax = plt.subplots(figsize=(12,5))
ax.plot(X_new, pred_samples['f_pred'].T, 'C1-', alpha=0.3)
ax.plot(X, y, 'ko')
ax.set_xlabel('X')
1 Like

Try var_names instead of vars, and you pass the name as a string instead of the variable itself

There’s still something wrong.

TypeError: sample_posterior_predictive() got an unexpected keyword argument ‘samples’

That argument is gone in V5. The number of samples is determined by the size of the inferencedata

Thanks.What about drawing, it seems pred_samples['f_pred'].T didn’t work?

It seems like you’re running into API differences between v3 and v5. The main one is that sampling functions return arviz InferenceData objects by default now. Here’s a thread discussing some of these changes that might be helpful.

I change my codes like this:

ax.plot(X_new, pred_samples.posterior['f_pred'], 'C1-', alpha=0.3)

but still get error

AttributeError: ‘InferenceData’ object has no attribute ‘posterior’
My purpose is to plot the fitted function on the original data to visually inspect its fit to the data and the uncertainty in the predictions,like this:


Here is my entire code

X = x[:, None]

with pm.Model() as model_reg:
    # hyperprior for lengthscale kernel parameter
    ℓ = pm.Gamma('ℓ', 2, 0.5)
    # instanciate a covariance function
    cov = pm.gp.cov.ExpQuad(1, ls=ℓ)
    # instanciate a GP prior
    gp = pm.gp.Marginal(cov_func=cov)
    # prior
    ϵ = pm.HalfNormal('ϵ', 25)
    # likelihood
    y_pred = gp.marginal_likelihood('y_pred', X=X, y=y, sigma=ϵ)
    trace_reg = pm.sample(2000)
az.plot_trace(trace_reg)
#%%
X_new = np.linspace(np.floor(x.min()), np.ceil(x.max()), 100)[:,None]
#%%
with model_reg:
    f_pred = gp.conditional('f_pred', X_new)
#%%
with model_reg:
#    pred_samples = pm.sample_posterior_predictive(f_pred)
    pred_samples = pm.sample_posterior_predictive(trace_reg, var_names=['f_pred'])
#%%
_, ax = plt.subplots(figsize=(12,5))
#ax.plot(X_new, pred_samples.posterior['f_pred'].T, 'C1-', alpha=0.3)
ax.plot(X_new, pred_samples.posterior['f_pred'], 'C1-', alpha=0.3)


ax.plot(X, y, 'ko')
ax.set_xlabel('X')```

Your pred_samples variable doesn’t have a posterior because you saved your posterior in trace_reg. The pred_samples variable only has the posterior predictive samples in it (in a group called posterior_predictive.

1 Like

Thanks, I change my code to ax.plot(X_new, pred_samples.posterior_predictive['f_pred'], 'C1-', alpha=0.3)when I try to plot it,I got error ValueError: x and y must have same first dimension, but have shapes (100, 1) and (4, 2000, 100)

That’s because your posterior predictive object has 4 (chains) x 2000 (draw) posterior predictive set of your 100 observations. I would suggest using arviz.plot_ppc().

Sorry,I’m not familiar with arviz, so I may ask silly questions.I just want to plot ,export, and save the result of predictive of my observations. Can you tell me the details I need to do to complies this? I’m working on m1 MacBook, it’s difficult for me to use v3 like before.

ArviZ is the standard method of visualizing the (ArviZ) InferenceData objects returned by py.sample(), sample_prior_predictive, pm.sample_posterior_predictive(), etc. (as well as traces, diagnostics, and a whole load of other things).