2016-05-30 103 views
0

我創建了一個類似於sklearn gridsearch的小代碼,它在一組超參數上訓練模型(在下面的代碼中爲X和y),使用一組超參數有關驗證數據的幾個指標(Xt,yt_class),並將結果存儲在pandas DataFrame中。Python,在多個CPU上運行循環

from sklearn.grid_search import ParameterGrid 
    from sklearn.metrics import precision_score,f1_score 

    grid = {'C':[1,10.0,50,100.0],'gamma':[0.00001,0.0001,0.001,0.01,0.1]} 
    param_grid = ParameterGrid(grid) 
    results = pd.DataFrame(list(param_grid)) 
    precision = [] 
    f1 = [] 
    for params in param_grid: 
     model = SVC(kernel='rbf',cache_size=1000,class_weight='balanced',**params) 
     model.fit(X,y) 
     precision.append(precision_score(yt_class, model.predict(Xt), average='weighted')) 
     f1.append(f1_score(yt_class, model.predict(Xt), average='weighted')) 
     print(params) 
     print(precision_score(yt_class, model.predict(Xt), average='weighted')) 
     print(f1_score(yt_class, model.predict(Xt), average='weighted')) 

    results['precision'] = precision 
    results['f1'] = f1 

現在我努力讓自己在多個CPU我的循環運行,我嘗試以下基本的例子多處理模塊,但作爲新的Python和總體規劃的無法弄清楚它的作品在我的情況。什麼不起作用

例子:

import multiprocessing as mp 
pool = mp.Pool(processes=8) 

def get_scores(param_grid): 
    precision = [] 
    f1 = [] 
    for params in param_grid: 
     model = SVC(kernel='rbf',cache_size=1000,class_weight='balanced',**params) 
     model.fit(X,y) 
     model.predict(Xt) 
     precision.append(precision_score(yt_class, model.predict(Xt), average='weighted')) 
     f1.append(f1_score(yt_class, model.predict(Xt), average='weighted')) 
    return precision,f1  

scores = pool.apply(get_scores,param_grid) 

回答

2

get_scores方法應該只包含循環

的內部的試試這個:

import multiprocessing as mp 
from sklearn.grid_search import ParameterGrid 
from sklearn.metrics import precision_score,f1_score 

def get_scores(params): 
    model = SVC(kernel='rbf',cache_size=1000,class_weight='balanced',**params) 
    model.fit(X,y) 
    model.predict(Xt) 
    precision = precision_score(yt_class, model.predict(Xt), average='weighted') 
    f1 = f1_score(yt_class, model.predict(Xt), average='weighted') 
    return precision, f1  


grid = {'C':[1,10.0,50,100.0],'gamma':[0.00001,0.0001,0.001,0.01,0.1]} 
param_grid = ParameterGrid(grid) 
pool = mp.Pool(processes=8) 

scores = pool.map_async(get_scores, param_grid).get() 
# scores is a list of tuples [(precision_1, f1_1), (precision_2, f1_2)...] 
# you can "unzip" it like this 

precision, f1 = zip(*scores) 
+0

我拿到後出現以下錯誤運行代碼(運行我的代碼後出現同樣的錯誤):'TypeError:get_scores()需要1個位置參數,但是有20個被賦予' –

+1

對,它應該是map_異步,而不是apply_async –