gpt4 book ai didi

python - Keras 2, TypeError : can't pickle _thread. 锁定对象

转载 作者:太空狗 更新时间:2023-10-30 01:12:03 28 4
gpt4 key购买 nike

我正在使用 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

有什么解决办法吗,谢谢

最佳答案

好的问题似乎是在这一行中将方法作为参数传递:

model = KerasClassifier(build_fn=create_model(input_dim), verbose=0)

create_model 是传递给 build_fn 的参数,但我想传递另一个参数给 create_model。但这不是正确的做法,因此会导致错误。

不幸的是,在这种情况下错误消息没有提供信息。

关于python - Keras 2, TypeError : can't pickle _thread. 锁定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45081204/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com