简体   繁体   English

Keras模型上的递归特征消除(RFE)-TypeError:无法腌制_thread.RLock对象

[英]Recursive Feature Elimination (RFE) on Keras Model - TypeError: can't pickle _thread.RLock objects

I'd like to follow up on a previous question ( Recursive Feature Elimination on Keras Models ) because I have hit a road block on it. 我想跟进上一个问题( Keras模型上的递归特征消除 ),因为我遇到了障碍。 I am currently trying to implement the following(not all my code is here for ease of legibility): 我目前正在尝试实现以下内容(为了便于阅读,并非所有代码都在这里):

from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.feature_selection import RFE

k_model  = KerasClassifier(build_fn=model, epochs=epochs, 
batch_size=bs, verbose=0) #model is a standard Keras MLP
selector  = RFE(k_model, step=1)

This appears to be functional, however, on the next line where I try to fit the model: 但是,在我尝试拟合模型的下一行中,这似乎起作用了:

selector  = selector.fit(x_train, y_train)

I receive the following error: 我收到以下错误:

TypeError: can't pickle _thread.RLock objects

Any thoughts/help would be appreciated. 任何想法/帮助将不胜感激。 Edit: 编辑:

# =============================================================================
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils, plot_model
from sklearn.model_selection import train_test_split
from sklearn.ensemble import AdaBoostClassifier as ABC
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.feature_selection import RFE
import numpy as np
import time
# =============================================================================
# Functions:
# MLP(x, y)
# =============================================================================
def MLP(x, y):
    s_time = time.clock()
#   ATR, MOM, RSI, OBV
#   keep_features = ['ATR', 'MOM', 'RSI', 'OBV']
#   drop_features = list(set(list(x)).difference(keep_features))
#   x.drop(drop_features, axis=1, inplace=True)    
#    x = x.as_matrix()
#    y = y.as_matrix()
#    num_features = np.array((5,10,15))
    num_classes = 3
    epochs = 1
    bs = 10
    np.random.seed(7)

    x_train,  x_test,  y_train,  y_test  = train_test_split(x, y, test_size=0.33)

#    y_train  = np_utils.to_categorical(y_train, num_classes)
#    y_test   = np_utils.to_categorical(y_test, num_classes)

    # create model
    model  = Sequential()    
    model.add( Dense(50, input_shape=(x_train.shape[1],), activation='tanh'))    
    model.add( Dense(num_classes, activation='softmax'))

    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#
# =============================================================================
#     plot_model(model, to_file='../3_Deliverables/Final Paper/data/keras_model.png')
# =============================================================================

    # Port Keras Framework into SK-Learn
    k_model  = KerasClassifier(build_fn=model, epochs=epochs, batch_size=bs, verbose=0)
    temp = k_model
    selector = RFE(temp, step=1)
    out = selector.fit(x_train, y_train)

    # evaluate the model
#    scores = model.evaluate(x_test,  y_test)
#    print("\n%s: %.2f%%" % (indicators.metrics_names[1], scores[1]*100))

    e_time = time.clock()
    print('\n Total Time: ', e_time-s_time)

Error Stack: 错误堆栈:

File "C:\Miniconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", 
line 705, in runfile
execfile(filename, namespace)

File "C:\Miniconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", 
line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Schifman Ben/2_Software/project.py", line 51, in <module>
pred.MLP(x_all, y_all)

File "C:\Users\Schifman Ben\2_Software\prediction.py", line 50, in MLP
out = selector.fit(x_train, y_train)

File "C:\Miniconda3\lib\site-packages\sklearn\feature_selection\rfe.py", 
line 134, in fit
return self._fit(X, y)

File "C:\Miniconda3\lib\site-packages\sklearn\feature_selection\rfe.py", 
line 169, in _fit
estimator = clone(self.estimator)

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

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

File "C:\Miniconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)

File "C:\Miniconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)

File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)

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

