与全局字典并行的嵌套 For 循环

Nested For Loop Parallel with global dictionary

我正在尝试并行编写此代码块 运行。

目前 运行每个模型大约需要 6 分钟,这太慢了。

d_mtry={}
# ------------------------------------------------------------------------
pass; count = 0; itr = COUNT
while count < itr
    kf = KFold(n_splits = 10)
    # --------------------------------------------------------------------
    for j in range (2, 25):
        avg_acc = 0
        for train_index, test_index in kf.split(X_train):
            X_train_K, X_test_K = X_train[train_index], X_train[test_index]
            y_train_K, y_test_K = y_train[train_index], y_train[test_index]
            rotf = RRForestClassifier( n_estimators = 30,
                                       criterion    =  'entropy',
                                       max_features =  j,
                                       n_jobs       = -1,
                                       random_state =  1
                                       )
            rotf.fit(X_train_K, y_train_K)

            y_predict_K = rotf.predict(X_test_K)
            y_prob = rotf.predict_proba(X_test_K)

            acc_score = accuracy_score(y_test_K, y_predict_K)
            avg_acc += acc_score

        d_mtry[str(j)] = avg_acc/10

    # --------------------------------------------------------------------
    best_mtry = max(d_mtry.iteritems(), key=operator.itemgetter(1))[0]

    f.write("\n" + "Iteration: " + str(count+1) + " Best M_Try: " + str(best_mtry)+ "\n")
    f.write(str(d_mtry))

    rotf = RRForestClassifier( n_estimators = 30,
                               criterion    =  'entropy',
                               max_features =  int( best_mtry ),
                               n_jobs       = -1,
                               random_state =  1
                               )
    # there is more code after this
    # I don't think it is relevant,
    # it has to do with calculations on the model rotf
    # --------------------------------------------------------------------
    count += 1
# ------------------------------------------------------------------------

我一直遇到的主要问题是字典没有针对我所做的尝试正确地并行更新。在使用 multiprocessing.Pool() 实例执行方法时,它似乎 运行 也没有快多少,我在另一个 post.[=15= 中发现]

这里的目标是根据折叠的平均准确度,并在我创建模型并在测试集上 运行 时使用它。

最初,我尝试使用 GridSearchCV(),但是 运行 遇到了安装问题并且从未完成 运行ning,即使在 AWS 托管的设置上,也有 36 个内核.

感谢任何帮助。

我喜欢 python 原型设计的简便性。
我也知道,当需要更改架构设计优先级以获得最终性能时。


事实 #1:n_jobs == -1 实际上占据了所有 CPU 核心

这就是说,没有免费的 CPU-资源(你可能属于财富之子,如果不是,也可以进入硬件限制的 [SPACE]-维度(是的,谈到内存计算,后来翻译成 [TIME] 维度,一旦你的模型停止适应内存并且性能将跳升许多数量级)在密集的 ML 模型调整期间)。

在没有可用备用资源的情况下,当实际执行块交错时,任何进程调度都将求助于纯[SERIAL] system-scheduling ( going one after another ) or at maximum to a "just"-[CONCURRENT]系统调度,由O/S 系统调度程序,到 CPU-资源的一个受限(大小)子集,因为没有其他 "free" CPU-核心无法做得更好。

在这种情况下,none 可用于 multiprocessing 模块中的 Python 代码的工具将永远提高代码执行性能,但恰恰相反(可能已经对此进行了基准测试,或者刚刚在您自己的鞋子中感受到了这一点)。

None 以上命名的系统调度策略是真实的-[PARALLEL] 系统调度示例。


事实 #2:"just"-[CONCURRENT] 有助于 I/O-bound,而不是 CPU-绑定案例

除了将初始数据集加载到 RAM 中,没有其他 I/O-related 性能阻塞。因此,任何 "increased"(以及任何 multiprocessing 模块工具的注入)并发永远不会提高您的本地主机处理吞吐量(没有 I/O-latency 掩码似乎比增加 [=91 时失去性能更好=] 作业调度交错和上下文切换成本)。


解决方案:如果确实需要增加原型制作吞吐量

已经制作了一个 AI/ML-prototyping 工厂的原型,运行 几乎是 24/7/365 BAU,这可能对你有帮助。

考虑一种不同的体系结构,而不是使 dict 全局可访问。

可以编排流程:

一个host_00是一个"parameter-server",告诉别人什么[ aMlMODEL, aParamSET, DataSET ]

A host_01 是 "worker-pool-member",运行 全力以赴,有 n_jobs = -1
host_02 是 "worker-pool-member",运行 全力以赴,有 n_jobs = -1
host_03 是 "worker-pool-member",运行 全力以赴,有 n_jobs = -1
..
host_NN 是 "worker-pool-member",运行 全力以赴,有 n_jobs = -1

拥有原型工厂概念:
- 吞吐量最大,
- 吞吐量可以动态扩展(隔夜等),
- 吞吐量几乎与 N
成线性比例 - 工厂架构有 lowest possible orchestration overheads, compared to any other naive attempts to "parallelise-a-code"