python - Numba、Jitclass w/nopython 模式和字典

标签 python numpy dictionary numba

尝试找出如何在使用 jitclass 时跳过类方法。

有一个相当大的递归模型(几乎是一个巨大的 for 循环),在给定路径相关计算的情况下,无法使用直接 Numpy 进行矢量化。

该类运行一系列 numpy 数组,通常具有 numba 友好的语法,但是我有一个部分以有序的方式调用一些方法:

def operations(self, i, ops_order_config):

    ops_dict = self.ops_dict

    for index in range(len(waterfall_config)):
        try:
            if isinstance(ops_config[index], tuple):
                ops_dict[ops_config[index][0]](i, ops_config[index][1])
            else:
                ops_dict[ops_config[index]](i)
        except KeyError:
            pass

模型的这一部分对于灵活性非常重要 - “配置”是元组的有序列表,其中包含要调用的适当方法以及相应的参数。 ops_dict 保存实际的 self。这是从配置中调用的,带有适当的参数。

如果我正在制作一个 jitclass,有什么方法可以跳过这个字典方面的 jitting 吗?

最佳答案

不,如果您创建 jitclass每个属性都必须输入,并且从 numba 0.34 开始不支持包含函数(即使是 jitted)的字典或列表/元组。例如尝试使用 dictobject作为类型:

import numpy as np
from numba import jitclass

spec = [('dct', dict)]

@jitclass(spec)
class ClsWithObject(object):
    def __init__(self, value):
        self.dct = {}

抛出 TypeError :

TypeError: spec values should be Numba type instances, got <class 'dict'>

此外,使用 isinstance以及 tryexcept在 nopython 模式下也不起作用。

您最好的选择是使用 jit从纯 Python 类中调用的 ted 函数。

关于python - Numba、Jitclass w/nopython 模式和字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45924666/

相关文章:

python - 如何在python中逐行并行读取两个文件?

python - 根据索引列表中的索引获取项目列表

python - 在 python 中,我如何从数组的元素中获取字符串的一部分

javascript - 在 JS 中给字典键加上引号

python - numpy.irr 和 Excel IRR 函数的 IRR 结果不同

python - 替换 numpy 数组中所有元素的最快方法

ios - 如何获取一个数组,它是 Swift 中字典键的值?

python - 在opencv中将轮廓转换为二维点列表

python - 按标签名称在元素内按标签名称查找元素(Selenium)

python - python中的同时模拟