Keras中的Logistic回归分类器

时间:2018-06-12 13:54:44

标签: python keras logistic-regression

我正在使用keras训练2000 Logistic回归分类器。 每个分类器的输入是:

用于培训:载体:8250X50,标签:8250

用于验证:2750X50,标签:2750

用于测试:3000X50,标签:3000

对于每个分类器,我保存预测和分数(kappa分数,准确度......)

代码非常慢,需要三个小时来训练前600个分类器。

我使用了以下代码

def lg_keras2(input_dim,output_dim,ep,X,y,Xv,yv,XT,yT,class_weight1):

    model = Sequential() 
    model.add(Dense(output_dim, input_dim=input_dim, activation='sigmoid'))
    #model.summary()
    model.compile(optimizer='adam', loss='binary_crossentropy',metrics = ["accuracy",mcor,recall, f1]) 
    result = model.fit(X, y, epochs=ep, verbose=0, batch_size = 128, class_weight = {0 :class_weight1[0]  , 1:class_weight1[1] } ,validation_data = (Xv, yv)) 
    test = model.evaluate(XT, yT, verbose=0) 
    kappa_Score=(cohen_kappa_score( yT,(model.predict_classes(XT))))

    return model,result,test,kappa_Score

之后我训练了2000个分类器如下:

from sklearn.utils import class_weight
from sklearn.metrics import cohen_kappa_score
directionsLGR=[]
scores=[]
predictions=[]
kappa_Score_all=[]
for i in  range(0,2000):

    Class_weight = class_weight.compute_class_weight('balanced',
                                             np.unique(pmiweights_Train[:,i]),
                                             pmiweights_Train[:,i])

    #start_time = time.time()
    model,results,test,kappa = lg_keras2(50,1,30,mdsTrain, pmiweights_Train[:,i],mdsVal, pmiweights_val[:,i],mdsTest,pmiweights_Test[:,i],Class_weight)
    #print("--- %s seconds ---" % (time.time() - start_time))
    weights=np.array(model.get_weights())[0].flatten()
    directionsLGR.append(weights)
    predictions.append(model.predict_classes(mds))
    kappa_Score_all.append(kappa)
    scores.append(test)

我能做些什么来加快这个过程。 我将不胜感激任何建议

0 个答案:

没有答案