# Bad initial energy: inf or nan

**URL:** <https://discourse.pymc.io/t/bad-initial-energy-inf-or-nan/1036>\
**Category:** Questions\
**Created:** [March 30, 2018, 4:10pm UTC](https://discourse.pymc.io/t/bad-initial-energy-inf-or-nan/1036 "2018-03-30T16:10:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevemju](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@stevemju](https://discourse.pymc.io/u/stevemju)\
**Post date:** [March 30, 2018, 4:10pm UTC](https://discourse.pymc.io/t/bad-initial-energy-inf-or-nan/1036/1 "2018-03-30T16:10:04Z")

</div>

Hello, by running the code below multiple times, all things fixed, it can either compile successfully (mostly when we limit the size of train), or sometimes it triggers a ValueError: Bad initial energy: inf, or a ValueError: Bad initial energy: nan.  
Here is the code:

train = df.head(100)

```
with pm.Model() as model:
    BoundedNormal = pm.Bound(pm.Normal, lower=-10, upper=10)

    theta_sd = pm.InverseGamma('theta_sd', alpha=1 + 50, beta=2 + 50)
    theta_not_centered = BoundedNormal('theta_not_centered', mu=0, sd=theta_sd,
                                       shape=len(train.student_id.unique()))
    theta = pm.Deterministic('theta', theta_not_centered - tt.mean(theta_not_centered))

    alpha = pm.Lognormal('alpha', mu=0.5, sd=1, shape=100)

    beta_not_centered = BoundedNormal('beta_not_centered', mu=0, sd=1,
                                      shape=100)
    beta = pm.Deterministic('beta', beta_not_centered - tt.mean(beta_not_centered))

    lesson_not_centered = BoundedNormal('lesson_not_centered', mu=0, sd=1, shape=400)
    lesson = pm.Deterministic('lesson', lesson_not_centered - tt.mean(lesson_not_centered))

    intercept = BoundedNormal('intercept', mu=0, sd=1)

    theta_for_each_rows = theta[list(train['student_code'].values)]
    alpha_for_each_rows = alpha[list(train['exercise_with_level_code'].values)]
    beta_for_each_rows = beta[list(train['exercise_with_level_code'].values)]
    lesson_for_each_rows = lesson[list(train['lesson_code'].values)]

    linear_component = alpha_for_each_rows * (theta_for_each_rows - beta_for_each_rows) + lesson_for_each_rows + intercept

    proba_without_left_asymptot = pm.Deterministic('proba_without_left_asymptot', pm.math.sigmoid(linear_component))
    proba = pm.Deterministic('proba', train['min_success_rate'].values + (
            1 - train['min_success_rate'].values) * proba_without_left_asymptot)

    result = pm.Bernoulli('result', p=proba, observed=train['correctness'])

    trace = pm.sample()

```

Post on similar subject didn’t help us debug it. Especially, running

```
for RV in model.basic_RVs:
    print(RV.name, RV.logp(model.test_point))

```

didn’t show unusual values.

Thanks for your help!

---

<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:** [March 30, 2018, 4:27pm UTC](https://discourse.pymc.io/t/bad-initial-energy-inf-or-nan/1036/2 "2018-03-30T16:27:55Z")

</div>

This issue has been noticed a couple of times (eg. [Limit or prevent unrealistic output of neural network](https://discourse.pymc.io/t/limit-or-prevent-unrealistic-output-of-neural-network/871/4?u=junpenglao)). The reason is that jitter sometimes makes the initial value goes out of the support of the logp. We are working to make this more robust.

For now, you can set `init=adapt_diag` to avoid this problem.
