gpt4 book ai didi

python - 有没有办法在 numba jitclass 中包含字符串属性?

转载 作者:行者123 更新时间:2023-12-01 01:04:20 24 4
gpt4 key购买 nike

我知道 numba 现在支持字符串,但找不到任何有关如何将字符串与 jitclass 一起使用的文档,并且无法弄清楚。

如何使用 jitclass 创建字符串属性?

(这个 hack 是预字符串支持并且相当困惑: How can I pass string type in class in numba jitclass python? )

我尝试过 unicode_type、char、char[:]、uint8、str ——基本上我能想到的一切。

COND_SPEC = [
('feature',nb.unicode_type),
('val', nb.unicode_type)
]

@jitclass(COND_SPEC)
class Cond:
""" Class implementing conditional. """

def __init__(self, feature, val):
self.feature = feature
self.val = val

该类可以编译,但声明该类的实例会产生错误:

c = Cond('education','HS-grad')
numba.errors.LoweringError: Failed in nopython mode pipeline (step: nopython mode backend)
Cannot cast unicode_type to int8: %".37" = load {i8*, i64, i32, i64, i8*, i8*}, {i8*, i64, i32, i64, i8*, i8*}* %"feature"

File "<ipython-input-19-aaeb1c1955cb>", line 12:
def __init__(self, feature, val):
self.feature = feature
^

[1] During: lowering "(self).feature = feature" at <ipython-input-19-aaeb1c1955cb> (12)
[2] During: resolving callee type: jitclass.Cond#7f9c36758a18<feature:int8,val:int8>
[3] During: typing of call at <string> (3)

--%<----------------------------------------------------------------------------


File "<string>", line 3:
<source missing, REPL/exec in use?>

最佳答案

我相信这是numba.types.string:

import numba as nb
from numba import jitclass

COND_SPEC = [
('feature', nb.types.string),
('val', nb.types.string)
]

@jitclass(COND_SPEC)
class Cond:
""" Class implementing conditional. """

def __init__(self, feature, val):
self.feature = feature
self.val = val

c = Cond('Hello', 'world')
print(c.feature, c.val)

>>>Hello world

关于python - 有没有办法在 numba jitclass 中包含字符串属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55522355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com