代码之家  ›  专栏  ›  技术社区  ›  WZhao

为什么numba在numpy linspace中提出类型错误

  •  2
  • WZhao  · 技术社区  · 7 年前

    我正在使用 numba numpy 1.13.1. 下面是一个小示例:

    import numpy as np    
    from numba import jit
    @jit(nopython=True)
    def initial(veh_size):
        t = np.linspace(0, (100 - 1) * 30, 100, dtype=np.int32)
        t0 = np.linspace(0, (veh_size - 1) * 30, veh_size, dtype=np.int32)
        return t0
    
    initial(100)
    

    这两条线 t t0

    numba.errors.InternalError: 
    [1] During: resolving callee type: Function(<function linspace at 0x000001F977678C80>)
    [2] During: typing of call at ***/test1.py (6)
    
    1 回复  |  直到 7 年前
        1
  •  8
  •   Community Mike Causer    4 年前

    因为麻木版的 np.linspace 不接受 dtype 参数( source: numba 0.34 documentation

    支持以下顶级功能:

    • [...]

    • numpy.linspace()

    • [...]

    你需要使用 astype 要在nopython Nuba函数内转换它:

    import numpy as np    
    from numba import jit
    @jit(nopython=True)
    def initial(veh_size):
        t = np.linspace(0, (100 - 1) * 30, 100).astype(np.int32)
        t0 = np.linspace(0, (veh_size - 1) * 30, veh_size).astype(np.int32)
        return t0
    
    initial(100)
    

    np。林空间 比NumPys快。