我正在使用Keras创建ANN并在网络上进行网格搜索。运行以下代码时遇到以下错误:
model = KerasClassifier(build_fn=create_model(input_dim), verbose=0)
# define the grid search parameters
batch_size = [10, 20]
epochs = [50, 100]
dropout = [0.3, 0.5, 0.7]
param_grid = dict(dropout_rate=dropout, batch_size=batch_size, nb_epoch=epochs)
pipe.append(('classify', model))
params.append(param_grid)
pipeline=Pipeline(pipe)
#the pipeline also contains feature selector, but for convenience I do not include code here
piped_classifier = GridSearchCV(estimator=pipeline, param_grid=params, n_jobs=-1,
cv=nfold)
piped_classifier.fit(X_train, y_train) #this is line 246 of classifier_gridsearch.py causing error, see below,
def create_model(input_dim,dropout_rate=0.0):
# create model
model = Sequential()
model.add(Dense(80,
input_dim=input_dim,
kernel_initializer='uniform', activation='relu'))
model.add(Dropout(dropout_rate))
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
第246行的错误会引发错误,如下所示:堆栈跟踪:
Traceback (most recent call last):
File "/home/zqz/Work/chase/python/src/exp/classifier_gridsearch_main.py", line 176, in <module>
classifier.gridsearch()
File "/home/zqz/Work/chase/python/src/exp/classifier_gridsearch_main.py", line 155, in gridsearch
self.fs_option,self.fs_gridsearch)
File "/home/zqz/Work/chase/python/src/ml/classifier_gridsearch.py", line 246, in learn_dnn
piped_classifier.fit(X_train, y_train)
File "/home/zqz/Programs/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_search.py", line 945, in fit
return self._fit(X, y, groups, ParameterGrid(self.param_grid))
File "/home/zqz/Programs/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_search.py", line 550, in _fit
base_estimator = clone(self.estimator)
File "/home/zqz/Programs/anaconda3/lib/python3.6/site-packages/sklearn/base.py", line 69, in clone
new_object_params[name] = clone(param, safe=False)
File "/home/zqz/Programs/anaconda3/lib/python3.6/site-packages/sklearn/base.py", line 57, in clone
return estimator_type([clone(e, safe=safe) for e in estimator])
File "/home/zqz/Programs/anaconda3/lib/python3.6/site-packages/sklearn/base.py", line 57, in <listcomp>
return estimator_type([clone(e, safe=safe) for e in estimator])
File "/home/zqz/Programs/anaconda3/lib/python3.6/site-packages/sklearn/base.py", line 57, in clone
return estimator_type([clone(e, safe=safe) for e in estimator])
File "/home/zqz/Programs/anaconda3/lib/python3.6/site-packages/sklearn/base.py", line 57, in <listcomp>
return estimator_type([clone(e, safe=safe) for e in estimator])
File "/home/zqz/Programs/anaconda3/lib/python3.6/site-packages/sklearn/base.py", line 69, in clone
new_object_params[name] = clone(param, safe=False)
File "/home/zqz/Programs/anaconda3/lib/python3.6/site-packages/sklearn/base.py", line 60, in clone
return copy.deepcopy(estimator)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 215, in _deepcopy_list
append(deepcopy(a, memo))
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 215, in _deepcopy_list
append(deepcopy(a, memo))
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 180, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 280, in _reconstruct
state = deepcopy(state, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 150, in deepcopy
y = copier(x, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/home/zqz/Programs/anaconda3/lib/python3.6/copy.py", line 169, in deepcopy
rv = reductor(4)
TypeError: can't pickle _thread.lock objects
任何建议如何解决这个问题,谢谢
答案 0 :(得分:4)
好的问题似乎是将方法作为参数传递,在这一行:
model = KerasClassifier(build_fn=create_model(input_dim), verbose=0)
create_model 是要传递给 build_fn 的参数,但我想将另一个参数传递给 create_model 。但这不是正确的做法,因而导致错误。
不幸的是,在这种情况下,错误消息没有提供信息。