gpu - Multiprocessing for Python parallelization error - "function' object is not iterable" -
we have nvidia tesla k80 gpu accelerator computing in our data center following characteristics: intel(r) xeon(r) cpu e5-2670 v3 @2.30ghz, 48 cpu processors, 128gb ram, 12 cpu cores
running under linux 64-bit.
i running following code gridsearchcv
after vertically appending different sets of dataframes single series of data randomforestregressor
model. example, 2 sample datasets considering found in this link
from joblib import parallel, delayed import multiprocessing import sys import imp import glob import os import pandas pd import math sklearn.feature_extraction.text import countvectorizer sklearn.feature_extraction.text import tfidfvectorizer sklearn.linear_model import logisticregression import matplotlib sklearn.model_selection import cross_val_score import matplotlib.pyplot plt import numpy np sklearn.ensemble import randomforestregressor, gradientboostingregressor sklearn.naive_bayes import gaussiannb sklearn.model_selection import gridsearchcv sklearn.naive_bayes import multinomialnb sklearn.linear_model import lassocv sklearn.metrics import r2_score, mean_squared_error, make_scorer sklearn.model_selection import train_test_split math import sqrt sklearn.cross_validation import train_test_split df = pd.concat(map(pd.read_csv, glob.glob(os.path.join('', "cubic*.csv"))), ignore_index=true) #df = pd.read_csv('cubic31.csv') in range(1,3): df['x_t'+str(i)] = df['x'].shift(i) print(df) df.dropna(inplace=true) x = (pd.dataframe({ 'x_%d'%i : df['x'].shift(i) in range(3)}).apply(np.nan_to_num, axis=0).values) x = df.drop('y', axis=1) y = df['y'] x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.40) x_train = x_train.drop('time', axis=1) x_test = x_test.drop('time', axis=1) print(x.shape) print(df['y'].shape) print() print("size of x_train:",(len(x_train))) print("size of y_train:",(len(x_train))) print("size of x_test:",(len(x_test))) print("size of y_test:",(len(y_test))) print() def gridsearchcvparallel(): #fit models grid search cv=5 (not low), use best model parameters = {'n_estimators': [10,30,100,500,1000]} clf_rf = randomforestregressor(random_state=1) clf = gridsearchcv(clf_rf, parameters, cv=5, scoring='neg_mean_squared_error') model = clf.fit(x_train, y_train) model.cv_results_['params'][model.best_index_] math.sqrt(model.best_score_*-1) model.grid_scores_ ##### print() print(model.grid_scores_) print("the best score: ",model.best_score_) print("rmse:",math.sqrt(model.best_score_*-1)) #reg = randomforestregressor(criterion='mse') clf_rf.fit(x_train,y_train) modelprediction = clf_rf.predict(x_test) print(modelprediction) print("number of predictions:",len(modelprediction)) meansquarederror=mean_squared_error(y_test, modelprediction) print("mean square error (mse):", meansquarederror) rootmeansquarederror = sqrt(meansquarederror) print("root-mean-square error (rmse):", rootmeansquarederror) ####### add trendline fig, ax = plt.subplots() #df.plot(x='time', y='y', ax=ax) ax.plot(df['time'].values, df['y'].values) fig, ax = plt.subplots() index_values=range(0,len(y_test)) y_test.sort_index(inplace=true) x_test.sort_index(inplace=true) modelpred_test = clf_rf.predict(x_test) ax.plot(pd.series(index_values), y_test.values) plotinone=pd.dataframe(pd.concat([pd.series(modelpred_test), pd.series(y_test.values)], axis=1)) plt.figure(); plotinone.plot(); plt.legend(loc='best') numberofcores = multiprocessing.cpu_count() gridresults = parallel(n_jobs=numberofcores)(delayed(gridsearchcvparallel)) print(gridresults)
when run program huge dataset (around 2 million rows), taking more 4 days gridsearchcv
. after bit of search, found out python
threads can utilize more 1 cpu using either concurrent.futures
or multiprocessing
. shown in code, tried make use of multiplrocessing
getting error typeerror: 'function' object not iterable
. seems function should take single parameter input , pass in iterable argument. how can fix issue utilize more 1 cpu , task faster in short period of time?
thank in advance.
do not attempt parallelize on own. not use joblib.parallel
. reinventing wheel, anyway, since gridsearchcv
already-parellized. pass n_jobs
parameter, defaults 1
, i.e. defaults using single job. take advantage of multi-core architecture, pass n_jobs = number_of_cores
, number_of_cores
number of cores want use.
and if check source code, you'll see wraps call joblib.parallel, n_jobs=-1
should work "all cores".
Comments
Post a Comment