我對 numba (和發布問題)非常缺乏經驗,所以希望這不是一個未指定的問題。
我正在嘗試創建一個涉及字典的 jitted 函式。我希望字典將元組作為鍵,并將浮點數作為值。下面是numba 檔案中的 numba 幫助中的一些代碼,我用來幫助演示我的問題。
我了解 numba 想要指定變數型別。我認為的問題是我沒有將正確的 numba 型別指定為函式內的字典鍵。我已經看過這個問題 ,但仍然無法弄清楚該怎么做。
import numpy as np
from numba import njit
from numba import types
from numba.typed import Dict
# Make array type. Type-expression is not supported in jit functions.
float_array = types.float64[:]
@njit
def foo():
list_out=[]
# Make dictionary
d = Dict.empty(
key_type=types.Tuple, #<= I suppose im not putting the right 'type' here
value_type=float_array,
)
# an example of how I would like to fill the dictionary
d[(1,1)] = np.arange(3).astype(np.float64)
d[(2,2)] = np.arange(3, 6).astype(np.float64)
list_out.append(d[(2,2)])
return list_out
list_out = foo()
任何幫助或指導表示贊賞。謝謝你的時間!
uj5u.com熱心網友回復:
types.Tuple是不完整的型別,因此不是有效的型別。您需要指定元組中專案的型別。在這種情況下,您可以使用types.UniTuple(types.int32, 2)完整的鍵型別(包含兩個 32 位整數的元組)。這是生成的代碼:
import numpy as np
from numba import njit
from numba import types
from numba.typed import Dict
# Make key type with two 32-bit integer items.
key_type = types.UniTuple(types.int32, 2)
# Make array type. Type-expression is not supported in jit functions.
float_array = types.float64[:]
@njit
def foo():
list_out=[]
# Make dictionary
d = Dict.empty(
key_type=key_type,
value_type=float_array,
)
# an example of how I would like to fill the dictionary
d[(1,1)] = np.arange(3).astype(np.float64)
d[(2,2)] = np.arange(3, 6).astype(np.float64)
list_out.append(d[(2,2)])
return list_out
list_out = foo()
順便說一句,請注意arange接受一個dtypein 引數,以便您可以np.arange(3, dtype=np.float64)直接使用,使用時更有效astype。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/445167.html
上一篇:基于互動式地圖選擇更新復選框
