多处理机器学习代码永无止境

2024-06-16 15:17:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力并行运行几个机器学习算法(来自scikit learn),并且我正在使用Process类,在进程之间共享一个变量,以便保存结果。你知道吗

不幸的是,我的代码永远不会结束。会不会是内存问题,因为我运行了10个相当繁重的算法?或者只是速度慢?你知道吗

我试着把整个代码分成两部分(我想这会使它更快),但是,它没有改变任何东西。。。你知道吗

注意,train\u bow和test\u bow只是浮动向量。你知道吗

from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import MultinomialNB, ComplementNB, BernoulliNB
from sklearn.ensemble import GradientBoostingClassifier, AdaBoostClassifier, VotingClassifier, ExtraTreesClassifier
from sklearn.svm import SVC, LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier as Knn
from sklearn.feature_extraction.text import TfidfVectorizer

#Custom class
from utilities.db_handler import *
from utilities.utils import *
from multiprocessing import Process, Manager
import json
import pickle as pkl
import os
import numpy as np
import pandas as pd

manager = Manager()
return_dict = manager.dict()

# Use a shared variable in order to get the results
proc = []
fncs1 = [random_forest_classification, SVC_classification, LinearSVC_classification, MultinomialNB_classification,
        LogisticRegression_classification]
fncs2 = [BernoulliNB_classification, GradientBoosting_classification,
        AdaBoost_classification, VotingClassifier_classification, ComplementNB_classification,
        ExtrExtraTrees_classification]

# Instantiating 2 set of processes with relative arguments. Each function
# writes the result on result_dict
for fn in fncs1:
    p = Process(target=fn, args=(train_bow, test_bow, label_train, label_test, return_dict))
    proc.append(p)
    p.start()
for p in proc:
    p.join()

for fn in fncs2:
    p = Process(target=fn, args=(train_bow, test_bow, label_train, label_test, return_dict))
    proc.append(p)
    p.start()
for p in proc:
    p.join()

# then pick te best of the results from return_dict and save them

这段代码给了我一些属于算法的警告,但是没有显示任何与多处理相关的错误或警告。你知道吗


Tags: infromtestimportforreturnastrain
2条回答

也许你应该详细调试一下。检查“长”时间段的迭代次数。迭代是在进行还是在某个地方卡住了?您的初始算法是否工作正常(尝试不进行多重处理)?如果是这样,那么您可以将问题隔离到多处理中,并查看管理器是如何进行接口的。你知道吗

另外,请包括您的系统规格,一般的经验法则是,您应该只运行n个进程的n核在您的电脑上

检查任务管理器上的性能规格,看看您使用了多少内存/内核。你知道吗

您还可能希望启动一个线程,检查进程是活动的还是完成的,以查看哪些算法没有运行。你知道吗

个人意见与ML,除非你有一个野兽电脑,它的缓慢:)

我用Thread替换了Process就成功了。你知道吗

我认为这是可行的,可能是因为创建许多流程比较慢,一些经理/调度员/下级实体必须处理和管理这些流程。你知道吗

我没有启动“controller”线程,但似乎一切正常。你知道吗

谢谢你们的帮助!你知道吗

相关问题 更多 >