2016-07-12 8 views
2

Ich habe eine Funktion, die von theano.function() zurückgegeben wird, und ich möchte es in Multiprocessing für Beschleunigung verwenden. Das Folgende ist eine vereinfachte Demo-Skript zu zeigen, wo ich in Probleme laufen:Wie kann man eine Theano-Funktion mit Multiprocessing parallelisieren?

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) 

In dem obigen Codes, die ‚f‘ Theanos Funktion zugeführt wird ‚func1()‘ als Eingabeargument. Wenn MPjob() korrekt ausgeführt wird, sollte es [0.1, 1.1, 2.1, 3.1, 4.1] zurückgeben. Eine Ausnahme "TypeError: func1() hat jedoch mehrere Werte für das Argument 'func'" ausgelöst.

Das vollständige Trackback-Protokoll ist wie folgt:

multiprocessing.pool.RemoteTraceback: 
""" 
Traceback (most recent call last): 
    File "C:\Python35\lib\multiprocessing\pool.py", line 119, in worker 
    result = (True, func(*args, **kwds)) 
TypeError: func1() got multiple values for argument 'func' 
""" 

The above exception was the direct cause of the following exception: 

Traceback (most recent call last): 
    File "F:/DaweiLeng/Code/workspace/Python/General/theano_multiprocess_debug.py", line 36, in <module> 
    Y = MPjob(xlist) 
    File "F:/DaweiLeng/Code/workspace/Python/General/theano_multiprocess_debug.py", line 29, in MPjob 
    for y in Results: 
    File "C:\Python35\lib\multiprocessing\pool.py", line 695, in next 
    raise value 
TypeError: func1() got multiple values for argument 'func' 

jemand einen Tip bekommen?

Antwort