如何在其中的代码中使用jit加速?

问题描述

我可以在不加速jit的情况下运行此代码

# @jit(nopython=True)
def mnc_bellman(param,kgrid,valorInit):
    import numpy as np
    # Declaramos los argumentos del problema de programacion dinamica
    alpha,delta,sigma,gamma,beta,tfp = param

    # Inicializamos objetos que vamos a necesitar
    tam = len(kgrid)
    matrizC = np.empty((tam,tam))
    valorObj = np.zeros((tam,tam))

    for i1 in range(0,tam):
        for i2 in range(0,tam):
            matrizC[i1,i2] = tfp * kgrid[i1] ** alpha \
                              + (1.0 - delta) * kgrid[i1] \
                              - kgrid[i2]

    # Un indicador de posiciones de consumo positivas
    indC = matrizC <= 0
    matrizU = matrizC ** (1.0 - sigma) / (1.0 - sigma)
    matrizU[indC] = -1.e+6

    # A partir de aqui podemos inicializar el algoritmo IteraCION de la
    # funcion de VALOR
    # Establecemos cierta tolerancia en el criterio de convergencia
    tol = 1.0e-6

    # Esta variable va a guardar cuantas veces iteramos hasta cumplir con los
    # criterios de convergencia
    cont = 0

    # Tambien tenemos que establecer un error inicial que no se satisfaga
    # trivialmente en el primer paso del bucle
    error = 10

    while error > tol:
        for i2 in range(0,tam):
            valorObj[:,i2] = matrizU[:,i2] + beta * valorInit[i2]
        pos = valorObj.argmax(axis=1)
        valor = valorObj.max(axis=1)
        error = np.max(np.abs(valor - valorInit))
        valorInit = valor
        cont += 1
        print(error)

    # Ahora reconstruimos las reglas de decision
    reglaK = kgrid[pos]
    reglaC = tfp * kgrid ** alpha + (1.0 - delta) * kgrid - reglaK

    return valor,reglaK,reglaC

取消注释@jit时,偶然发现以下错误

“ UnsupportedError:发现使用不受支持的操作码(IMPORT_NAME)”

我希望有人能使我适应这个问题。

谢谢

解决方法

这可以工作

import numpy as np
from numba import jit

@jit(nopython=True)
def mnc_bellman(param,kgrid,valorInit):
    # Declaramos los argumentos del problema de programacion dinamica
    alpha,delta,sigma,gamma,beta,tfp = param

    # Inicializamos objetos que vamos a necesitar
    tam = len(kgrid)
    matrizC = np.empty((tam,tam))
    valorObj = np.zeros((tam,tam))

    for i1 in range(0,tam):
        for i2 in range(0,tam):
            matrizC[i1,i2] = (
                tfp * kgrid[i1] ** alpha + (1.0 - delta) * kgrid[i1] - kgrid[i2]
            )

    # Un indicador de posiciones de consumo positivas
    indC = matrizC <= 0
    matrizU = matrizC ** (1.0 - sigma) / (1.0 - sigma)
    matrizU[indC] = -1.0e6

    # A partir de aqui podemos inicializar el algoritmo ITERACION de la
    # funcion de VALOR
    # Establecemos cierta tolerancia en el criterio de convergencia
    tol = 1.0e-6

    # Esta variable va a guardar cuantas veces iteramos hasta cumplir con los
    # criterios de convergencia
    cont = 0

    # Tambien tenemos que establecer un error inicial que no se satisfaga
    # trivialmente en el primer paso del bucle
    error = 10

    while error > tol:
        for i2 in range(0,tam):
            valorObj[:,i2] = matrizU[:,i2] + beta * valorInit[i2]
        pos = valorObj.argmax(axis=1)
        valor = valorObj.max(axis=1)
        error = np.max(np.abs(valor - valorInit))
        valorInit = valor
        cont += 1
        print(error)

    # Ahora reconstruimos las reglas de decision
    reglaK = kgrid[pos]
    reglaC = tfp * kgrid ** alpha + (1.0 - delta) * kgrid - reglaK

    return valor,reglaK,reglaC

该错误表明未定义导入名称的操作码,您应该在jit装饰器之外定义import语句。