python-3.x - 使用 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)
解决方案
正在显示的错误消息很容易解决。np.zeros
创建一个默认的数组dtype=np.float64
,它nb.float64
在 numba. 您必须指定dtype
in才能获得ornp.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:替换with
的类型签名(以及,因为and需要它):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 在 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_x
和w_a
as的规范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)
由于我猜铸造行为是一个错误,我建议您使用此线程的链接报告它。
推荐阅读
- javascript - 如何在 DOM 中呈现 Javascript console.log 的文本?
- windows - 您可以在同一个 PSExec 命令中执行多个批处理文件吗?
- python - 在任意轴上迭代体积的更 Pythonic 方式?
- web - 是什么阻止我通过检查响应标头来规避 CSP 随机数?
- input - Svelte - 使用复选框在两个输入值之间切换
- angular - 如何在离子 4 App sqlite db 中的 1 之后启动索引数组
- gstreamer-1.0 - 将 tsdemux 链接到 h264parse 的 GStreamer 1.16.0 问题
- vue.js - 如何从 api 获取 taginput 数据
- http - 如何在golang中使用multipart
- docker - Docker栈容器交互