我遇到了關于使用 numba jit 裝飾器 ( @nb.jit) 的問題!這是來自 jupyter notebook 的警告,
NumbaWarning:編譯正在回退到啟用回圈提升的物件模式,因為函式“get_nb_freq”由于以下原因導致型別推斷失敗:未找到用于簽名的函式函式(<function dot
這是我的代碼:
@numba.jit def get_nb_freq( nb_count = None, onehot_ct = None): # nb_freq = onehot_ct.T @ nb_count nb_freq = np.dot(onehot_ct.T, nb_count) res = nb_freq/nb_freq.sum(axis = 1).reshape(Num_celltype,-1) return res ## onehot_ct is array, and its shape is (921600,4) ## nb_count is array, and its shape is the same as onehot_ct ## Num_celltype equals 4uj5u.com熱心網友回復:
根據您提到的形狀,我們可以將陣列創建為:
onehot_ct = np.random.rand(921600, 4) nb_count = np.random.rand(921600, 4)您準備的代碼將正常作業并得到如下答案:
[[0.25013102754197963 0.25021461207825463 0.2496806287276126 0.24997373165215303] [0.2501574139037384 0.25018726649940737 0.24975108864220968 0.24990423095464467] [0.25020550587624757 0.2501303498983212 0.24978335463279314 0.24988078959263807] [0.2501855533482036 0.2500913419625523 0.24979681404573967 0.24992629064350436]]因此,它表明代碼正在運行,并且問題似乎與陣列的型別有關,numba 無法識別它們。因此,簽名在這里可能會有所幫助,我們可以手動識別函式的型別。因此,基于錯誤,我認為以下簽名將通過您的問題:
@nb.jit("float64[:, ::1](float64[:, ::1], float32[:, ::1])") def get_nb_freq( nb_count = None, onehot_ct = None): nb_freq = np.dot(onehot_ct.T, nb_count) res = nb_freq/nb_freq.sum(axis=1).reshape(4, -1) return res但是如果你測驗它會再次卡住
get_nb_freq(nb_count.astype(np.float64), onehot_ct.astype(np.float32)),所以另一個原因可能與不相等的型別有關np.dot。因此,使用onehot_ct陣列作為陣列型別np.float64,可以傳遞問題:@nb.jit("float64[:, ::1](float64[:, ::1], float32[:, ::1])") def get_nb_freq( nb_count, onehot_ct): nb_freq = np.dot(onehot_ct.astype(np.float64).T, nb_count) res = nb_freq/nb_freq.sum(axis=1).reshape(4, -1) return res它在我的機器上運行了這個更正。我建議撰寫 numba 等效代碼(例如 for
np.dot)而不是使用np.dotor ...,這樣可以更快。轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/497791.html

