0

在 Rasbperry Pi 3 上运行 Python 时,我在多处理池中使用 train_test_split 时遇到了一些奇怪的行为。

我有这样的事情:

def evaluate_Classifier(model,Features,Labels,split_ratio):

  X_train, X_val, y_train, y_val = train_test_split(Features,Labels,test_size=split_ratio)
...


iterations=500
pool = multiprocessing.Pool(4)
results = [pool.apply_async(evaluate_Classifier, args=(w,Current_Features,Current_Labels,0.35)) for i in range(iterations)]
output = [p.get() for p in results]
pool.close()
pool.join()

现在,上面的代码在 Windows 7 Python 3.5.6 上完美运行,实际上 4 个线程中的每一个线程都会有不同的训练/测试拆分。

但是,当我在 Raspberry Pi 3 (scikit-learn 0.19.2) 上运行它时,似乎 4 个线程以完全相同的方式拆分数据,因此所有线程都产生完全相同的结果。接下来的 4 个线程将再次拆分数据(这次不同),但它们之间的方式仍然完全相同,依此类推....

我什至尝试使用带有 random_state=np.random.randint 的 train_test_split,但它没有帮助。

任何想法为什么这在 Windows 中有效但在树莓派 3 上似乎不能正确并行化?

非常感谢

4

2 回答 2

0

shuffle 默认情况下是打开的,所以即使使用 shuffle=True 它也没有什么区别。如果可能的话,我还想在并行函数中拆分数据。

实际上,一些挖掘我发现这是因为Windows和Linux如何处理多个线程和子进程等资源等。上述问题的最佳解决方案是执行以下操作:

def evaluate_Classifier(model,Features,Labels,split_ratio,i):

X_train, X_val, y_train, y_val =   train_test_split(Features,Labels,test_size=split_ratio,random_state=i)
...


iterations=500
pool = multiprocessing.Pool(4)
results = [pool.apply_async(evaluate_Classifier,   args=(w,Current_Features,Current_Labels,0.35, i)) for i in range(iterations)]
output = [p.get() for p in results]
pool.close()
pool.join()

这会很好用,并且为了在代码的不同运行之间增加一点随机性,我们可以在函数之外使用一些随机数生成器而不是 i

于 2018-09-13T14:06:03.057 回答
0

与其设置随机状态,不如在拆分前尝试对数据进行洗牌。您可以通过设置参数来做到这一点:shuffle=True。

于 2018-09-12T12:37:03.837 回答