python - Numba:初始化一个 jitclass 装饰类的成员生成器
问题描述
在尝试使用如下所述初始化一个简单的类时,我遇到了两个问题,
# Python Version : 3.8.3
import numpy as np
# np.__version__ : 1.18.5
import numba as nb
# nb.__version__ : 0.50.1
mydict_like = nb.typed.Dict()
mydict_like['SomeKey'] = np.ones((100,6), dtype=np.float64)
myspecs = [('mydict', nb.typeof(mydict_like)),
# ('tid_gen', #<----What Type??--->#)
]
@nb.experimental.jitclass(spec=myspecs)
class MyClass:
def __init__(self):
pass
def _initialiser(self, new_dict):
self.mydict = new_dict
# This is where the initialisation of the generator is supposed to
# take place. Although i am unsure of what type would be of the
# the attribute `tid_gen` to be difined in `myspecs`.
# self.tid_gen = self._unique_id_generator()
@staticmethod
def _unique_id_generator():
"""
At every next() call this generator needs to yield the next number.
For Ex:
- next(self.tid_gen) => 0
- next(self.tid_gen) => 1
- next(self.tid_gen) => 2
- next(self.tid_gen) => 3
- [next(self.tid_gen) for _ in range(5)] => [4,5,6,7,8]
"""
n=0 #First Id
while True:
yield n
n+=1
clsObj = MyClass()
# A new Dictionary
updated_dict = nb.typed.Dict()
updated_dict['RandomKey'] = np.ones((200,12), dtype=np.float64)
# clsObj._initialiser(new_dict=updated_dict) # TypeError: some keyword arguments unexpected :: Why??
clsObj._initialiser(updated_dict)
print(clsObj.mydict)
第一个问题:当我尝试将类对象初始化为时,我收到如下所述的错误
clsObj
:clsObj._initialiser(new_dict=updated_dict)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-542-4ab32d47645b> in <module> 37 updated_dict['RandomKey'] = np.ones((200,12), dtype=np.float64) 38 ---> 39 clsObj._initialiser(new_dict=updated_dict) # TypeError: some keyword arguments unexpected :: Why?? 40 # clsObj._initialiser(updated_dict) 41 print(clsObj.mydict) c:\users\130453\anaconda3\lib\site-packages\numba\experimental\jitclass\boxing.py in wrapper(*args, **kwargs) 58 @wraps(func) 59 def wrapper(*args, **kwargs): ---> 60 return method(*args, **kwargs) 61 62 return wrapper TypeError: some keyword arguments unexpected
尽管目前这不是那么紧迫的问题,但我仍然想知道为什么会这样?
- 第二个问题:我正在尝试在
_initialiser
方法中初始化一个类的成员生成器函数,并且我知道每个属性类型都需要预先声明,以便 numba 能够对其进行 jit 编译。但是,该类型的属性会是什么?如果这种方法不可行,欢迎使用任何其他替代方法。只要记住这一点- 每当
_initializer
调用 时,_unique_id_generator
(或类似的功能)需要再次初始化。
- 每当
任何帮助将不胜感激:) 谢谢。
解决方案
推荐阅读
- java - UnsatisfiedDependencyException:创建名为 'procjectController' 的 bean 时出错:通过字段表示的不满足依赖项
- c# - TcpListener 等待客户端和一个队列同时处理
- java - Java 处理 cURL 的方式不同吗?
- python - 如何优化由多个变量组成的结果
- django - Django - 相对模型
- python - 为什么 socket.recv() 有时会阻塞,有时不会?
- ionic-framework - Ionic 4 手动覆盖离子选择
- javascript - 如何将 Naver Analytics 添加到 Nuxt.js SPA 应用程序?
- c# - 试图在后台工作人员在 C# 中显示用户交互表单时停止主线程
- python - 如何在匹配来自不同数据框的另一列的行后更新一列 dF 的值