Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Keras cross_val_score Error

I am trying to do this little tutorial on keras about regression: http://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/

Unfortunately I am running into an error I cannot fix. If i just copy and paste the code I get the following error when running this snippet:

import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
# load dataset
dataframe = pandas.read_csv("housing.csv", delim_whitespace=True,header=None)
dataset = dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,0:13]
Y = dataset[:,13]
# define base mode
def baseline_model():
    # create model
    model = Sequential()
    model.add(Dense(13, input_dim=13, init='normal', activation='relu'))
    model.add(Dense(1, init='normal'))
    # Compile model
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# evaluate model with standardized dataset
estimator = KerasRegressor(build_fn=baseline_model, nb_epoch=100,batch_size=5, verbose=0)

kfold = KFold(n_splits=10, random_state=seed)
results = cross_val_score(estimator, X, Y, cv=kfold)

The error says:

TypeError: get_params() got an unexpected keyword argument 'deep'

Thanks for any help.

Here is the full traceback:

Traceback (most recent call last):


File "<stdin>", line 1, in <module>
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 140, in cross_val_score
    for train, test in cv_iter)
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 758, in __call__
    while self.dispatch_one_batch(iterator):
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 603, in dispatch_one_batch
    tasks = BatchedCalls(itertools.islice(iterator, batch_size))
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 127, in __init__
    self.items = list(iterator_slice)
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 140, in <genexpr>
    for train, test in cv_iter)
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\base.py", line 67, in clone
    new_object_params = estimator.get_params(deep=False)
TypeError: get_params() got an unexpected keyword argument 'deep'
like image 375
user7454972 Avatar asked Jan 22 '17 21:01

user7454972


People also ask

What is Cross_val_score in Python?

Cross_val_score is a common function to use during the testing and validation phase of your machine learning model development. In this post I will explain what it is, what you can use it for, and how to implement it in Python.

What is the use of Cross_val_score?

The cross_val_score() function will be used to perform the evaluation, taking the dataset and cross-validation configuration and returning a list of scores calculated for each fold.

What does Sklearn Model_selection Cross_val_score do?

cross_val_score. Evaluate a score by cross-validation.


1 Answers

The specific error reported is:

TypeError: get_params() got an unexpected keyword argument 'deep'

The fault was introduced by a bug in Keras version 1.2.1. It occurs when you use the Keras wrapper classes (e.g. KerasClassifier and KerasRegressor) and scikit-learn function cross_val_score().

The bug has been identified and patched in the Keras GitHub project.

There are two fixes that I have tried:

Fix 1: Roll-back to Keras version 1.2.0.

Type:

sudo pip install keras==1.2.0

Fix 2: Monkey-patch Keras with the fix.

After your imports, but before your work type:

from keras.wrappers.scikit_learn import BaseWrapper
import copy

def custom_get_params(self, **params):
    res = copy.deepcopy(self.sk_params)
    res.update({'build_fn': self.build_fn})
    return res

BaseWrapper.get_params = custom_get_params

Both fixes work for me (Python 2 and 3/sklearn 0.18.1).

Some additional candidate fixes:

  • Wait for the next version of Keras (1.2.2) to be released.
  • Checkout Keras from Github then build and install manually.
like image 66
jasonb Avatar answered Sep 24 '22 10:09

jasonb