多处理机器学习代码永无止境
Multiprocessing machine learning code never ends
我正在努力 运行 并行使用多个机器学习算法(来自 scikit-learn),并且我正在使用进程 class 和进程之间共享的变量,以节省结果。
不幸的是,我的代码永远不会结束。会不会是内存问题,因为我正在 运行宁 10 个相当繁重的算法?还是只是速度慢?
我尝试将整个代码分成两部分(我认为这样会更快),但是,它并没有改变任何东西...
请注意 train_bow 和 test_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
这段代码给出了一些属于算法的警告,但没有显示任何与多处理相关的错误或警告。
也许你应该详细调试一下。检查 'long' 时间段的迭代次数。迭代是在进行中还是在某个地方卡住了?您的初始算法是否完全正常工作(尝试不进行多处理)?如果是,那么您可以将问题隔离到多处理并查看管理器的接口方式。
另外请包括您的系统规格,一般的经验法则是您的 PC 上应该只 运行 n 个进程用于 n 个内核
检查任务管理器的性能规格,了解您的使用量 memory/cores。
您可能还想启动一个线程来检查进程是否处于活动状态或已完成以查看哪些算法不正常。
个人对 ML 的看法,除非你有一台野兽 PC,否则它很慢:)
我设法让它工作,只是将 Process
替换为 Thread
。
我认为它可行可能是因为创建许多进程速度较慢,并且一些 manager/scheduler/lower 级别的实体必须处理并管理这些进程。
我没有启动 "controller" 线程,但似乎一切正常。
谢谢大家的帮助!
我正在努力 运行 并行使用多个机器学习算法(来自 scikit-learn),并且我正在使用进程 class 和进程之间共享的变量,以节省结果。
不幸的是,我的代码永远不会结束。会不会是内存问题,因为我正在 运行宁 10 个相当繁重的算法?还是只是速度慢?
我尝试将整个代码分成两部分(我认为这样会更快),但是,它并没有改变任何东西...
请注意 train_bow 和 test_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
这段代码给出了一些属于算法的警告,但没有显示任何与多处理相关的错误或警告。
也许你应该详细调试一下。检查 'long' 时间段的迭代次数。迭代是在进行中还是在某个地方卡住了?您的初始算法是否完全正常工作(尝试不进行多处理)?如果是,那么您可以将问题隔离到多处理并查看管理器的接口方式。
另外请包括您的系统规格,一般的经验法则是您的 PC 上应该只 运行 n 个进程用于 n 个内核
检查任务管理器的性能规格,了解您的使用量 memory/cores。
您可能还想启动一个线程来检查进程是否处于活动状态或已完成以查看哪些算法不正常。
个人对 ML 的看法,除非你有一台野兽 PC,否则它很慢:)
我设法让它工作,只是将 Process
替换为 Thread
。
我认为它可行可能是因为创建许多进程速度较慢,并且一些 manager/scheduler/lower 级别的实体必须处理并管理这些进程。
我没有启动 "controller" 线程,但似乎一切正常。
谢谢大家的帮助!