以下代码抛出错误:
TypeError: can't pickle _thread.lock objects
我可以看出,这可能与将先前的方法作为函数传递给def fit(self, c_m)有关。 但是,根据文档,我认为这是正确的:https://keras.io/scikit-learn-api/ 如果有人看到我的代码中的错误,请指出,我会感激您的帮助。
我可以看出,这可能与将先前的方法作为函数传递给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,但没有任何效果。还有其他想法吗?