我是 Python 新手。我想使用 Python 進行我的數值實驗,其中我需要準確地解決許多動態規劃問題。因此,優化我的代碼以提高效率很重要。我的代碼實際上可以與帶有 Numba 的 @jit 一起使用,但我想通過 @njit 進一步提升性能。您可以從下面的代碼中看到,我試圖在 for 回圈中對我的操作進行矢量化以提高效率。正如我之前提到的,@jit 作業正常,但使用@njit,它不斷給我錯誤訊息。眾所周知,精確求解動態程式是計算密集型的,因此我真的很想使用@njit 來進一步提升性能。我真的可以使用一些幫助來更改代碼以允許@njit。非常感謝您!
我的代碼:
import numba as nb
import numpy as np
#DP computation
@nb.njit
def dp(beta,cost,wcost,decisions,number_of_stages,states):
tbeta=1-beta
odcost=min((cost[-max(decisions):] wcost)/beta[-max(decisions):])
terminal=(max(states)-states)*odcost
L=max(states)
D=number_of_stages
value=np.zeros((D 1,L 1))
choice=np.zeros((D 1,L)).astype(np.int64)
value[-1]=terminal
for s in range(D-1,L-2,-1):
intmatrix=cost[:, None] np.outer(beta,value[s 1][1:L 1]) np.outer(tbeta,value[s 1][0:L])
choice[s]=intmatrix.T.argmin(axis=1)
value[s][0:L]=intmatrix[choice[s],np.arange(intmatrix.shape[1])]
for s in range(L-2,-1,-1):
intmatrix=cost[:, None] np.outer(beta,value[s 1][1:s 2]) np.outer(tbeta,value[s 1][0:s 1])
choice[s][0:s 1]=intmatrix.T.argmin(axis=1)
value[s][0:s 1]=intmatrix[choice[s][0:s 1],np.arange(intmatrix.shape[1])]
return value, choice
#initialization
decisions=np.arange(100)
number_of_stages=200
states=np.arange(101)
np.random.seed(2021)
beta=np.append(0,np.random.uniform(0,1,max(decisions)))
wcost=np.random.uniform(0,1)
cost=np.square(beta)
value, choice=dp(beta,cost,wcost,decisions,number_of_stages,states)
錯誤資訊:
TypingError: No implementation of function Function(<built-in function getitem>) found for signature:
getitem(array(float64, 1d, C), Tuple(slice<a:b>, none))
There are 22 candidate implementations:
- Of which 20 did not match due to:
Overload of function 'getitem': File: <numerous>: Line N/A.
With argument(s): '(array(float64, 1d, C), Tuple(slice<a:b>, none))':
No match.
- Of which 2 did not match due to:
Overload in function 'GetItemBuffer.generic': File: numba\core\typing\arraydecl.py: Line 162.
With argument(s): '(array(float64, 1d, C), Tuple(slice<a:b>, none))':
Rejected as the implementation raised a specific error:
TypeError: unsupported array index type none in Tuple(slice<a:b>, none)
raised from C:\ProgramData\Anaconda3\lib\site-packages\numba\core\typing\arraydecl.py:68
uj5u.com熱心網友回復:
似乎 numba 不支持所有型別的 numpy 花式索引。你必須回到回圈。這在 numba 中很常見,您需要忘記 numpy、矢量化的編碼方式才能回到顯式回圈。
這應該可以完成這項作業,我只是明確地說明了回圈。
@nb.njit
def dp(beta, cost, wcost, decisions, number_of_stages, states):
tbeta = 1 - beta
odcost = min((cost[-max(decisions) :] wcost) / beta[-max(decisions) :])
terminal = (max(states) - states) * odcost
L = max(states)
D = number_of_stages
value = np.zeros((D 1, L 1))
choice = np.zeros((D 1, L)).astype(np.int64)
value[-1] = terminal
for s in range(D - 1, L - 2, -1):
intmatrix = (
cost.reshape(-1, 1)
np.outer(beta, value[s 1][1 : L 1])
np.outer(tbeta, value[s 1][0:L])
)
choice[s] = intmatrix.T.argmin(axis=1)
for i in range(intmatrix.shape[1]):
value[s][i] = intmatrix[choice[s][i], i]
for s in range(L - 2, -1, -1):
intmatrix = (
cost.reshape(-1, 1)
np.outer(beta, value[s 1][1 : s 2])
np.outer(tbeta, value[s 1][0 : s 1])
)
choice[s][0 : s 1] = intmatrix.T.argmin(axis=1)
for i in range(intmatrix.shape[1]):
value[s][0 : s 1][i] = intmatrix[choice[s][0 : s 1][i], i]
return value, choice
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/485479.html
