- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要一些帮助来准确理解我做了什么/为什么我的代码没有按我预期的那样运行。
我已经开始使用 joblib 来尝试通过并行运行(大)循环来加速我的代码。
我是这样使用它的:
from joblib import Parallel, delayed
def frame(indeces, image_pad, m):
XY_Patches = np.float32(image_pad[indeces[0]:indeces[0]+m, indeces[1]:indeces[1]+m, indeces[2]])
XZ_Patches = np.float32(image_pad[indeces[0]:indeces[0]+m, indeces[1], indeces[2]:indeces[2]+m])
YZ_Patches = np.float32(image_pad[indeces[0], indeces[1]:indeces[1]+m, indeces[2]:indeces[2]+m])
return XY_Patches, XZ_Patches, YZ_Patches
def Patch_triplanar_para(image_path, patch_size):
Image, Label, indeces = Sampling(image_path)
n = (patch_size -1)/2
m = patch_size
image_pad = np.pad(Image, pad_width=n, mode='constant', constant_values = 0)
A = Parallel(n_jobs= 1)(delayed(frame)(i, image_pad, m) for i in indeces)
A = np.array(A)
Label = np.float32(Label.reshape(len(Label), 1))
R, T, Y = np.hsplit(A, 3)
return R, T, Y, Label
我一直在试验“n_jobs”,期望增加它会加快我的功能。但是,当我增加 n_jobs 时,速度会显着降低。在没有“并行”的情况下运行此代码时,事情会变慢,直到我将作业数从 1 增加。
为什么会这样?我知道我运行的作业越多,脚本越快?我用错了吗?
谢谢!
最佳答案
可能你的问题是因为image_pad
是一个大数组。在您的代码中,您正在使用 joblib
的默认 multiprocessing
后端。这个后端创建了一个工作池,每个工作池都是一个 Python 进程。然后将函数的输入数据复制 n_jobs
次并广播给池中的每个工作人员,这会导致严重的开销。引自 joblib的文档:
By default the workers of the pool are real Python processes forked using the multiprocessing module of the Python standard library when n_jobs != 1. The arguments passed as input to the Parallel call are serialized and reallocated in the memory of each worker process.
This can be problematic for large arguments as they will be reallocated n_jobs times by the workers.
As this problem can often occur in scientific computing with numpy based datastructures, joblib.Parallel provides a special handling for large arrays to automatically dump them on the filesystem and pass a reference to the worker to open them as memory map on that file using the numpy.memmap subclass of numpy.ndarray. This makes it possible to share a segment of data between all the worker processes.
Note: The following only applies with the default "multiprocessing" backend. If your code can release the GIL, then using backend="threading" is even more efficient.
所以如果是这种情况,你应该切换到线程后端,如果你能够在调用 frame
时释放全局解释器锁,或者切换到 的共享内存方法作业库
.
docs假设 joblib
提供了一个可能有用的自动 memmap
转换。
关于Python - 使用 joblib 进行循环并行化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37545626/
我正在努力解决 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 并行的函数中生成随机数。但是,从内核生成的随机数是完全相同的。 目前我通过为不同的核心分配随机种子来解决这个问题。有什么简单的方法可以解决这个问题吗? 最佳答案 这是预料之
我是一名优秀的程序员,十分优秀!