# Initialization failed

**URL:** <https://discourse.mc-stan.org/t/initialization-failed/4713>\
**Category:** Modeling\
**Tags:** fitting-issues\
**Created:** [July 2, 2018, 5:34pm UTC](https://discourse.mc-stan.org/t/initialization-failed/4713 "2018-07-02T17:34:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasmine2018jixun](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jasmine2018jixun/32/2714_2.png) [@jasmine2018jixun](https://discourse.mc-stan.org/u/jasmine2018jixun)\
**Post date:** [July 2, 2018, 5:34pm UTC](https://discourse.mc-stan.org/t/initialization-failed/4713/1 "2018-07-02T17:34:33Z")

</div>

I am trying to fit the hierarchical bayesian logistic model using pystan:  
#construct the data block  
bmtl\_code = “”"  
data {  
int\<lower=0\> N; //num observations  
int\<lower=1\> J; // num predicators  
int\<lower=1\> K; //num tasks  
int\<lower=1, upper=K\> kk[N]; //task label for each observation  
matrix[N,J] x; //observation predicators  
int y[N]; //observation outcomes  
matrix[J,K] Zero;  
}

parameters {  
vector[K] alpha; //intercept alpha  
matrix[J, K] beta; //coefficients beta  
corr\_matrix[K] Omega; //prior correlation  
vector\<lower=0\>[K] sigma; //prior scale  
real\<lower=0\> tau;  
vector\<lower=0\>[J] psi;  
}

transformed parameters {  
vector[J] r; //shrinkage scalar  
vector[J] r\_square;  
r = tau\*psi;  
for (j in 1:J)  
r\_square[j]=r[j]\*r[j];  
}

model {  
psi ~ cauchy(0,1);  
tau ~ cauchy(0,1);

alpha ~ cauchy(0,10);  
sigma ~ cauchy(0,2.5);  
Omega ~ lkj\_corr(1);  
for (j in 1:J)  
beta[j] ~ multi\_normal(Zero[j], r\_square[j] \* quad\_form\_diag(Omega, sigma));

for (n in 1:N)  
y[n] ~ bernoulli(inv\_logit(alpha[kk[n]] + dot\_product(x[n],beta[,kk[n]])));  
}  
“”"  
bmtl\_data = dict(N=X\_train.shape[0], J=num\_features, K=num\_tasks,  
kk=df\_train[‘label’].values, x=X\_train, y=y\_train,  
Zero=np.zeros((num\_features,num\_tasks)))

fit = pystan.stan(model\_code=bmtl\_code, data=bmtl\_data)

## I ran the code above and it returned the following message:

RemoteTraceback Traceback (most recent call last)  
RemoteTraceback:  
“”"  
Traceback (most recent call last):  
File “/anaconda3/lib/python3.6/multiprocessing/pool.py”, line 119, in worker  
result = (True, func(\*args, \*\*kwds))  
File “/anaconda3/lib/python3.6/multiprocessing/pool.py”, line 44, in mapstar  
return list(map(\*args))  
File “stanfit4anon\_model\_6f6086b057a6e799617a8be0e0bc8dce\_4816030848099204205.pyx”, line 368, in stanfit4anon\_model\_6f6086b057a6e799617a8be0e0bc8dce\_4816030848099204205.\_call\_sampler\_star  
File “stanfit4anon\_model\_6f6086b057a6e799617a8be0e0bc8dce\_4816030848099204205.pyx”, line 401, in stanfit4anon\_model\_6f6086b057a6e799617a8be0e0bc8dce\_4816030848099204205.\_call\_sampler  
RuntimeError: Initialization failed.  
“”"

The above exception was the direct cause of the following exception:

RuntimeError Traceback (most recent call last)  
 in ()  
3 Zero=np.zeros((num\_features,num\_tasks)))  
4  
----\> 5 fit = pystan.stan(model\_code=bmtl\_code, data=bmtl\_data)

/anaconda3/lib/python3.6/site-packages/pystan/api.py in stan(file, model\_name, model\_code, fit, data, pars, chains, iter, warmup, thin, init, seed, algorithm, control, sample\_file, diagnostic\_file, verbose, boost\_lib, eigen\_lib, n\_jobs, \*\*kwargs)  
400 sample\_file=sample\_file, diagnostic\_file=diagnostic\_file,  
401 verbose=verbose, algorithm=algorithm, control=control,  
–\> 402 n\_jobs=n\_jobs, \*\*kwargs)  
403 return fit

