Initialization failed in Pystan

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

as well as how my data looks like

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

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!

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

It looks like you’re not using Python 2.7, so judging from PyStan’s documentation , 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.)

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

1 Like