# Seeding issues when using model with Dirichlet and Gamma

**URL:** <https://discourse.pymc.io/t/seeding-issues-when-using-model-with-dirichlet-and-gamma/2249>\
**Category:** Questions\
**Created:** [November 20, 2018, 9:13pm UTC](https://discourse.pymc.io/t/seeding-issues-when-using-model-with-dirichlet-and-gamma/2249 "2018-11-20T21:13:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fullofpy](https://avatars.discourse-cdn.com/v4/letter/f/f0a364/32.png) [@fullofpy](https://discourse.pymc.io/u/fullofpy)\
**Post date:** [November 20, 2018, 9:13pm UTC](https://discourse.pymc.io/t/seeding-issues-when-using-model-with-dirichlet-and-gamma/2249/1 "2018-11-20T21:13:27Z")

</div>

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:

```python
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!

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [November 21, 2018, 6:00am UTC](https://discourse.pymc.io/t/seeding-issues-when-using-model-with-dirichlet-and-gamma/2249/2 "2018-11-21T06:00:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fullofpy](https://avatars.discourse-cdn.com/v4/letter/f/f0a364/32.png) [@fullofpy](https://discourse.pymc.io/u/fullofpy)\
**Post date:** [November 21, 2018, 1:47pm UTC](https://discourse.pymc.io/t/seeding-issues-when-using-model-with-dirichlet-and-gamma/2249/3 "2018-11-21T13:47:06Z")

</div>

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!