/anaconda3/lib/python3.6/site-packages/pystan/model.py in sampling(self, data, pars, chains, iter, warmup, thin, seed, init, sample\_file, diagnostic\_file, verbose, algorithm, control, n\_jobs, \*\*kwargs)  
724 call\_sampler\_args = izip(itertools.repeat(data), args\_list, itertools.repeat(pars))  
725 call\_sampler\_star = self.module.\_call\_sampler\_star  
–\> 726 ret\_and\_samples = \_map\_parallel(call\_sampler\_star, call\_sampler\_args, n\_jobs)  
727 samples = [smpl for \_, smpl in ret\_and\_samples]  
728

/anaconda3/lib/python3.6/site-packages/pystan/model.py in \_map\_parallel(function, args, n\_jobs)  
79 try:  
80 pool = multiprocessing.Pool(processes=n\_jobs)  
—\> 81 map\_result = pool.map(function, args)  
82 finally:  
83 pool.close()

/anaconda3/lib/python3.6/multiprocessing/pool.py in map(self, func, iterable, chunksize)  
264 in a list that is returned.  
265 ‘’’  
–\> 266 return self.\_map\_async(func, iterable, mapstar, chunksize).get()  
267  
268 def starmap(self, func, iterable, chunksize=None):

/anaconda3/lib/python3.6/multiprocessing/pool.py in get(self, timeout)  
642 return self.\_value  
643 else:  
–\> 644 raise self.\_value  
645  
646 def \_set(self, i, obj):

RuntimeError: Initialization failed.

I am new to pystan and really not sure how to debug.

