Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: TypeError: can't pickle _thread.lock objects with KerasClassifier

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

dataset = pd.read_csv("Churn_Modelling.csv")
X = dataset.iloc[:,3:13].values
Y = dataset.iloc[:,13:].values

from sklearn.preprocessing import OneHotEncoder,LabelEncoder,StandardScaler

enc1=LabelEncoder()
enc2=LabelEncoder()
X[:,1] = enc1.fit_transform(X[:,1])
X[:,2] = enc2.fit_transform(X[:,2])

one = OneHotEncoder(categorical_features=[1])
X=one.fit_transform(X).toarray()

X = X[:,1:]

from sklearn.model_selection import train_test_split
Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,Y,random_state=0,test_size=0.2)

scale = StandardScaler()
scale.fit_transform(Xtrain)
scale.transform(Xtest)

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

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

    return net

classfier = KerasClassifier(build_fn=func1(),batch_size=10, epochs=100)
cross = cross_val_score(estimator=classfier, X=Xtrain, y=Ytrain, cv=10 , n_jobs=-1)

Throws the error:

Traceback (most recent call last):

  File "<ipython-input-7-e80e82960eb9>", line 1, in <module>
    cross = cross_val_score(estimator=classfier, X=Xtrain, y=Ytrain, cv=10 , n_jobs=-1)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\model_selection\_validation.py", line 342, in cross_val_score
    pre_dispatch=pre_dispatch)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\model_selection\_validation.py", line 206, in cross_validate
    for train, test in cv.split(X, y, groups))

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\externals\joblib\parallel.py", line 779, in __call__
    while self.dispatch_one_batch(iterator):

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\externals\joblib\parallel.py", line 620, in dispatch_one_batch
    tasks = BatchedCalls(itertools.islice(iterator, batch_size))

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\externals\joblib\parallel.py", line 127, in __init__
    self.items = list(iterator_slice)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\model_selection\_validation.py", line 206, in <genexpr>
    for train, test in cv.split(X, y, groups))

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\base.py", line 62, in clone
    new_object_params[name] = clone(param, safe=False)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\site-packages\sklearn\base.py", line 53, in clone
    return copy.deepcopy(estimator)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 215, in _deepcopy_list
    append(deepcopy(a, memo))

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 215, in _deepcopy_list
    append(deepcopy(a, memo))

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 150, in deepcopy
    y = copier(x, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File "C:\Users\Joish\Anaconda3\envs\project\lib\copy.py", line 169, in deepcopy
    rv = reductor(4)

TypeError: can't pickle _thread.lock objects

How do I solve this?

like image 522
Joish Avatar asked Jan 17 '18 14:01

Joish


1 Answers

Change this line:

classfier = KerasClassifier(build_fn=func1, batch_size=10, epochs=100, verbose=0)

Note that func1 is not called. From the documentation:

build_fn: callable function or class instance

The build_fn should construct, compile and return a Keras model, which will then be used to fit/predict. One of the following three values could be passed to build_fn:

  1. A function

  2. An instance of a class that implements the __call__ method

  3. None. This means you implement a class that inherits from either KerasClassifier or KerasRegressor. The __call__ method of the present class will then be treated as the default build_fn.
like image 105
Maxim Avatar answered Nov 12 '22 21:11

Maxim