我一直在尝试并行化整个函数,当它在main中被调用时,或者,当你在下面看到的函数的任何部分时,我都不走运,似乎我无法摆脱TypeError: function object is not iterable
。谢谢你的建议。你知道吗
from joblib import Parallel, delayed
num_cores = multiprocessing.cpu_count()
parallel = Parallel(n_jobs=num_cores)
from multiprocessing import Pool
p = Pool(4)
def kmean(layerW,cluster):
weights1d = np.reshape(layerW,-1)
print(np.shape(weights1d))
#Parallelizing Here
centroids,_ = parallel(delayed(kmeans(weights1d, cluster)))
idxs,_ = parallel(delayed(vq(weights1d,centroids)))
#Here, using Parallel
weights1d_q = parallel(delayed([centroids[idxs[i]] for i in range(len(weights1d))]))
#OR --- using pool instead
weights1d_q = p.map([centroids[idxs[i]] for i in range(len(weights1d))])
weights4d_q = np.reshape(weights1d_q, np.shape(layerW))
return weights4d_q
为了
TypeError
:TypeError
由于语法错误(对joblib.Parallel( delayed( ... ) ... )
的调用格式错误,不遵守有文档记录的调用语法构造函数),异常被正确抛出。你知道吗示例1:正确的呼叫:
此调用遵循文档中的语法规范,一直到最后一点:
生成的结果证实了调用是完全兼容和可解释的。你知道吗
例2:打错电话:
结果证实,O/p使用了一种语法,与文档中的
joblib.Parallel( delayed(...) ... )
不兼容 Q.E.D
补救措施:
遵循
joblib.Parallel( delayed( ... ) ... )
记录的语法:最好的第一步:
是为了重新阅读
joblib.Parallel
是如何设计的以及使用模式的详细文档,以便更好地了解该工具:接下来,您可以掌握一些琐碎的示例(并进行实验,并将其扩展到预期的用例):
下一步:尝试理解使用
n_jobs
实例化的成本和限制使用基于线程的后端允许“共享”,但这意味着要付出巨大的成本-线程重新引入GIL步进,这将以GIL锁步进的方式将代码执行流重新注入到一个接一个的执行流中。对于计算密集型处理,其性能比原始的纯代码差(虽然这种模式有助于延迟掩蔽用例,在这种情况下,等待网络响应可能会允许线程释放GIL锁并让其他线程继续工作)
有一些步骤,人们可以实现,以便使单独的基于进程的计算能够传达这样的需求,然而,这需要一些附加成本。你知道吗
计算密集型问题必须平衡对最终性能的需求(使用更多的核心),同时考虑到只有一个独立的(分离的)工作单元,以及参数传输和结果返回的最小附加成本,所有这些都很容易增加成本,而不是一个错误的设计意图来利用可用的进程调度形式。你知道吗
更多信息details on ^{}
更多关于并行加速的含义
相关问题 更多 >
编程相关推荐