【问题标题】:Use numba on classes?在课堂上使用 numba?
【发布时间】:2015-03-31 02:53:05
【问题描述】:

我可以在 0.6 numba 文档中找到一些关于如何使用 numba on classes 的信息:

from numba import jit, void, int_, double

# All methods must be given signatures

@jit
class Shrubbery(object):
    @void(int_, int_)
    def __init__(self, w, h):
        # All instance attributes must be defined in the initializer
        self.width = w
        self.height = h

        # Types can be explicitly specified through casts
        self.some_attr = double(1.0)

    @int_()
    def area(self):
        return self.width * self.height

    @void()
    def describe(self):
        print("This shrubbery is ", self.width,
              "by", self.height, "cubits.")

但我在0.16 documentation 中找不到。是否总是可以在类上使用 numba ?

【问题讨论】:

    标签: python-3.x numba


    【解决方案1】:

    从 0.23 版开始,有一个 numba.jitclass 方法。我可以说以下适用于 0.26 版

    @numba.jitclass([('width', numba.float64), ('height', numba.float64)])
    class Shrubbery(object):
        def __init__(self, w, h):
            self.width = w
            self.height = h
    
        def area(self):
            return self.width * self.height
    
    shrubbery = Shrubbery(10, 20)
    print(shrubbery.area())
    

    documentation 表示只允许使用方法和属性,因此很遗憾,包含 describe 函数目前将不起作用。

    【讨论】:

      【解决方案2】:

      我最后一次听说numba class support是他们temporarily removed it since 0.12

      【讨论】:

      • 关于 Travis 和他勇敢的numba 团队如何推动这一进程,您是否有任何消息或信号?也曾尝试直接询问小君临,所幸至今未有回应。感谢您提前分享您的观点。
      • 最近我没有时间与 Numba 合作,所以我没有关于这个主题的更多信息。对我来说,联系小君临的最佳方式是给他发一封电子邮件。他回答任何问题都非常快。如果这对您不起作用,您应该尝试google glroup
      • 谢谢@user4711332,也做了同样的事情,Siu 已经回答并表达了对未来的一些展望,如果类不使用Listdictionary 对象。
      猜你喜欢
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-03
      相关资源
      最近更新 更多