如果函数不在模块级别,如何将函数提交给 Dask?

How to submit a function to the Dask if the function is not in module level?

我在 dask 分布式系统中有 scipy 个稀疏矩阵。我想将其转换为数组并留在分布式系统中。我可以在我的本地机器上这样做:

from scipy.sparse import coo_matrix
coo_matrix((3, 4), dtype=np.int8).toarray()

这returns一个数组。我尝试像下面这样转换稀疏矩阵:

from scipy import sparse
# Some code here
#
#
my_sparse_matrix = sparse.coo_matrix((3, 4), dtype=np.int8)
my_sparse_matrix = client.scatter(my_sparse_matrix)  # Send to the distributed system
# submit conversion function (to the distributed system)
my_result = self.client.submit(sparse.csr_matrix.toarray, my_sparse_matrix)
client.gather(my_result)

返回此错误:

{AttributeError}_swap not found

我收到此错误是因为 .toarray() 不是模块级函数。我该如何解决这个问题?

你的代码工作正常,但我相信你有错字:你应该使用适合你想要操作的对象的方法,但你在 submit 调用中使用了 csr_matrix而不是 coo_matrix 这是 my_sparse_matrix 引用的内容。以下作品:

>>> from scipy import sparse
>>> my_sparse_matrix = sparse.coo_matrix((3, 4), dtype=np.int8)
>>> my_sparse_matrix = client.scatter(my_sparse_matrix)
>>> my_result = client.submit(sparse.coo_matrix.toarray, my_sparse_matrix)
>>> client.gather(my_result)
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int8)

不幸的是,错误消息没有给你一个明显的原因,但你在没有 Dask 的情况下得到了同样的消息:

>>> arr = sparse.coo_matrix((3, 4), dtype=np.int8)
>>> parse.csc_matrix.toarray(arr)
AttributeError: _swap not found