首页 > 解决方案 > 使用 Numba 时将多个输入传递给类时出错

问题描述

我正在尝试在课堂上使用 Numba Decorator。但是,我收到以下错误。我检查了输入尺寸,它看起来正确,但仍然出现相同的错误。关于如何解决这个问题的任何想法?

   spec = [('w_x', nb.int32), ('w_a', nb.int32),('mu_a', nb.int64[:]), 
      ('sig_a',nb.int64[:]),('mu_x', nb.int64[:]),('sig_x', nb.int32[:]), 
       ('mu_a_a',nb.float64[:,:]),('sig_a_a', nb.float64[:,:]), ('mu_x_a', 
       nb.int32[:]),('sig_x_a', nb.float32[:,:]),('mu_0', nb.boolean), 
       ('sig_0', nb.boolean),('beta', nb.int32),('policy', nb.uint8)]
    @nb.jitclass(spec)        
    class learner(object):
    def __init__ (self, w_x, w_a, beta, policy):
    '''
        initialize: 
        w_x: the dim of customer features
        w_a: the dim of ad features
        mu_a: the prior of mean of weights on ad
        sig_a: the prior of var of weights on ad
        mu_x: the prior of mean of weights on customer
        sig_x: the prior of var of weights on customer
        mu_a_a: the prior of interactions between ad segments
        sig_a_a: the prior of var of interactions between ad segments
        mu_x_a: the prior of mean of interactions between customers and ad 
        segments
        sig_x_a: the prior of var of interactions between customers and ad 
     segments
    '''
    self.w_x = w_x
    self.w_a = w_a
    self.mu_a = np.zeros(self.w_a)
    self.sig_a = np.ones(self.w_a)
    self.mu_x = np.zeros(self.w_x)
    self.sig_x = np.ones(self.w_x)
    self.mu_a_a = np.zeros((self.w_a, self.w_a))
    #self.mu_a_a = np.triu(self.mu_a_a, k=1)
    self.sig_a_a = np.ones((self.w_a, self.w_a))
    #self.sig_a_a = np.triu(self.sig_a_a, k=1)
    self.mu_x_a = np.zeros((self.w_x, self.w_a))
    self.sig_x_a = np.ones((self.w_x, self.w_a))
    #the intercept term w_0
    self.mu_0 = 0
    self.sig_0 = 1
    self.beta = beta
    self.policy = policy

以下是错误消息:

File "C:\Users\MSHAHAB2\AppData\Local\Continuum\anaconda3\lib\site- 
packages\numba\six.py", line 659, in reraise
raise value numba.errors.LoweringError: Failed at nopython (nopython mode 
backend)
Can only insert i64* at [4] in {i8*, i8*, i64, i64, i64*, [1 x i64], [1 x 
i64]}: got double*

File "batch_mode_function.py", line 147:
def __init__ (self, w_x, w_a, beta, policy):
    <source elided>
    self.w_a = w_a
    self.mu_a = np.zeros(self.w_a)
    ^
[1] During: lowering "(self).mu_a = $0.9" at 
W:\GRMOS\MShahabi\MNV\HillClimbSim\batch_mode_function.py (147)
[2] During: resolving callee type: 
jitclass.learner#1e390f65798<w_x:int32,w_a:int32,mu_a:array(int64, 1d, 
A),sig_a:array(int64, 1d, A),mu_x:array(int64, 1d, A),sig_x:array(int32, 1d, 
A),mu_a_a:array(float64, 2d, A),sig_a_a:array(float64, 2d, 
A),mu_x_a:array(int32, 1d, A),sig_x_a:array(float32, 2d, 
A),mu_0:bool,sig_0:bool,beta:int32,policy:uint8>
[3] During: typing of call at <string> (3)

标签: python-3.xanacondanumba

解决方案


正在显示的错误消息很容易解决。np.zeros创建一个默认的数组dtype=np.float64,它nb.float64在 numba. 您必须指定dtypein才能获得ornp.zeros的数组:np.int64np.int32

self.mu_a = np.zeros(self.w_a, dtype=np.int64)
self.sig_a = np.ones(self.w_a, dtype=np.int64)
self.mu_x = np.zeros(self.w_x, dtype=np.int64)
self.sig_x = np.ones(self.w_x, dtype=np.int32)

