- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试实现 parallel for
循环使用 joblib
在 python 中 3.8.3
.
在 for 循环中,我想将一个类方法应用于一个类的实例,同时在另一个类中应用一个方法。
这是一个 MWE,我尝试看看我的想法是否有效,但它没有。有谁知道如何让它发挥作用?
from joblib import Parallel, delayed
class A():
def __init__(self):
self.val = 0
def add5(self):
self.val += 5
class B():
def __init__(self):
self.obj = [A() for _ in range(10)]
def apply(self):
""" this is where I'm trying to use joblib:
for a in self.obj:
a.add5()"""
def f(x):
x.add5()
Parallel(n_jobs=-1)(delayed(f)(x) for x in self.obj)
def prnt(self):
print([a.val for a in self.obj])
b = B()
b.prnt() # returns [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
b.apply()
b.prnt() # returns [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] but
# I expect [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
我的问题的更多背景:我正在使用 sci-kit 学习实现一个提升算法,在应用算法之前我生成弱学习器。拟合和预测是在 for 循环中完成的,可能需要一些时间,所以我想添加并行化以尝试加快进度。基本上,
class A
是一个分类器和
class B
是我的算法,我想在其中拟合我生成的所有分类器。
最佳答案
从( source )可以读到:
The default backend of joblib will run each function call in isolatedPython processes, therefore they cannot mutate a common Python objectdefined in the main program.
However if the parallel function really needs to rely on the sharedmemory semantics of threads, it should be made explicit withrequire='sharedmem', for instance:
require='sharedmem'
给您的
Parallel
为了:
Parallel(n_jobs=-1, require='sharedmem')(delayed(f)(x) for x in self.obj)
然而,
source指出:
Keep in mind that relying a on the shared-memory semantics is probablysuboptimal from a performance point of view as concurrent access to ashared Python object will suffer from lock contention.
f
功能来自:
def f(x):
x.add5()
返回对象。
def f(x):
x.add5()
return x
并在
Parallel loop
,从:
Parallel(n_jobs=-1)(delayed(f)(x) for x in self.obj)
进入:
self.obj = Parallel(n_jobs=-1)(delayed(f)(x) for x in self.obj)
这样您就可以分配
self.obj
到由并行循环返回的列表。
from joblib import Parallel, delayed
class A:
def __init__(self):
self.val = 0
def add5(self):
self.val += 5
class B:
def __init__(self):
self.obj = [A() for _ in range(10)]
def apply(self):
""" this is where I'm trying to use joblib:
for a in self.obj:
a.add5()"""
def f(x):
x.add5()
return x
self.obj = Parallel(n_jobs=-1)(delayed(f)(x) for x in self.obj)
def prnt(self):
print([a.val for a in self.obj])
b = B()
b.prnt() # returns [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
b.apply()
b.prnt() # returns [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] but
# I expect [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
关于python - 如何将 joblib 并行化与不返回任何内容的类内方法一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65206662/
我正在努力解决 joblib 问题。 我需要将推理应用程序推送到远程服务器,并且我还需要加载保存的标准缩放器,因为如果我尝试适应,我会收到内存不足异常。我无法对服务器进行物理更改,因为它是 sap c
我在 Docker 容器内的 Flask 应用程序中运行 joblib 以及由 supervisord 启动的 uWSGI(启动时启用线程)。 网络服务器启动显示如下错误: unable to loa
我正在尝试在 python 中使用并行计算包 joblib。我可以执行下面的例子并得到结果 Parallel(n_jobs=8)(delayed(sqrt)(i) for i in range(10)
这是我的代码: from math import sqrt from joblib import Parallel, delayed import multiprocessing def parall
我正在使用Python中joblib包中的Parallel函数。我只想使用此函数来处理我的函数之一,但不幸的是整个代码是并行运行的(除了其他函数)。 示例: from joblib import Pa
我正在努力学习 joblib模块作为 python 中内置 multiprocessing 模块的替代品。我习惯于使用 multiprocessing.imap 在可迭代对象上运行一个函数并返回结果。
我正在尝试使用 joblib 来并行化一个在函数上运行的循环。我希望显示函数的中间 print 命令,而不仅仅是函数的 return 值。 from joblib import Parallel, d
我想打乱 3D numpy 数组中的值,但前提是它们 > 0。 当我用单核运行我的函数时,它甚至比使用 2 个核快得多。这远远超出了创建新 python 进程的开销。我错过了什么? 以下代码输出: r
我的目标结构: 工具 model_maker.py 模特 模型在这里 我当前的代码,位于工具目录中 joblib.dump(pipeline, "../models/model_full_June20
是否可以使用 joblib.Memory 以线程安全的方式写入跨多个进程的公共(public)缓存。在什么情况下,这会失败或导致错误? 最佳答案 库首先写入临时文件,然后将临时文件移动到目的地。 So
我目前正在尝试实现 parallel for循环使用 joblib在 python 中 3.8.3 . 在 for 循环中,我想将一个类方法应用于一个类的实例,同时在另一个类中应用一个方法。 这是一个
我的代码看起来像这样: from joblib import Parallel, delayed # prediction model - 10s of megabytes on disk LARGE
from joblib import Parallel, delayed def func(v): temp.append(v) return temp = [] Parallel(n
有关于使用内存映射文件在 Joblib 中持久保存 Numpy 数组的良好文档。 在最近的版本中,Joblib(显然)会以这种方式自动保留和共享 Numpy 数组。 Pandas 数据帧也会被持久化,
我正在运行一个需要一段时间才能评估 16 次的函数。然而,所有这些运行都是相互独立的。因此我决定使用 joblib 来加速它。 Joblib 的工作方式就像它应该的那样并加快了速度,但我正在努力解决一
我正在使用 joblib 并行化我的 python 3.5 代码。 如果我这样做: from modules import f from joblib import Parallel, delaye
我正在使用 Random Forest Regressor python 的 scikit-learn 模块来预测一些值。我使用 joblib.dump 来保存模型。有 24 个 joblib.dum
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我想问同样的问题 Python 3: does Pool keep the original order of data passed to map?对于作业库。例如: Parallel(n_jobs
我需要在使用 Joblib 并行的函数中生成随机数。但是,从内核生成的随机数是完全相同的。 目前我通过为不同的核心分配随机种子来解决这个问题。有什么简单的方法可以解决这个问题吗? 最佳答案 这是预料之
我是一名优秀的程序员,十分优秀!