如何将numpy.double,numpy.single和struct.Struct类型的类属性转换为相应的numba类型,以便能够在jitclass中使用此类属性?
1)例如,一个类属性具有以下形式:
self.type = numpy.double
为了编译具有此类属性的python类,我必须将其映射为numba类型。我尝试了numba.typeof(numpy.double),但会产生以下错误
In [1]: a=numpy.double
In [2]: numba.typeof(a)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-244-7ab9d6ba8d0a> in <module>
----> 1 numba.typeof(a)
C:\ProgramData\Anaconda3\lib\site-packages\numba\typing\typeof.py in typeof(val
purpose)
32 msg = _termcolor.errmsg(
33 "cannot determine Numba type of %r") % (type(val),)
---> 34 raise ValueError(msg)
35 return ty
36
ValueError: cannot determine Numba type of <class 'type'>
2)在使用struct.Struct的情况下,我也会收到类似的错误
In [1]: from struct import Struct
In [2]: a = Struct(">dd")
In [3]: numba.typeof(a)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-76-7ab9d6ba8d0a> in <module>
----> 1 numba.typeof(a)
C:\ProgramData\Anaconda3\lib\site-packages\numba\typing\typeof.py in typeof(val,
purpose)
32 msg = _termcolor.errmsg(
33 "cannot determine Numba type of %r") % (type(val),)
---> 34 raise ValueError(msg)
35 return ty
36
ValueError: cannot determine Numba type of <class 'Struct'>
最小(工作)示例:
import numpy as np
import numba as nb
from struct import Struct
spec = [('type', nb.typeof(np.double)), ('h', nb.typeof(Struct(">f8")))]
@nb.jitclass(spec)
class TestClass:
def __init__(self):
self.type = np.double
self.h = Struct(">f8")
当我尝试编译上述类时,出现以下错误:
C:>python classtest.py
Traceback (most recent call last):
File "classtest.py", line 5, in <module>
spec = [('type', nb.typeof(np.double)), ('h', nb.typeof(Struct(">f8")))]
File "C:\ProgramData\Anaconda3\lib\site-packages\numba\typing\typeof.py", line
34, in typeof
raise ValueError(msg)
ValueError: ←[1mcannot determine Numba type of <class 'type'>←[0m
答案 0 :(得分:0)
如何将numpy.double,numpy.single和struct.Struct类型的类属性转换为相应的numba类型,以便能够在jitclass中使用此类属性?
对于import Register from './src/pages/Register';
import Login from >>insert login page path here<<;
const MainNavigator = createStackNavigator({
LoginPage : { screen : Login},
SignUp : {screen : Register}
}
);
和numpy.double
来说很容易,numpy.single
和numba.float64
是相应的数字类型。
在版本0.43.1中,numba.float32
也适用于NumPy类型,但是该函数期望的是实例而不是类本身:
numba.typeof
但是,目前(从numba的0.43.1版本开始)在numba中不支持>>> import numpy as np
>>> import numba as nb
>>> nb.__version__
'0.43.1'
>>> nb.typeof(np.double(10))
float64
(请参见"2.6. Supported Python features" of the numba documentation),因此您不能在numba中将struct
与nopython模式一起使用(包括struct
)。
当然,您可以将结构解压缩到各个组件中,如果numba支持它们,请改用它们。例如,您可以在jitclass
上添加两个'dd'
字段来代替numba.float64
。由于您一直在使用不同的结构,并且numba并不支持每种Python类型,因此您可能需要尝试一下。