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

时间:2016-07-12 06:52:39

标签: python multiprocessing theano

我有一个由theano.function()返回的函数,我想在多处理中使用它来加速。以下是一个简化的演示脚本,用于显示遇到问题的位置:

            <table>
            <tr ng-repeat="x in names">
                <td>{{ x.Name }}</td>
                <td ng-hide="(x.Country == 'Argentina' || x.Country == 'Brazil' || x.Country == 'Germany')">{{ x.Country }}</td>
                <td ng-show="x.Country=='Germany'">Europe</td>
                <td ng-show="x.Country == 'Argentina' || x.Country == 'Brazil'">South America</td>
            </tr>
        </table>

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

完整的引用记录如下:

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)

有人有提示吗?

1 个答案:

答案 0 :(得分:0)

原来它与partial()函数有关。完整的解释在https://github.com/Theano/Theano/issues/4720#issuecomment-232029702