'Unknown variable energy' error when calling `pm.bfmi(trace)`

Hi, I get this error 'Unknown variable energy' when calling pm.bfmi(trace) on a trace variable that I sampled with trace=backend where backend is a Text() backend. This is not the reloaded trace from disk, but the original object returned by pm.sample. Same error on the trace loaded from disk. Any ideas what I’m doing wrong here, or is this a bug?

with my_model:
    backend = pm.backends.Text("traces")
    trace = pm.sample(**SAMPLE_KWARGS, trace=backend)
pm.bfmi(trace)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-70-69aaad1a469d> in <module>()
----> 1 pm.bfmi(trace)

~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/pymc3/stats.py in bfmi(trace)
   1056         The Bayesian fraction of missing information of the model and trace.
   1057     """
-> 1058     energy = trace['energy']
   1059 
   1060     return np.square(np.diff(energy)).mean() / np.var(energy)

~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/pymc3/backends/base.py in __getitem__(self, idx)
    317         if var in self.stat_names:
    318             return self.get_sampler_stats(var, burn=burn, thin=thin)
--> 319         raise KeyError("Unknown variable %s" % var)
    320 
    321     _attrs = set(['_straces', 'varnames', 'chains', 'stat_names',

KeyError: 'Unknown variable energy'

Text backend does not have all the information saved. Text backend will be deprecated and replace by xarray backend soon, so things should improve in the future.

Got it, thx!

1 Like