如何使用多处理并行一个Theano函数?

2024-10-03 21:36:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数由theano.功能(),我想在多处理中使用它来加速。下面是一个简化的演示脚本,演示我在哪里遇到问题:

import numpy as np
from multiprocessing import Pool
from functools import partial
import theano
from theano import tensor

def get_theano_func():
    x = tensor.dscalar()
    y = x + 0.1
    f = theano.function([x], [y])
    return f

def func1(func, x):
    return func(x)

def MPjob(xlist):
    f = get_theano_func()
    fp = partial(func1, func=f)
    pool = Pool(processes=5)
    Results = pool.imap(fp, xlist)
    Y = []
    for y in Results:
        Y.append(y[0])
    pool.close()
    return Y

if __name__ == '__main__':
    xlist = np.arange(0, 5, 1)
    Y = MPjob(xlist)
    print(Y)

在上面的代码中,ano函数“f”作为输入参数输入到“func1()”。如果MPjob()运行正确,它应该返回[0.1,1.1,2.1,3.1,4.1]。但是,引发异常“TypeError:func1()为参数'func'”获取多个值。在

完整的trackback日志如下:

^{pr2}$

有人知道吗?在


Tags: 函数fromimportreturndefnptheanopartial