How to save and load trace

I tried doing what @junpenglao and @colcarroll pointed to at https://github.com/pymc-devs/pymc3/pull/2975 but I keep getting errors.

My model:

with pm.Model() as mG:
    alpha = pm.Normal(...)
    beta = [...]
    sigma = [...]
    mu =[...]
    Ages = pm.Normal(...)
    
    trace_mG = pm.sample(2000, tune=1000, random_seed=13)

Saving trace:

trace_1 = pm.save_trace(trace_mG)

Loading trace:

with mG:
    trace2 = pm.load_trace(trace_1)

The error I get:

AttributeError                            Traceback (most recent call last)
<ipython-input-201-ecc0fb6c7706> in <module>()
      1 # rr = pm.load_trace('m', model=mG)
----> 2 with mG:
      3     trace2 = pm.load_trace(trace_1)
AttributeError: __enter__

I also tried loading like this:

trace2 = pm.load_trace('.pymc_1.trace', model=mG)

But the error I get is:

AttributeError: 'str' object has no attribute 'unobserved_RVs'

Anyone know how to do this?

1 Like

It appears that no one knows the answer.

Should I change the tag to Development?

I am surprised by the error - could you please open an issue on Github with some simple examples?

I can’t reproduce the error, and for some reason now it works fine. I didn’t even restart my computer and did exactly the same thing. Not sure what the problem was; perhaps a bug in my computer.

Maybe we should still leave this closed question up here in case this happens to anyone else.

Thank you @junpenglao

1 Like

Thanks for the reply. @michaelosthege called these kind of bugs Heisenbug which is pretty fitting :joy:

@junpenglao, After I restart my computer and re-open my jupyter notebook, I tried loading the saved trace:

with model_1:
    trace_1 = pm.load_trace(".pymc_1.trace")

But it says it doesn’t recognize the model. Do I have to re-run my model? If so, what is save_trace usually used for, considering that you have to run the whole model again?

You do need to reinitialized the model, meaning that you should run the same model code, excluding the pm.sample(...) line.

1 Like

For what it is worth, I have found this to be a good pattern in pymc3 - to separate the modelling step from the sampling step. I even will often define a model in a function, and do things like


def my_model():
    with pm.Model() as model:
        x = pm.Normal('x')
        ...
    return model

with my_model():
    other stuff
3 Likes

This is a good idea. Separation between model building and inference is a good way to avoid confusion, we should enforce it better in PyMC4.

2 Likes

Hi all,

I tried to load my trace as well, but it came with ‘NoneType’ error.
I followed what suggested here but no avail.

the last several lines of my code looks as follows:

with pm.Model() as model:
(some indentation) …
(some indentation) y_ = gp.marginal_likelihood(‘y’, X=Xs, Xu=Xu, y=Ys, noise=sigma)
(some indentation) # approx = pm.ADVI().fit(n=5000)
(some indentation) # trace = approx.sample(1000)
trace = pm.load_trace(’.pymc_1.trace’, model=model)

Did I miss something to load?

Thanks

1 Like

You can go through this thread, where I had pretty much the same problem with saving at some point. Tell me if the advice there ends up working for you.