【问题标题】:Using a jitted function with a for loop inside使用带有 for 循环的 jited 函数
【发布时间】:2020-03-05 05:05:08
【问题描述】:

我有以下代码

@njit
    def ss(w,g,sm):
        kb = ( (α/(r*(1+τk)))**((1-γ)/(1-γ - α)) )* \
        ( (γ/(w*(1+τn)))**(γ/(1-γ-α)) )* \
        (smat*(1-τo))**(1/(1-γ-α)) ## ss capital

        nb = (1+τk)*r*γ/((1+τn)*w*α)*kb ## ss labor

        πb = (1-τo)*sm*(kb**α)*(nb**γ)- (1+τn)*w*nb-(1+τk)*r*kb-cf #ss profit
        W = πb/(1-0.0196) ## error in the code
        for i in range(ns):
            for j in range(nτ):
                xb[i,j] = 1 if W[i,j]>=0 else xb[i,j]
        we = sum(W*g*xb) - ce
        return we

据我所知它应该可以工作,但我不断收到以下错误

TypingError: Invalid use of Function(<built-in function setitem>) with argument(s) of type(s): (readonly array(float64, 2d, C), tuple(int64 x 2), float64)
 * parameterized
In definition 0:
    All templates rejected with literals.
In definition 1:
    All templates rejected without literals.
In definition 2:
    All templates rejected with literals.
In definition 3:
    All templates rejected without literals.
In definition 4:
    All templates rejected with literals.
In definition 5:
    All templates rejected without literals.
In definition 6:

我仍在寻找解决 python 的方法。我知道这是因为我正在使用 njit,但究竟是什么部分导致了它?如果我删除 for 循环,它可以正常工作,但我想知道 for 循环中的什么可能导致这种情况?

可重现的例子

import numpy as np
from numba import njit
class rep:
    def __init__(self, A = 1, B=2, C = 3, D = 4):
        self.A, self.B,self.C, self.D = A,B,C,D


def op(cls):
    A,B,C,D = cls.A, cls.B,cls.C, cls.D
    xb = np.zeros([10,10])
    @njit
    def rep1(w,g,sm):
        kb = A*3 + B*2
        nb = C*3 - D*w
        pib = sm*kb - nb
        W = pib/4
        for i in range(10):
            for j in range(10):
                xb[i,j] = 1 if W[i,j]>=0 else xb[i,j]
        we = np.sum(w*xb*g)
        return we
    return rep1


g = np.zeros([10,10])
sm = np.ones([10,10])
w = 1
r = rep()
rep1 = op(r)
print(rep1(w,g,sm))

【问题讨论】:

  • 几乎不可能回答一个有很多变量的问题,而没有它们是什么。 (Arrays-> which shape, Tuples,...必须重新编译,这可能是不想要的)
  • 添加了可重现的示例。所有数组都是 numpy 数组。错误似乎来自 for 循环,因为 xb 是在函数外部定义的。但即使我在里面定义它仍然给我同样的错误。它仅在 xb 是参数时才有效,但我的印象是 njit 可以在 numpy 数组上工作,即使它们不是参数。

标签: python loops for-loop jit numba


【解决方案1】:

这似乎与 cmets 中提到的 @max9111 相关 - xb 数组是在 jited 函数外部创建的,因此无法在内部修改。我通过将xb 移出op 并将其作为参数传递给rep1 对您的代码进行了一些更改,并且可以成功执行:

import numpy as np
from numba import njit
class rep:
    def __init__(self, A = 1, B=2, C = 3, D = 4):
        self.A, self.B,self.C, self.D = A,B,C,D


def op(cls):
    A,B,C,D = cls.A, cls.B,cls.C, cls.D
    @njit
    def rep1(w,g,sm,xb):
        kb = A*3 + B*2
        nb = C*3 - D*w
        pib = sm*kb - nb
        W = pib/4
        for i in range(10):
            for j in range(10):
                xb[i,j] = 1 if W[i,j]>=0 else xb[i,j]
        we = np.sum(w*xb*g)
        return we
    return rep1


g = np.zeros([10,10])
sm = np.ones([10,10])
xb = np.zeros([10,10])
w = 1
r = rep()
rep1 = op(r)
print(rep1(w,g,sm,xb))

【讨论】:

  • 这可行,但我希望不必将 xb 定义为变量。不过非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 2021-10-28
  • 2017-08-28
  • 1970-01-01
相关资源
最近更新 更多