Any help would be greatly appreciated!  
Jasmine

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [July 2, 2018, 6:11pm UTC](https://discourse.mc-stan.org/t/initialization-failed/4713/2 "2018-07-02T18:11:31Z")

</div>

Use `bernoulli_logit(eta)` rather than `bernoulli(inv_logit(eta))`. Also, you would be much better served to fill in a `vector[N] eta` like

```
for (n in 1:N) {
  eta[n] = alpha[kk[n]] + dot_product(x[n],beta[,kk[n]];
}

```

and then evaluate the log-likelihood once with

```
y ~ bernoulli_logit(eta);

```

---

<div class="post-metadata">

**Author:** ![jasmine2018jixun](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jasmine2018jixun/32/2714_2.png) [@jasmine2018jixun](https://discourse.mc-stan.org/u/jasmine2018jixun)\
**Post date:** [July 3, 2018, 1:04pm UTC](https://discourse.mc-stan.org/t/initialization-failed/4713/3 "2018-07-03T13:04:10Z")

</div>

Thanks for the quick reply.  
I have modified my code as you suggest and it seems initializing successfully. However, the code has run for 3 hours without any error or output. There are 64612 observations and 24 features in my model. Without any prompt information, I don’t know how far does my code run. I am wondering if something wrong in my model.

My code:  
#construct the data block  
bmtl\_code = “”"  
data {  
int\<lower=0\> N; //num observations  
int\<lower=1\> J; // num predicators  
int\<lower=1\> K; //num tasks  
int\<lower=1, upper=K\> kk[N]; //task label for each observation  
matrix[N,J] x; //observation predicators  
int y[N]; //observation outcomes  
matrix[J,K] Zero;  
}

parameters {  
vector[K] alpha; //intercept alpha  
matrix[J, K] beta; //coefficients beta  
corr\_matrix[K] Omega; //prior correlation  
vector\<lower=0\>[K] sigma; //prior scale  
real\<lower=0\> tau;  
vector\<lower=0\>[J] psi;  
}

transformed parameters {  
vector[J] r; //shrinkage scalar  
vector[J] r\_square;  
vector[N] eta;  
r = tau\*psi;  
for (j in 1:J){  
r\_square[j]=r[j]\*r[j];  
}  
for (n in 1:N){  
eta[n]=alpha[kk[n]] + dot\_product(x[n],beta[,kk[n]]);  
}  
}

model {  
psi ~ cauchy(0,1);  
tau ~ cauchy(0,1);

alpha ~ cauchy(0,10);  
sigma ~ cauchy(0,2.5);  
Omega ~ lkj\_corr(1);  
for (j in 1:J){  
beta[j] ~ multi\_normal(Zero[j], r\_square[j] \* quad\_form\_diag(Omega, sigma));  
}  
y ~ bernoulli\_logit(eta);  
}  
“”"  
bmtl\_data = dict(N=X\_train.shape[0], J=num\_features, K=num\_tasks,  
kk=df\_train[‘label’].values, x=X\_train, y=y\_train,  
Zero=np.zeros((num\_features,num\_tasks)))

fit = pystan.stan(model\_code=bmtl\_code, data=bmtl\_data)

Running message:  
INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon\_model\_4a5092a673047c964a13bcadc3583b74 NOW.  
In file included from /var/folders/vn/sc2r7p\_1557\_lflrw2r6jg740000gn/T/tmpol8xa1rx/stanfit4anon\_model\_4a5092a673047c964a13bcadc3583b74\_2693544027111664653.cpp:599:  
In file included from /anaconda3/lib/python3.6/site-packages/numpy/core/include/numpy/arrayobject.h:4:  
In file included from /anaconda3/lib/python3.6/site-packages/numpy/core/include/numpy/ndarrayobject.h:18:  
In file included from /anaconda3/lib/python3.6/site-packages/numpy/core/include/numpy/ndarraytypes.h:1816:  
/anaconda3/lib/python3.6/site-packages/numpy/core/include/numpy/npy\_1\_7\_deprecated\_api.h:15:2: warning: "Using deprecated NumPy API, disable it by " “#defining NPY\_NO\_DEPRECATED\_API NPY\_1\_7\_API\_VERSION” [-W#warnings]  
#warning "Using deprecated NumPy API, disable it by "   
^  
/var/folders/vn/sc2r7p\_1557\_lflrw2r6jg740000gn/T/tmpol8xa1rx/stanfit4anon\_model\_4a5092a673047c964a13bcadc3583b74\_2693544027111664653.cpp:9155:30: warning: comparison of integers of different signs: ‘Py\_ssize\_t’ (aka ‘long’) and ‘std::\_\_1::vector\<std::\_\_1::basic\_string, std::\_\_1::allocator\<std::\_\_1::basic\_string \> \>::size\_type’ (aka ‘unsigned long’) [-Wsign-compare]  
\_\_pyx\_t\_12 = ((\_\_pyx\_t\_9 != \_\_pyx\_v\_fitptr-\>param\_names\_oi().size()) != 0);  
~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
In file included from /var/folders/vn/sc2r7p\_1557\_lflrw2r6jg740000gn/T/tmpol8xa1rx/stanfit4anon\_model\_4a5092a673047c964a13bcadc3583b74\_2693544027111664653.cpp:603:  
In file included from /anaconda3/lib/python3.6/site-packages/pystan/stan\_fit.hpp:22:  
In file included from /anaconda3/lib/python3.6/site-packages/pystan/stan/src/stan/services/diagnose/diagnose.hpp:10:  
In file included from /anaconda3/lib/python3.6/site-packages/pystan/stan/src/stan/model/test\_gradients.hpp:7:  
In file included from /anaconda3/lib/python3.6/site-packages/pystan/stan/src/stan/model/log\_prob\_grad.hpp:4:  
In file included from /anaconda3/lib/python3.6/site-packages/pystan/stan/lib/stan\_math/stan/math/rev/mat.hpp:12:  
In file included from /anaconda3/lib/python3.6/site-packages/pystan/stan/lib/stan\_math/stan/math/prim/mat.hpp:298:  
In file included from /anaconda3/lib/python3.6/site-packages/pystan/stan/lib/stan\_math/stan/math/prim/arr.hpp:38:  
In file included from /anaconda3/lib/python3.6/site-packages/pystan/stan/lib/stan\_math/stan/math/prim/arr/functor/integrate\_ode\_rk45.hpp:17:  
In file included from /anaconda3/lib/python3.6/site-packages/pystan/stan/lib/stan\_math/lib/boost\_1.64.0/boost/numeric/odeint.hpp:61:  
In file included from /anaconda3/lib/python3.6/site-packages/pystan/stan/lib/stan\_math/lib/boost\_1.64.0/boost/numeric/odeint/util/multi\_array\_adaption.hpp:29:  
In file included from /anaconda3/lib/python3.6/site-packages/pystan/stan/lib/stan\_math/lib/boost\_1.64.0/boost/multi\_array.hpp:21:  
In file included from /anaconda3/lib/python3.6/site-packages/pystan/stan/lib/stan\_math/lib/boost\_1.64.0/boost/multi\_array/base.hpp:28:  
/anaconda3/lib/python3.6/site-packages/pystan/stan/lib/stan\_math/lib/boost\_1.64.0/boost/multi\_array/concept\_checks.hpp:42:43: warning: unused typedef ‘index\_range’ [-Wunused-local-typedef]  
typedef typename Array::index\_range index\_range;  
^  
/anaconda3/lib/python3.6/site-packages/pystan/stan/lib/stan\_math/lib/boost\_1.64.0/boost/multi\_array/concept\_checks.hpp:43:37: warning: unused typedef ‘index’ [-Wunused-local-typedef]  
typedef typename Array::index index;  
^  
/anaconda3/lib/python3.6/site-packages/pystan/stan/lib/stan\_math/lib/boost\_1.64.0/boost/multi\_array/concept\_checks.hpp:53:43: warning: unused typedef ‘index\_range’ [-Wunused-local-typedef]  
typedef typename Array::index\_range index\_range;  
^  
/anaconda3/lib/python3.6/site-packages/pystan/stan/lib/stan\_math/lib/boost\_1.64.0/boost/multi\_array/concept\_checks.hpp:54:37: warning: unused typedef ‘index’ [-Wunused-local-typedef]  
typedef typename Array::index index;  
^  
6 warnings generated.  
/anaconda3/lib/python3.6/site-packages/pystan/misc.py:399: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.  
elif np.issubdtype(np.asarray(v).dtype, float):

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [July 3, 2018, 3:47pm UTC](https://discourse.mc-stan.org/t/initialization-failed/4713/4 "2018-07-03T15:47:09Z")

</div>

> [@jasmine2018jixun](#):
>
> the code has run for 3 hours without any error or output

As you’ve figured out, there’s probably some funky with your model or data :P. The way to figure out what is going on is to start with small amounts of simulated data.

Simulate from your model and see if you recover the parameters you used to generate things. Start with really small bits of data – enough so that you can run your model in less than a minute. Waiting three hours to realize there is a bug is pretty slow :P. You’ll have to take your time on this.

Non-identified parameters or giant correlations in your output can cause these slowdowns. Pairplots (like these: [seaborn.pairplot — seaborn 0.13.0 documentation](https://seaborn.pydata.org/generated/seaborn.pairplot.html)) can be really helping for finding problems.
