gpt4 book ai didi

dictionary - 在 IPython 中并行化字典上的函数

转载 作者:行者123 更新时间:2023-12-04 04:57:00 25 4
gpt4 key购买 nike

到目前为止,我通过使用函数 map_sync(function, list) 将函数映射到分布到各个集群的列表来实现并行化。 .

现在,我需要对字典的每个条目运行一个函数。

map_sync 似乎不适用于字典。我还尝试分散字典并使用装饰器并行运行该函数。然而,字典似乎也不适合散播。 有没有其他方法可以并行化字典上的函数而不必转换为列表?

这些是我迄今为止的尝试:

from IPython.parallel import Client
rc = Client()
dview = rc[:]

test_dict = {'43':"lion", '34':"tiger", '343':"duck"}
dview.scatter("test",test)

dview["test"]
# this yields [['343'], ['43'], ['34'], []] on 4 clusters
# which suggests that a dictionary can't be scattered?

不用说,当我运行函数本身时,我收到一个错误:
@dview.parallel(block=True)
def run():
for d,v in test.iteritems():
print d,v

run()

属性错误
回溯(最近一次调用最后一次)在 ()
在运行(字典)
AttributeError: 'str' 对象没有属性 'iteritems'

我不知道它是否相关,但我使用的是连接到 Amazon AWS 集群的 IPython Notebook。

最佳答案

您可以使用以下命令分散 dict:

def scatter_dict(view, name, d):
"""partition a dictionary across the engines of a view"""
ntargets = len(view)
keys = d.keys() # list(d.keys()) in Python 3
for i, target in enumerate(view.targets):
subd = {}
for key in keys[i::ntargets]:
subd[key] = d[key]
view.client[target][name] = subd

scatter_dict(dview, 'test', test_dict)

然后像往常一样远程操作它。

您还可以使用以下命令再次将远程字典收集到一个本地字典中:
def gather_dict(view, name):
"""gather dictionaries from a DirectView"""
merged = {}
for d in view.pull(name):
merged.update(d)
return merged

gather_dict(dv, 'test')

An example notebook

关于dictionary - 在 IPython 中并行化字典上的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16630747/

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