question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cross_val_score() does not terminate

See original GitHub issue

Description

Using KerasClassifier for the estimator function of cross_value_score() as shown in the following code,

classifier=KerasClassifier(build_fn=build_classifier,batch_size=10,epochs=100) accuracies=cross_val_score(estimator=classifier,X=X_train,y=y_train,cv=10,n_jobs=-1)

only the following four lines are printed:

Epoch 1/100 Epoch 1/100 Epoch 1/100 Epoch 1/100

and the process does not terminate.

Steps/Code to Reproduce

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score

def build_classifier():
    classifier = Sequential()
    classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 11))
    classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu'))
    classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
    classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
    return classifier

classifier=KerasClassifier(build_fn=build_classifier,batch_size=10,epochs=100)
accuracies=cross_val_score(estimator=classifier,X=X_train,y=y_train,cv=10,n_jobs=-1)

Expected Results

The process should print all the epochs and continue until completion, then terminate.

Actual Results

Epoch 1/100 Epoch 1/100 Epoch 1/100 Epoch 1/100

Versions

import platform; print(platform.platform()) Linux-4.10.0-33-generic-x86_64-with-debian-stretch-sid

import sys; print(“Python”, sys.version) Python 3.6.2 |Anaconda custom (64-bit)| (default, Jul 20 2017, 13:51:32) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

import numpy; print(“NumPy”, numpy.version) NumPy 1.13.3

import scipy; print(“SciPy”, scipy.version) SciPy 0.19.1

import sklearn; print(“Scikit-Learn”, sklearn.version) Scikit-Learn 0.19.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
bahariancommented, Oct 27, 2017

I have had issues that were similar to this, in the sense that the program would stop outputting things after a while. It turned out that some implementations (and/or algorithms) don’t like to be run in parallel when called from within a BaseSearchCV class (for me, it was AdaBoostClassifier and MLPClassifier). Have you tried a single-threaded run by removing n_jobs = -1 from your function call to cross_val_score()?

3reactions
AbhayKoushikcommented, Oct 28, 2017

@baharian and @amueller You are right, it is a parallelization issue. Thank you very much for your insights! n_jobs=1 does the job. How can we improve and debug this parallelization issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

kfold cross validation wont terminate, stuck at cross_val_score
I am trying to run kfold cross validation. but for some reason, it gets stuck here, it wont terminate ...
Read more >
Why are sklearn's cross_val_score values not increasing with ...
I am calling sklearn's cross_val_score to assess the performance of each model. I did plot the mean score ( cross_val_score(clf, X, y, cv=5)....
Read more >
3.1. Cross-validation: evaluating estimator performance
The simplest way to use cross-validation is to call the cross_val_score helper function on the estimator and the dataset. The following example demonstrates...
Read more >
Using cross_val_score in sklearn, simply explained
A common question developers have is whether cross_val_score can also function as a way of training the final model. Unfortunately this is not...
Read more >
2. Splitting and Cross-validation
Explain cross-validation and use cross_val_score() and cross_validate() to calculate ... Which type of machine learning does not have labels?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found