2017-04-19 10 views
2

numba.jitclassデコレータを使用して最適化のためにクラスをマークします。numbaでカスタムタイプを使用してメソッドの関数シグネチャを指定する方法

私は最適化したいrunメソッドの署名を指定する方法がわかりません。このメソッドは、ConvertedDocumentの配列を引数として取ります。 numbaは私がnopythonモードでrunメソッドを呼び出ししようとすると、次のエラーが発生したとして独自に配列型を把握することはできないようです:

Traceback (most recent call last): 
    File "numba_test.py", line 53, in <module> 
    print run(a) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/dispatcher.py", line 310, in _compile_for_args 
    raise e 
numba.errors.TypingError: Caused By: 
Traceback (most recent call last): 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/compiler.py", line 230, in run 
    stage() 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/compiler.py", line 444, in stage_nopython_frontend 
    self.locals) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/compiler.py", line 800, in type_inference_stage 
    infer.propagate() 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 767, in propagate 
    raise errors[0] 
TypingError: Internal error at <numba.typeinfer.ExhaustIterConstraint object at 0x788cc9572d50>: 
--%<----------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 128, in propagate 
    constraint(typeinfer) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 264, in __call__ 
    raise TypingError("failed to unpack {}".format(tp), loc=self.loc) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/contextlib.py", line 35, in __exit__ 
    self.gen.throw(type, value, traceback) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/errors.py", line 249, in new_error_context 
    six.reraise(type(newerr), newerr, sys.exc_info()[2]) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/errors.py", line 243, in new_error_context 
    yield 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 264, in __call__ 
    raise TypingError("failed to unpack {}".format(tp), loc=self.loc) 
InternalError: local variable 'tp' referenced before assignment 
[1] During: typing of exhaust iter at numba_test.py (40) 
--%<----------------------------------------------------------------- 

File "numba_test.py", line 40 

Failed at nopython (nopython frontend) 
Internal error at <numba.typeinfer.ExhaustIterConstraint object at 0x788cc9572d50>: 
--%<----------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 128, in propagate 
    constraint(typeinfer) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 264, in __call__ 
    raise TypingError("failed to unpack {}".format(tp), loc=self.loc) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/contextlib.py", line 35, in __exit__ 
    self.gen.throw(type, value, traceback) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/errors.py", line 249, in new_error_context 
    six.reraise(type(newerr), newerr, sys.exc_info()[2]) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/errors.py", line 243, in new_error_context 
    yield 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 264, in __call__ 
    raise TypingError("failed to unpack {}".format(tp), loc=self.loc) 
InternalError: local variable 'tp' referenced before assignment 
[1] During: typing of exhaust iter at numba_test.py (40) 
--%<----------------------------------------------------------------- 

File "numba_test.py", line 40 

This error may have been caused by the following argument(s): 
- argument 0: Unsupported array dtype: object 

ここで私はnumbaデコレータを指定する方法は次のとおりです。

spec = [ 
    ('profile', numba.typeof(numpy.asarray([1.0, 2.0]))), 
    ('word_weights', numba.typeof(numpy.asarray([(1.0,2.0)]))) 
] 

@numba.jitclass(spec) 
class ConvertedDocument(object): 
    def __init__(self, profile, word_weights): 
     self.profile = profile 
     self.word_weights = word_weights 

@numba.jit(nopython=True,cache=True) 
def run(docs): 
    s = 0 
    for doc in docs: #array of documents 
     for w_id, weight in doc.word_weights: #accessing document's property 
      s += weight 

    return s 

そして、これはrunメソッドが呼び出される方法です。

x = numpy.asarray([1.0, 2.0]) 
y = numpy.asarray([(1.0,2.0), (3.0,4.0)]) 
a = numpy.asarray([ConvertedDocument(x,y)]) 
print run(a) 

a numpyのアレイは、Pythonのリストに置き換えている場合は、次のように例外がある:

Failed at nopython (nopython mode backend) 
reflected list(instance.jitclass.ConvertedDocument#3bffb70<profile:array(float64, 1d, C),word_weights:array(float64, 2d, C)>): unsupported nested memory-managed object 

誰もがオブジェクトの配列の反復処理をサポートしているか否かのカスタムタイプが使用されているときにメソッドのシグネチャを指定する方法を知っているかいますか?

答えて

0

jitclassオブジェクトに呼び出すことができないという問題があります。jitclassは反復可能ではないため、意味があります。構造体の配列ではなく、配列の構造体(および他のデータ型)としてデータを格納します。あなたは後者としてそれを使用しようとしています。 2つの配列属性に加えて、スカラーデータ属性または異なるサイズの配列がある場合は、jitclassオブジェクトをどのように反復するかについてはあいまいです。

エラーメッセージは明らかに不明です。私の助言はword_weightsのインデックスを繰り返し処理する必要があります。

+0

ご返信ありがとうございます。つまり、 'docs'はドキュメントの配列であり、単一の' jitclass'オブジェクトではありませんので、繰り返し可能な配列のドキュメントを繰り返し処理しようとしています。おそらく必要ではなく、以前のエラーの原因となっていた 'np.nditer'を削除して私の例を更新しました。今はエラーが異なり、この種の反復が現時点ではサポートされていない可能性があることを示唆していますが、わかりません。 – ce57

関連する問題