对于数组self.mu_x_aself.sig_x_a

self.mu_x_a = np.zeros((self.w_x, self.w_a), dtype=np.int32)
self.sig_x_a = np.ones((self.w_x, self.w_a), dtype=np.float32)

因为self.mu_x_a你也错过了spec. 它一定要是:

spec = [('mu_x_a',  nb.int32[:, :])]

然后在创建数组时出现后续错误self.mu_a_a。Numba 引发错误,形状元组(self.w_a, self.w_a)的类型为(i64, i32)。这显然是numba类型推断/转换的一些错误。所有nb.int32类型似乎都被nb.int64自动转换为。
有两种解决方法:

解决方法 1:替换with
的类型签名(以及,因为and需要它):self.w_anb.int64self.w_xself.mu_x_aself.sig_x_a

spec = [('w_x', nb.int64), ('w_a', nb.int64)]

解决方法 2: 不要使用以某种方式不一致地强制转换的实例变量。而是使用给定的输入:

self.mu_a_a = np.zeros((w_a, w_a))
self.sig_a_a = np.ones((w_a, w_a))
self.mu_x_a = np.zeros((w_x, w_a), dtype=np.int32)
self.sig_x_a = np.ones((w_x, w_a), dtype=np.float32)

我建议使用解决方法 1,因为目前 int32 在 numba 中被强制转换为 int64。使用解决方法 1,它应该如下所示:

spec = [('w_x', nb.int64), ('w_a', nb.int64),('mu_a', nb.int64[:]), 
      ('sig_a',nb.int64[:]),('mu_x', nb.int64[:]),('sig_x', nb.int32[:]), 
       ('mu_a_a',nb.float64[:,:]),('sig_a_a', nb.float64[:,:]), ('mu_x_a', 
       nb.int32[:, :]),('sig_x_a', nb.float32[:,:]),('mu_0', nb.boolean), 
       ('sig_0', nb.boolean),('beta', nb.int32),('policy', nb.uint8)]
@nb.jitclass(spec)        
class learner(object):
    def __init__ (self, w_x, w_a, beta, policy):
        '''
            initialize: 
            w_x: the dim of customer features
            w_a: the dim of ad features
            mu_a: the prior of mean of weights on ad
            sig_a: the prior of var of weights on ad
            mu_x: the prior of mean of weights on customer
            sig_x: the prior of var of weights on customer
            mu_a_a: the prior of interactions between ad segments
            sig_a_a: the prior of var of interactions between ad segments
            mu_x_a: the prior of mean of interactions between customers and ad 
            segments
            sig_x_a: the prior of var of interactions between customers and ad 
         segments
        '''
        self.w_x = w_x
        self.w_a = w_a
        self.mu_a = np.zeros(self.w_a, dtype=np.int64)
        self.sig_a = np.ones(self.w_a, dtype=np.int64)
        self.mu_x = np.zeros(self.w_x, dtype=np.int64)
        self.sig_x = np.ones(self.w_x, dtype=np.int32)
        self.mu_a_a = np.zeros((self.w_a, self.w_a))
        #self.mu_a_a = np.triu(self.mu_a_a, k=1)
        self.sig_a_a = np.ones((self.w_a, self.w_a))
        #self.sig_a_a = np.triu(self.sig_a_a, k=1)
        self.mu_x_a = np.zeros((self.w_x, self.w_a), dtype=np.int32)
        self.sig_x_a = np.ones((self.w_x, self.w_a), dtype=np.float32)
        #the intercept term w_0
        self.mu_0 = 0
        self.sig_0 = 1
        self.beta = beta
        self.policy = policy

对于解决方法 2,您可以保留w_xw_aas的规范nb.int32,只需将以下 4 个数组的数组创建替换为:

self.mu_a_a = np.zeros((w_a, w_a))
self.sig_a_a = np.ones((w_a, w_a))
self.mu_x_a = np.zeros((w_x, w_a), dtype=np.int32)
self.sig_x_a = np.ones((w_x, w_a), dtype=np.float32)

由于我猜铸造行为是一个错误,我建议您使用此线程的链接报告它。


推荐阅读