【问题标题】:TypeError: Arrays must have consistent types in assignmentTypeError:数组在赋值中必须具有一致的类型
【发布时间】:2014-09-05 14:38:40
【问题描述】:

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 以加快我的代码速度之前,我实际上对此没有任何问题。

【问题讨论】:

  • 看起来 g_hNone。如果您的 for 循环从未被输入,那么 g_h 可能不会被设置为任何内容。
  • 为什么不输出zweights?如果其中任何一个为空,则永远不会进入您的 for 循环。
  • 我刚刚编辑了代码以在 g 为空时为其分配一些值。另外 - 我确实尝试输出权重和 z,但问题是 - 这些不是运行时错误。我认为这些是 numba 包中的编译错误,甚至在代码开始运行之前。我认为的问题是将一个无的变量分配给一个不是无的变量。在 Numba 中难道就没有这个吗?
  • 啊,我明白了。尝试将g_h 初始化为np.zeroes 而不是None
  • 就是这样!谢谢。你可以把它作为答案。

标签: python numpy types numba numba-pro


【解决方案1】:

问题在于 Numba 将 g_h 推断为 NoneType;将其初始化为向量,它将正确编译:

g_h = np.zeroes((H, no_features, no_classes))

【讨论】:

    【解决方案2】:

    问题是 numba 在最终分配给g 时无法知道g_h 不会是None,因为g_h 的类型取决于运行时流控制。换句话说,如果g_h 可能永远不是 float64,那么它必须假设有时不是。

    这是documented limitation of 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 documentation on types了解更多信息。

    【讨论】:

    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2019-04-26
    • 2013-01-18
    • 2011-04-29
    • 1970-01-01
    相关资源
    最近更新 更多