1

hereからフォローアップすると、次のようなコードが得られます。

@jit(float_[:,:,:](float_[:,:], int_[:], int_)) 
def train_function(X, y, H):
    # do lots of stuff, including setting the arrays g and g_per_round like this:
    g = np.zeros((no_features, no_classes))
    g_per_round = np.zeros((H, no_features, no_classes))

    # do more stuff, then:    
        g_h = None
        j = 0
        print "Calculating regression coefficients per class. .."
        # building the parameters per j class
        for y1_w in zip(z.T, weights.T):
            y1, w = y1_w 
            temp_g = sm.WLS(y1, X, w).fit()  # Step 2(a)(ii)
            if g_h is None: # sometimes g *is* None, and that's fine
                   g_h = temp_g.params # this is an array of floats
            else:
                    g_h = np.c_[g_h, temp_g.params]
            j = j + 1

        if np.allclose(g,0) or g is None:
            g = g_h
        else:            
            g = g + g_h 

    # do lots more stuff, then finally:
    return g_per_round

class GentleBoostC(object):
    # init functions and stuff
    def train(self, X, y, H):
        self.g_per_round = train_function(X, y, H)    

今、私は次のエラーが発生しています:

 @jit(float_[:,:,:](float_[:,:], int_[:], int_))
 more lines, etc etc etc, last few lines:
    unresolved_types, var_name)
  File "C:\Users\app\Anaconda\lib\site-packages\numba\typesystem\ssatypes.py", line 767, in promote_arrays
    assert_equal(non_array_types[0])
  File "C:\Users\app\Anaconda\lib\site-packages\numba\typesystem\ssatypes.py", line 764, in assert_equal
    var_name, result_type, other_type))
TypeError: Arrays must have consistent types in assignment for variable 'g': 'float64[:, :]' and 'none'

@jitコードを高速化するために追加しようとする前に、実際にこれに問題はありませんでした。

4

2 に答える 2

2

問題は、 のタイプがランタイム フロー制御に依存するため、numba が最終的に割り当てられるときではg_hないことを認識できないことです。言い換えれば、float64 ではない可能性がある場合は、そうでない場合があると想定する必要があります。Nonegg_hg_h

これは文書化された numbaの制限であり、一般的な型推論システムの制限です。

ただし、いくつかの制限があります。つまり、変数は、制御フローのマージ ポイントで単一化可能な型でなければなりません。たとえば、次のコードはコンパイルされません。

@jit def incompatible_types(arg):
    if arg > 10:
        x = "hello"
    else:
        x = 1

    return x        # ERROR! Inconsistent type for x!

g_h解決策は、の代わりに互換性のある型に初期化することです= None

Numba の型推論は実際には非常にスマートであるため、型が返される前に統合できる限り、多くの場合、特定のローカル変数で型を問題なく混在させることができます。詳細については、型に関する Numba のドキュメントを参照してください。

于 2014-09-05T15:05:05.927 に答える
2

問題は、Numba が;であると推測g_hしていることです。NoneTypeベクトルに初期化すると、適切にコンパイルされます。

g_h = np.zeroes((H, no_features, no_classes))
于 2014-09-05T15:02:04.297 に答える