[Python] How to properly use njit/jit in Python?

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to write a program for value function iteration, and I want to utilize the nopython mode from numba library. The code below doesn't really do anything(I wanted to start from the beginning to understand where I am making a mistake). It should just return the matrices I create in the function. The inputs in the function are needed for optimization that I would do later. However, I am facing the error(please see below).

    I tried using @njit and also @jit that includes specific type I use for each inputs. However, both do not work.

    import numpy as np
    import matplotlib.pyplot as plt
    import mpl_toolkits.mplot3d
    import time
    from datetime import timedelta
    from numba import jit, njit, int32, float64
    from gridlookup import gridlookup


    beta = 0.99322
    sigma = 1.5
    enum = 2
    egrid = np.array([0.1, 1.0])
    pie = np.array([[0.5, 0.5],
    [0.075, 0.925]])
    blow = -2.0; bhigh = 4.0; bnum = 10
    bgrid = np.logspace(np.log(blow + -1.0*blow + 1.0)/np.log(10.0), np.log(bhigh + -1.0*blow + 1.0)/np.log(10.0), bnum)
    bgrid = bgrid + np.ones(np.shape(bgrid))*(blow - 1.0)
    mubgnum = 1000
    mubgrid = np.linspace(blow, bhigh, mubgnum)

    v0 = np.array(np.zeros((enum,bnum)))

    @njit
    def vfini(bnum, enum, bgrid, egrid, v0):
    ## calculate the initial value function.
    for i in range(bnum):
    bval = bgrid
    for m in range(enum):
    eval0 = egrid[m]
    yval = 0.025*bval + eval0
    v0[m,i] = (yval**(1-sigma))/(1.0-sigma)

    return v0

    v0 = vfini(bnum, enum, bgrid, egrid, v0)

    @jit([(int32, float64[:], float64[:,:], float64, float64, int32,
    float64[:], int32, float64[:], float64, float64[:,:],float64)],nopython=True)
    def huggettqegm(enum, egrid, pie, beta, sigma, bnum,
    bgrid, mubgnum, mubgrid, precision, v0, q):

    v_I = np.array(np.zeros((enum,bnum)))
    g_I = np.array(np.zeros((enum,bnum)))
    tv_I = np.array(np.zeros((enum,bnum)))
    tg_I = np.array(np.zeros((enum,bnum)))

    return v_I, g_I

    q = qlow

    v, g = huggettqegm(enum, egrid, pie, beta, sigma, bnum, bgrid, mubgnum, mubgrid, precision, v0, q)


    The below is the error message when I run the simple code above:


    TypingError: Invalid use of Function() with argument(s) of type(s): (array(float64, 2d, C)) * parameterized In definition 0: TypingError: array(float64, 2d, C) not allowed in a homogeneous sequence raised from C:\Anaconda3\lib\site-packages\numba\typing\npydecl.py:460 In definition 1: TypingError: array(float64, 2d, C) not allowed in a homogeneous sequence raised from C:\Anaconda3\lib\site-packages\numba\typing\npydecl.py:460 This error is usually caused by passing an argument of a type that is unsupported by the named function. [1] During: resolving callee type: Function() [2] During: typing of call at C:/Users/Jung Hwan Kim/Dropbox/StudentDebtCrisis/Program/Python/July012019/practice.py (124)
    ​

    I thought I could use the numpy to create the array and use them in the njit mode. Or maybe I am misunderstanding the basic use of njit.. I would really appreciate your help. Thank you very much!

    Continue reading...

Compartilhe esta Página