gpt4 book ai didi

python - scikit - 随机森林回归 - AttributeError : 'Thread' object has no attribute '_children'

转载 作者:太空狗 更新时间:2023-10-29 21:45:55 24 4
gpt4 key购买 nike

在为随机森林回归器设置 n_jobs 参数 > 1 时出现以下错误。如果我设置 n_jobs=1,一切正常。

AttributeError: 'Thread' object has no attribute '_children'

我在 flask 服务中运行这段代码。有趣的是,在 flask 服务之外运行时不会发生这种情况。我只在新安装的 Ubuntu 机器上重现了这个。在我的 Mac 上它工作得很好。

这是一个讨论这个问题的线程,但似乎没有解决任何问题: 'Thread' object has no attribute '_children' - django + scikit-learn

对此有什么想法吗?

这是我的测试代码:

@test.route('/testfun')    def testfun():        from sklearn.ensemble import RandomForestRegressor        import numpy as np        train_data = np.array([[1,2,3], [2,1,3]])        target_data = np.array([1,1])        model = RandomForestRegressor(n_jobs=2)        model.fit(train_data, target_data)        return "yey"

堆栈跟踪:

    Traceback (most recent call last):      File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__        return self.wsgi_app(environ, start_response)      File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app        response = self.make_response(self.handle_exception(e))      File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception        reraise(exc_type, exc_value, tb)      File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app        response = self.full_dispatch_request()      File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request        rv = self.handle_user_exception(e)      File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception        reraise(exc_type, exc_value, tb)      File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request        rv = self.dispatch_request()      File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request        return self.view_functions[rule.endpoint](**req.view_args)      File "/home/vagrant/flask.global-relevance-engine/global_relevance_engine/routes/test.py", line 47, in testfun        model.fit(train_data, target_data)      File "/usr/local/lib/python2.7/dist-packages/sklearn/ensemble/forest.py", line 273, in fit        for i, t in enumerate(trees))      File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/parallel.py", line 574, in __call__        self._pool = ThreadPool(n_jobs)      File "/usr/lib/python2.7/multiprocessing/pool.py", line 685, in __init__        Pool.__init__(self, processes, initializer, initargs)      File "/usr/lib/python2.7/multiprocessing/pool.py", line 136, in __init__        self._repopulate_pool()      File "/usr/lib/python2.7/multiprocessing/pool.py", line 199, in _repopulate_pool        w.start()      File "/usr/lib/python2.7/multiprocessing/dummy/__init__.py", line 73, in start        self._parent._children[self] = None

最佳答案

问题

这可能是由于 python 2.7.5 和 3.3.2 之前存在的 multiprocessing.dummy(参见 herehere)中的错误所致。

解决方案 A - 升级 Python

查看评论以确认新版本适用于 OP。

解决方案 B - 修改 dummy

如果您无法升级但可以访问 .../py/Lib/multiprocessing/dummy/__init__.py,请编辑 start 方法 DummyProcess 类如下(应该是 ~line 73):

if hasattr(self._parent, '_children'):  # add this line
self._parent._children[self] = None # indent this existing line

解决方案 C - 猴子补丁

DummyProcess 就是这个 bug 存在的地方。让我们看看它在您导入的代码中的什么位置,以确保我们在正确的地方修补它。

  • 随机森林回归器
  • 继承:ForestRegressor
  • 继承:BaseForest
  • 创建于:sklearn.ensemble.forest
  • 其中导入:来自 sklearn.externals.joblib 的并行
  • 从 multiprocessing.pool 导入 ThreadPool
  • 从 multiprocessing.dummy 导入和存储进程
  • 已分配给:也在 multiprocessing.dummy 中的 DummyProcess

DummyProcess 在该链中的存在保证它已经在导入 RandomForestRegressor 之后被导入。此外,我认为我们可以在创建任何实例之前访问 DummyProcess 类。因此,我们可以对类进行一次修补,而无需寻找实例进行修补。

# Let's make it available in our namespace:
from sklearn.ensemble import RandomForestRegressor
from multiprocessing import dummy as __mp_dummy

# Now we can define a replacement and patch DummyProcess:
def __DummyProcess_start_patch(self): # pulled from an updated version of Python
assert self._parent is __mp_dummy.current_process() # modified to avoid further imports
self._start_called = True
if hasattr(self._parent, '_children'):
self._parent._children[self] = None
__mp_dummy.threading.Thread.start(self) # modified to avoid further imports
__mp_dummy.DummyProcess.start = __DummyProcess_start_patch

除非我遗漏了什么,否则从现在开始所有创建的 DummyProcess 实例都将被修补,因此不会发生该错误。

对于任何更广泛地使用 sklearn 的人,我认为您可以反向完成此操作并使其适用于所有 sklearn,而不是专注于一个模块。在进行任何 sklearn 导入之前,您需要导入 DummyProcess 并按上述方式对其进行修补。然后 sklearn 将从一开始就使用补丁类。


原答案:

在我写评论时,我意识到我可能已经发现了你的问题——我认为你的 flask 环境使用的是旧版本的 python。

原因是在最新版本的 python 多处理中,您收到该错误的行受条件保护:

if hasattr(self._parent, '_children'):
self._parent._children[self] = None

看起来像this bug在 python 2.7 期间被修复(我认为从 2.7.5 开始修复)。也许您的 flask 是旧的 2.7 或 2.6?

你能检查一下你的环境吗?如果您不能更新解释器,也许我们可以找到一种方法来猴子补丁多处理以防止它崩溃。

关于python - scikit - 随机森林回归 - AttributeError : 'Thread' object has no attribute '_children' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32877365/

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