Python Numba函数无法从Numba修饰的生成器函数附加到列表
Numba无法在Numba修饰的生成器中执行Python Numba函数无法从Numba修饰的生成器函数附加到列表,python,generator,numba,Python,Generator,Numba,Numba无法在Numba修饰的生成器中执行\uuuuuuuuuuuuuuuuuuuuu next()调用。错误显示UniTuple(float64 x 4)类型的Unknown属性'\uuuuuu next\uuuuuuuu' 完整错误输出为 TypingError: Failed in nopython mode pipeline (step: nopython frontend) Unknown attribute '__next__' of type UniTuple(float64 x
\uuuuuuuuuuuuuuuuuuuuu next()
调用。错误显示UniTuple(float64 x 4)类型的Unknown属性'\uuuuuu next\uuuuuuuu'
完整错误输出为
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute '__next__' of type UniTuple(float64 x 4) generator(func=<function random_walk at 0x7fbe39806488>, args=(int64, int64, float64, float64, int64), has_finalizer=True)
File "rw_nb.py", line 47:
def random_walk_simulation(initial_position = 0, acceleration = 0,
<source elided>
data = []
data.append(rw.__next__())
^
[1] During: typing of get attribute at /home/igor/rw_nb.py (47)
File "rw_nb.py", line 47:
def random_walk_simulation(initial_position = 0, acceleration = 0,
<source elided>
data = []
data.append(rw.__next__())
^
TypingError:在nopython模式管道中失败(步骤:nopython前端)
UniTuple(float64 x 4)生成器(func=,args=(int64,int64,float64,float64,int64)类型的未知属性“\uuuuu next\uuuuuu”具有\u finalizer=True)
文件“rw_nb.py”,第47行:
def随机行走模拟(初始位置=0,加速度=0,
数据=[]
data.append(rw.\uuuu next\uuuu())
^
[1] 期间:在/home/igor/rw_nb.py中键入get属性(47)
文件“rw_nb.py”,第47行:
def随机行走模拟(初始位置=0,加速度=0,
数据=[]
data.append(rw.\uuuu next\uuuu())
^
MWE源代码如下所示
import random
import numba
import numpy as np
@numba.njit
def random_walk(s_0, a_0, pa, pb, seed=None):
"""Initial position (often 0), acceleration, 0 < pa < pb < 1"""
if seed is not None:
random.seed(seed)
# Time, x-position, Velocity, Acceleration
t, x, v, a = 0, s_0, 0, a_0
yield (t, x, v, a)
while True:
# Roll the dices
rnd = random.random()
if rnd <= pa:
# Increase acceleration
a += .005
elif rnd <= pa+pb:
# Reduce acceleration
a -= .005
# Lets avoid too much acceleration
#lower, upper = -0.2, 0.2
a = -0.2 if a < -0.2 else 0.2 if a > 0.2 else a
# How much time has passed, since last update?
dt = random.random()
v += dt*a
x += dt*v
t += dt
yield (t, x, v, a)
@numba.njit
def random_walk_simulation(initial_position = 0, acceleration = 0,
prob_increase=5e-3, prob_decrease=5e-3,
max_distance=1e5, simul_time=1e3,
seed=None):
rw = random_walk(initial_position, acceleration,
prob_increase, prob_decrease, seed)
# Runs the first iteraction
data = []
data.append(rw.__next__())
# While there is simulation time or not too far away
while (data[-1][0] < simul_time) and (abs(data[-1][1]) < max_distance):
data.append(rw.__next__())
return np.array(data)
def main():
experiment = random_walk_simulation(seed=0)
print(experiment.shape)
if __name__ == '__main__':
main()
随机导入
进口麻木
将numpy作为np导入
@纳巴恩吉特
def随机游动(s_0,a_0,pa,pb,种子=无):
“”“初始位置(通常为0),加速度,0 如果rnd调用Numba中生成器的下一项,而不是使用data.append(rw.\uuu next\uuuu())
您可以:
data.append(next(rw))
我的天啊。太差劲了。我用错了发电机。你能写一个答案来获得正确的信用吗?