File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)

File "C:\Miniconda3\lib\copy.py", line 215, in _deepcopy_list
append(deepcopy(a, memo))

File "C:\Miniconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)

File "C:\Miniconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)

File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)

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

File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)

File "C:\Miniconda3\lib\copy.py", line 215, in _deepcopy_list
append(deepcopy(a, memo))

File "C:\Miniconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)

File "C:\Miniconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)

File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)

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

File "C:\Miniconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)

File "C:\Miniconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)

File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)

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

File "C:\Miniconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)

File "C:\Miniconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)

File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)

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

File "C:\Miniconda3\lib\copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)

File "C:\Miniconda3\lib\copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)

File "C:\Miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)

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

File "C:\Miniconda3\lib\copy.py", line 169, in deepcopy
rv = reductor(4)

TypeError: can't pickle _thread.RLock objects

KerasClassifier build_fn expects a pointer to a function which returns the model , not the model itself. KerasClassifier build_fn需要指向返回model的函数的指针,而不是模型本身。

So change your code like: 因此,像这样更改您的代码:

def MLP(x, y):

    ...
    ...
    ...
    x_train,  x_test,  y_train,  y_test  = train_test_split(x, y, test_size=0.33)

#    y_train  = np_utils.to_categorical(y_train, num_classes)
#    y_test   = np_utils.to_categorical(y_test, num_classes)

    # This is what you need
    # create model
    def create_model():
        model = Sequential()    
        model.add(Dense(50, input_shape=(x_train.shape[1],), activation='tanh'))    
        model.add(Dense(num_classes, activation='softmax'))

        # Compile model
        model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        return model

    ...
    ...

    # Port Keras Framework into SK-Learn
    k_model  = KerasClassifier(build_fn=create_model, epochs=epochs, batch_size=bs, verbose=0)
    ...
    ...

But after that you will get another error, about feature_importances_ , because RFE is not compatible with KerasClassifier. 但是在那之后,您将得到另一个关于feature_importances_错误,因为RFE与KerasClassifier不兼容。

See this question for more details: Feature selection on a keras model 有关更多详细信息,请参见此问题: keras模型上的特征选择

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用pickle保存keras模型时面临“无法pickle _thread.rlock对象”错误 - Facing 'can't pickle _thread.rlock objects' error while saving keras model using pickle “TypeError: can&#39;t pickle _thread.RLock objects”,同时使用 pickle 保存 Facebook Prophet 模型 - "TypeError: can't pickle _thread.RLock objects" while saving Facebook Prophet model using pickle 类型错误:无法pickle _thread.RLock 对象 - TypeError: can't pickle _thread.RLock objects TypeError:无法在 python 3 中腌制 _thread.RLock 对象 - TypeError: can't pickle _thread.RLock objects in python 3 获取 TypeError:无法pickle _thread.RLock 对象 - Getting TypeError: can't pickle _thread.RLock objects TypeError: can&#39;t pickle _thread.RLock objects in pandas with multiprocessing - TypeError: can't pickle _thread.RLock objects in pandas with multiprocessing 如何使用 mlflow.pyfunc.log_model() 使用 Keras 步骤记录 sklearn 管道? 类型错误:无法pickle _thread.RLock 对象 - How to log a sklearn pipeline with a Keras step using mlflow.pyfunc.log_model()? TypeError: can't pickle _thread.RLock objects TypeError: can't pickle _thread.RLock objects (Deep Learning) - TypeError: can't pickle _thread.RLock objects ( Deep Learning) Tensflow Keras: TypeError: can't pickle _thread.RLock objects when using multiprocessing - Tensflow Keras: TypeError: can't pickle _thread.RLock objects when using multiprocessing _pickle.PicklingError:无法序列化对象:TypeError:无法腌制_thread.RLock对象 - _pickle.PicklingError: Could not serialize object: TypeError: can't pickle _thread.RLock objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM