gpt4 book ai didi

python - PyQt5 在单独的 QThread 上运行 sklearn 计算

转载 作者:行者123 更新时间:2023-12-01 09:15:41 25 4
gpt4 key购买 nike

我正在创建一个 PyQt5 应用程序,并希望在单击按钮时在单独的线程中运行一些机器学习代码。我无法这样做。这是我的线程代码:

newthread = QtCore.QThread()
runthread = RunThread()
runthread.moveToThread(newthread)
runthread.finished.connect(newthread.quit)
newthread.started.connect(runthread.long_run)
newthread.start()

RunThread类如下:

class RunThread(QtCore.QObject):

finished = QtCore.pyqtSignal()

def __init__(self):
super().__init__()

@pyqtSlot()
def long_run(self):
#ML code called in the line below
simulation.start_simulation()
self.finished.emit()

正常运行是不行的。 Pycharm 退出并出现以下错误:

process finished with exit code -1073740791 (0xc0000409)

在 Debug模式下运行它会引发 sklearn 抛出的数千个警告:

Multiprocessing-backed parallel loops cannot be nested below threads,
setting n_jobs=1

代码最终会运行,但需要更多时间(至少 4 倍)。

有人可以告诉我这里的问题是什么吗?

最佳答案

来自 sklearn 的警告在这里非常明确。基本上,当从嵌套线程执行时,您无法将超参数 n_jobs 设置为大于 1 的值来训练 sklearn 模型。

话虽如此,在不了解 simulation.start_simulation() 内部发生的情况的情况下,很难猜测。

我最好的猜测是在 start_simulation() 中查找使用多个作业的任何内容,并查看将其设置为 n_jobs=1 时会发生什么。

或者,您可以尝试将 ML 代码编写为独立脚本并使用 QProcess 执行它。这可能允许您利用大于 1 的 n_jobs

关于python - PyQt5 在单独的 QThread 上运行 sklearn 计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51286339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com