# Initialization failed in Pystan

**URL:** <https://discourse.mc-stan.org/t/initialization-failed-in-pystan/11099>\
**Category:** Modeling\
**Created:** [September 24, 2019, 1:45pm UTC](https://discourse.mc-stan.org/t/initialization-failed-in-pystan/11099 "2019-09-24T13:45:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![erfpak7](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/erfpak7/32/5932_2.png) [@erfpak7](https://discourse.mc-stan.org/u/erfpak7)\
**Post date:** [September 24, 2019, 1:45pm UTC](https://discourse.mc-stan.org/t/initialization-failed-in-pystan/11099/1 "2019-09-24T13:45:00Z")

</div>

I’m trying to make a model in Pystan on my data. But, I keep receiving the initialization failed error. I have checked all the previous questions and the data format for me is changed to np.asarray

model\_code\_bel\_copy = “”"  
functions {  
real sigmoid(real x, real g) {  
return 1.0/(1.0 + exp(-g\*x));  
}  
real sigmoid\_simple(real x) {  
return 1.0/(1.0 + exp(-x));  
}  
}  
data {  
int\<lower=0\> N;  
int action[N];  
int item[N];  
}

parameters {  
real\<lower=0, upper=1\> bel\_prob[3];  
}  
transformed parameters {  
real V[N];  
real act[N];  
for (i in 1:N) {

```
    V[i] = bel_prob[item[i]]; 
    act[i] = sigmoid_simple(V[i]);
}

```

}

model {  
action ~ bernoulli(act);  
}

“”"

Here is my 10 fold cross validation on it the model where I get the initialization error while fitting the data on the model

# K fold cross validation

K = 10  
kf = KFold(n\_splits=K)  
kf.get\_n\_splits(actions)

train\_loglikelihood = []  
train\_loglikelihood\_sum = []  
test\_loglikelihood = []  
test\_loglikelihood\_sum = []

train\_auc\_score = []  
test\_auc\_score = []

for train\_index, test\_index in kf.split(actionTmp):  
actions\_train, actions\_test = actions[train\_index], actions[test\_index]  
beliefs\_train, beliefs\_test = beliefs[train\_index], beliefs[test\_index]  
items\_train, items\_test = items[train\_index], items[test\_index]  
#trusts\_train, trusts\_test = trusts[train\_index], trusts[test\_index]

```
model_data_bel = {
    'N' : len(actions_train),
    'action': actions_train,
    'item': items_train
}

fit = pystan.stan(model_code=model_code_bel_copy, data=model_data_bel,iter=2000, chains=4, n_jobs= 1)

```

In case there is a need for the error and

 ![Stan](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/0/01c458a05cc11c67ed21e44885ec1341f8168c3a.jpeg)

as well as how my data looks like

 ![Stan2](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/a/a68c7bf5e590004214c68d58cf5c82bd6ae29769.jpeg)

Any help on how I can fix it is greatly appreciated. Thanks

---

<div class="post-metadata">

**Author:** ![JimBob](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jimbob/32/19329_2.png) [@JimBob](https://discourse.mc-stan.org/u/JimBob)\
**Post date:** [September 24, 2019, 2:14pm UTC](https://discourse.mc-stan.org/t/initialization-failed-in-pystan/11099/2 "2019-09-24T14:14:17Z")

</div>

Total beginner here, so I cannot comment on whether there might be a general coding error, but I have also come across initialisation failure for binary outcome or ordinal models. In R and brms, it is possible to set the ‘inits’ or initialisation to ‘0’ to begin with. This often solves initialisation problems for what I have done.

I wonder if you can add ‘inits = 0’ to “fit = pystan.stan(model\_code=model\_code\_bel\_copy, data=model\_data\_bel,iter=2000, chains=4, n\_jobs= 1)” , after you specify the data and before you specify the number of iterations. It’s at least worth a try before people who know what they are talking about can comment!

---

<div class="post-metadata">

**Author:** ![erfpak7](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/erfpak7/32/5932_2.png) [@erfpak7](https://discourse.mc-stan.org/u/erfpak7)\
**Post date:** [September 24, 2019, 2:28pm UTC](https://discourse.mc-stan.org/t/initialization-failed-in-pystan/11099/3 "2019-09-24T14:28:16Z")

</div>

Thanks for the response.  
I’ve tried and the error is still there!

---

<div class="post-metadata">

**Author:** ![jjramsey](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jjramsey/32/3319_2.png) [@jjramsey](https://discourse.mc-stan.org/u/jjramsey)\
**Post date:** [September 24, 2019, 2:45pm UTC](https://discourse.mc-stan.org/t/initialization-failed-in-pystan/11099/4 "2019-09-24T14:45:51Z")

</div>

It looks like you’re not using Python 2.7, so judging from [PyStan’s documentation](https://pystan.readthedocs.io/en/latest/windows.html), you _should_ be able to avoid the “`n_jobs = 1`” argument.

(I’m making a crude guess that there may be a problem triggered by setting the `n_jobs` argument to 1, though that’s a stab in the dark.)

---

<div class="post-metadata">

**Author:** ![erfpak7](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/erfpak7/32/5932_2.png) [@erfpak7](https://discourse.mc-stan.org/u/erfpak7)\
**Post date:** [September 24, 2019, 3:28pm UTC](https://discourse.mc-stan.org/t/initialization-failed-in-pystan/11099/5 "2019-09-24T15:28:24Z")

</div>

Problem is Bernoli needs 0 and 1, it does not work on any other binary values! Once I changed the actions values to 0 and 1, I could make it work
