Seeding issues when using model with Dirichlet and Gamma

Hi!

I’ve been having trouble getting consistent results with the rng seeded and it only seems to happen when the model has a Gamma random variable defined after a Dirichlet random variable. This also only happens in Python 2.7 though – seeding seems to give me consistent results on Python 3.5. I’ve distilled it to this basic model:

import pymc3 as pm
import numpy as np
seed = 383561
with pm.Model() as model:
    p = pm.Dirichlet('w', np.ones(2))
    tau = pm.Gamma('tau', 1., 1.)    
    trace = pm.sample(1, tune=10, chains=1, random_seed=seed)

print(trace['tau'])

I end up getting either [1.06999556] or [2.30071916].

Strangely enough, this only occurs when I define the Dirichlet before the Gamma. If the Gamma is defined before the Dirichlet, then the numbers are deterministic (as far as I can tell!) and consistent with the results when run on Python 3.5 (consistently get [1.06999556])

Also, running pm.sample(1, tune=10, chains=1, random_seed=seed) itself repeatedly yields a consistent result. It’s only when repeatedly rebuilding the whole model that different results come out.

Setting the random seed (e.g. with np.random.seed(seed) ) before the model doesn’t seem to affect anything.

I’m still really new to PyMC3 and Python in general, but I haven’t been able to find an answer to this just yet. Thanks for reading and considering this issue!

Since you are new to Python and PyMC3, I would suggest you to do everything under python3 - we will sunset the support of py2.7 next year, so likely we are not going to spend time debugging this.

One possible reason is that, in your code you are doing tune=10, so there are 10 values before you finally do print(trace['tau']). You can try trace = pm.sample(1, tune=10, chains=1, random_seed=seed, discard_tuned_samples=False) and check whether the first value is the same after setting seed. It could be that the first value is the same but somewhere the metropolis acceptance diverge.

Ok! I’ve had a look at the first value and it looks like it is just as inconsistent, so it doesn’t look like it’s an issue with the acceptance?

Yeah I think it’d make a lot of sense for me to focus on Python 3.

Thanks a lot for taking the time to reply!