我正在尝试在课堂上使用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)
答案 0 :(得分:1)
显示的错误消息很容易解决。 np.zeros
会默认创建一个dtype=np.float64
的数组,其数量为numba中的nb.float64
。您必须在dtype
中指定np.zeros
才能获得np.int64
或np.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_a
和self.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:
将self.w_a
的类型签名替换为nb.int64
(以及self.w_x
的类型签名,因为self.mu_x_a
和self.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中的int32强制转换为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_x
和w_a
的规范保留为nb.int32
,而只需将以下4个数组的数组创建替换为:< / p>
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)
由于我认为behavious的转换是一个错误,因此建议您通过此线程的链接进行报告。