未键入的全局名称'pvc_sim':无法确定<class'skfuzzy.control.controlsystem.ControlSystemSimulation'>

时间:2019-08-13 16:40:41

标签: python numba skfuzzy

以下代码出现错误:

@vectorize(["float32(float32, float32, float32, float32)"], target='cuda')
def fuzz_comp(A_row, B_col, X, Y):
    for i in range(A_row):
        for j in range(B_col):
            pvc_sim.input['ipv'] = float(X[i,j])
            pvc_sim.compute()
            Y[i,j] = pvc_sim.output['opv']
    return Y

错误:

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Untyped global name 'pvc_sim': cannot determine Numba type of <class 'skfuzzy.control.controlsystem.ControlSystemSimulation'>

File "<ipython-input-13-4fe6c3f24f69>", line 5:
def fuzz_comp(A_row, B_col, X, Y):
    <source elided>
        for j in range(B_col):
            pvc_sim.input['ipv'] = float(X[i,j])
            ^

请通读一些文章,但无法解决该问题。该代码在Python中运行。使用装饰器时返回错误。我正在尝试在GPU中实现模糊计算。

2 个答案:

答案 0 :(得分:0)

该错误告诉您numba不知道如何键入全局变量pvc_sim。它似乎是python类,因此numba无法自动将其转换为低级代码。您可以编写专门的jitclass,但否则,此代码将失败。另外,从实现的角度来看,我尚不清楚它实际上会从cuda目标中受益。请记住,@vectorize函数是为在标量上运行而编写的,然后numba正确处理数组输入,并将函数应用于每个元素。

答案 1 :(得分:0)

感谢@JoshAdel。我可以使用jitclass开发用于输入和输出成员资格函数的以下代码。

spec = [
    ('value', int64),               # a simple scalar field
    ('array', float64[:]),          # an array field
]

@jitclass(spec)
class io_mf(object):
    def __init__(self, value, array):
        self.value = value
        self.array = array

    @property
    def triangular(self):
        if (self.array[0] - self.array[1]) <= self.value and self.value < self.array[0]:
            return 1. - (self.array[0] - self.value) / self.array[1]
        if self.array[0] <= self.value and self.value <= (self.array[0] + self.array[2]):
            return 1. - (self.value - self.array[0]) / self.array[2]
        return 0.

    @property
    def trapezoidal(self):
        if (self.array[0] - self.array[2]) <= self.value and self.value < self.array[0]:
            return 1. - (self.array[0] - self.value) / self.array[2]
        if self.array[0] <= self.value and self.value < self.array[1]: 
            return 1.
        if self.array[1] <= self.value and self.value <= (self.array[1] + self.array[3]):
            return 1. - (self.value - self.array[1]) / self.array[3]
        return 0