KeyError: salmon_pred ~ MvNormal

When I try the example code in gp_regression/spawning_salmon.ipynb at master · fonnesbeck/gp_regression · GitHub

I got the following error when execute this:

with gp_salmon_model:
salmon_pred = recruit_gp.conditional(“salmon_pred”, X_pred.reshape(-1, 1))
gp_salmon_samples = pm.sample_posterior_predictive(gp_trace, var_names=[salmon_pred], samples=3, random_seed=42)

KeyError: salmon_pred ~ MvNormal

KeyError Traceback (most recent call last)
~/installers/installed/lib/python3.8/site-packages/pymc3/model.py in getitem(self, key)
1235 try:
→ 1236 return self.named_vars[self.name_for(key)]
1237 except KeyError:

KeyError: salmon_pred ~ MvNormal

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
in
1 with gp_salmon_model:
2 salmon_pred = recruit_gp.conditional(“salmon_pred”, X_pred.reshape(-1, 1))
----> 3 gp_salmon_samples = pm.sample_posterior_predictive(gp_trace, var_names=[salmon_pred], samples=3, random_seed=42)

~/installers/installed/lib/python3.8/site-packages/pymc3/sampling.py in sample_posterior_predictive(trace, samples, model, var_names, size, keep_size, random_seed, progressbar)
1703
1704 if var_names is not None:
→ 1705 vars_ = [model[x] for x in var_names]
1706 else:
1707 vars_ = model.observed_RVs

~/installers/installed/lib/python3.8/site-packages/pymc3/sampling.py in (.0)
1703
1704 if var_names is not None:
→ 1705 vars_ = [model[x] for x in var_names]
1706 else:
1707 vars_ = model.observed_RVs

~/installers/installed/lib/python3.8/site-packages/pymc3/model.py in getitem(self, key)
1236 return self.named_vars[self.name_for(key)]
1237 except KeyError:
→ 1238 raise e
1239
1240 def makefn(self, outs, mode=None, *args, **kwargs):

~/installers/installed/lib/python3.8/site-packages/pymc3/model.py in getitem(self, key)
1231 def getitem(self, key):
1232 try:
→ 1233 return self.named_vars[key]
1234 except KeyError as e:
1235 try:

KeyError: salmon_pred ~ MvNormal

I think you just need to put

gp_salmon_samples = pm.sample_posterior_predictive(gp_trace, var_names=["salmon_pred"], samples=3, random_seed=42)

instead of

gp_salmon_samples = pm.sample_posterior_predictive(gp_trace, var_names=[salmon_pred], samples=3, random_seed=42)

i.e. the var_names kwarg should take a list of the string names of the variables and not the variables themselves.

It worked with your suggestion.

Thanks a lot!

Frank