以下代码引发错误: TypeError:无法pickle _thread.lock对象
我可以看到它可能与将前一个方法作为def fit中的函数传递(self,c_m)。但我认为这是正确的文件:https://keras.io/scikit-learn-api/
如果有人在我的代码中看到错误,我可能会犯一个新手错误,我会很感激帮助。
np.random.seed(7)
y_dic = []
class NN:
def __init__(self):
self.X = None
self.y = None
self.model = None
def clean_data(self):
seed = 7
np.random.seed(seed)
dataset = pd.read_csv('/Users/isaac/pca_rfe_tsne_comparisons/Vital_intrusions.csv', delimiter=',', skiprows=0)
dataset = dataset.iloc[:,1:6]
self.X = dataset.iloc[:, 1:5]
Y = dataset.iloc[:, 0]
for y in Y:
if y >= 8:
y_dic.append(1)
else:
y_dic.append(0)
self.y = y_dic
self.X = np.asmatrix(stats.zscore(self.X, axis=0, ddof=1))
self.y = to_categorical(self.y)
def create_model(self):
self.model = Sequential()
self.model.add(Dense(4, input_dim=4, activation='relu'))
self.model.add(Dense(4, activation='relu'))
self.model.add(Dense(2, activation='sigmoid'))
self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
pass
def fit(self, c_m):
model = KerasClassifier(build_fn=c_m, verbose=0)
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
pdb.set_trace()
grid_result = grid.fit(self.X, self.y)
return (grid_result)
def results(self, grid_results):
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))
def main():
nn = NN()
nn.clean_data()
nn.create_model()
grid_results = nn.fit(nn.create_model)
nn.results(grid_results)
if __name__ == "__main__":
main()
好的,跟进这个。感谢您的评论@MarcinMożejko。你是对的。我应该提到更多的错误。在def fit()中,我写了model = KerasClassifier,而不是self.model = Keras Classifier。我想提一下,任何人都在看代码。我现在在同一行上收到一个新错误:
AttributeError:'NoneType'对象没有属性'loss'。
我可以追溯到scikit_learn.py:
loss_name = self.model.loss
if hasattr(loss_name, '__name__'):
loss_name = loss_name.__name__
if loss_name == 'categorical_crossentropy' and len(y.shape) != 2:
y = to_categorical(y)
我不知道如何解决这个问题,因为我在self.model.compile中设置了损失项。我尝试将其更改为binary_crossentropy,但没有效果。还有什么进一步的想法吗?
答案 0 :(得分:5)
问题出在这行代码中:
keras
很遗憾 - 目前,pickle
不支持将sklearn
应用于grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=1)
应用多处理所需的模型(here您可以阅读有关此问题的讨论)。为了使此代码有效,您应该设置:
Resolver error
e is undefined