Numba 和二维 numpy 数组列表
Numba and list of two dimensional numpy array
我有一些 class:
from numba import jitclass, int32, float32, types
from numba.typed import List
_spec = [
('Y_rf', types.List(float32[:, :])),
...
]
@jitclass(_spec)
class DensityRatioEstimation:
def __init__(self, sigma):
self.sigma = sigma
self.Y_rf = [np.array([[0.]], dtype=float32)]
但我做不到。它总是因不同的错误而中断。现在错误是:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.typeinfer.CallConstraint object at 0x00000277CBBBF550>.
Failed in nopython mode pipeline (step: nopython mode backend)
File "src\models\DDRE.py", line 26:
def __init__(self, sigma):
<source elided>
self.sigma = sigma
self.Y_rf = [np.array([[0.]], dtype=float32)]
^
[1] During: lowering "(self).Y_rf = [=11=].11" at D:\anomaly-detection\src\models\DDRE.py (26)
[2] During: resolving callee type: jitclass.DensityRatioEstimation#277c8a6cdd8<sigma:float32,Y_rf:list(array(float32, 2d, A)),Y_te:list(array(float32, 2d, A)),k:int32,alphas:array(float32, 1d, A),b:array(float32, 1d, A)>
[3] During: typing of call at <string> (3)
Enable logging at debug level for details.
File "<string>", line 3:
<source missing, REPL/exec in use?>
我还尝试使用 numba.types.List
中的 List.empty_list(float32[:, :])
而不是 [np.array([[0.]], dtype=float32)]
。但它也不起作用。如何解决?
你的代码片段有一个问题,你正在尝试使用 Numba dtype 创建一个 Numpy 数组。
np.array([[1, 2], [3, 4]], dtype=np.float32) # OK
np.array([[1, 2], [3, 4]], dtype=nb.float32) # Not OK
但是,主要问题是您需要使用 numba.types.npytypes.Array
指定列表类型。这与使用 float32([:,:])
.
指定数组的函数签名不同
import numba as nb
import numpy as np
_spec = [
('Y_rf', nb.types.List(nb.types.Array(nb.types.float32, 2, 'C'))),
('sigma', nb.types.int32)
]
@jitclass(_spec)
class DensityRatioEstimation:
def __init__(self, sigma):
self.sigma = sigma
self.Y_rf = [np.array([[1, 2], [3, 4]], dtype=np.float32)]
dre = DensityRatioEstimation(1)
dre.Y_rf
输出
[array([[1., 2.],
[3., 4.]], dtype=float32)]
我有一些 class:
from numba import jitclass, int32, float32, types
from numba.typed import List
_spec = [
('Y_rf', types.List(float32[:, :])),
...
]
@jitclass(_spec)
class DensityRatioEstimation:
def __init__(self, sigma):
self.sigma = sigma
self.Y_rf = [np.array([[0.]], dtype=float32)]
但我做不到。它总是因不同的错误而中断。现在错误是:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.typeinfer.CallConstraint object at 0x00000277CBBBF550>.
Failed in nopython mode pipeline (step: nopython mode backend)
File "src\models\DDRE.py", line 26:
def __init__(self, sigma):
<source elided>
self.sigma = sigma
self.Y_rf = [np.array([[0.]], dtype=float32)]
^
[1] During: lowering "(self).Y_rf = [=11=].11" at D:\anomaly-detection\src\models\DDRE.py (26)
[2] During: resolving callee type: jitclass.DensityRatioEstimation#277c8a6cdd8<sigma:float32,Y_rf:list(array(float32, 2d, A)),Y_te:list(array(float32, 2d, A)),k:int32,alphas:array(float32, 1d, A),b:array(float32, 1d, A)>
[3] During: typing of call at <string> (3)
Enable logging at debug level for details.
File "<string>", line 3:
<source missing, REPL/exec in use?>
我还尝试使用 numba.types.List
中的 List.empty_list(float32[:, :])
而不是 [np.array([[0.]], dtype=float32)]
。但它也不起作用。如何解决?
你的代码片段有一个问题,你正在尝试使用 Numba dtype 创建一个 Numpy 数组。
np.array([[1, 2], [3, 4]], dtype=np.float32) # OK
np.array([[1, 2], [3, 4]], dtype=nb.float32) # Not OK
但是,主要问题是您需要使用 numba.types.npytypes.Array
指定列表类型。这与使用 float32([:,:])
.
import numba as nb
import numpy as np
_spec = [
('Y_rf', nb.types.List(nb.types.Array(nb.types.float32, 2, 'C'))),
('sigma', nb.types.int32)
]
@jitclass(_spec)
class DensityRatioEstimation:
def __init__(self, sigma):
self.sigma = sigma
self.Y_rf = [np.array([[1, 2], [3, 4]], dtype=np.float32)]
dre = DensityRatioEstimation(1)
dre.Y_rf
输出
[array([[1., 2.],
[3., 4.]], dtype=float32)]