简体   繁体   English

Python - Numba 产生 For-Loop 的语法错误

[英]Python - Numba produces syntax error for For-Loop

I have the following code in which I create two 1D NumPy arrays (f0src and f1src) and then create a two for loop for calculate some function with every combination of f0src and f1src arrays. I have the following code in which I create two 1D NumPy arrays (f0src and f1src) and then create a two for loop for calculate some function with every combination of f0src and f1src arrays. I pre-allocate the output to fasten the process (z3_2d).我预先分配了 output 来加快进程(z3_2d)。

n   = 50
f0  = 5.073174792651648
f1  = -1.50272e-13
df0  = 1e-7 
df1 = 8e-15

f0src = np.arange(f0 - n * df0, f0 + n * df0, df0)
f1src = np.arange(f1 - n * df1, f1 + n * df1, df1)

f0shape=f0src.shape[0]
f1shape=f1src.shape[0]
z3_2d = np.zeros([f0shape,f1shape])

%%time

for idxf0, f0 in enumerate(f0src):

    for idxf1, f1 in enumerate(f1src):

        phase=my_phase(mytime,f0,f1) #mytime is another 1D array around 100k
        z3=z_n(phase, n=3, norm=1)
        z3_2d[idxf0, idxf1]=np.copy(z3)

This works fine and gives me the output I desire but it is slow and with relatively a small sample array it takes around 1:35 second这工作得很好,给了我我想要的 output 但它很慢,而且样本阵列相对较小,大约需要 1:35 秒

CPU times: user 1min 34s, sys: 564 ms, total: 1min 34s Wall time: 1min 35s CPU时间:用户1分34秒,系统:564毫秒,总计:1分34秒挂壁时间:1分35秒

z3_2d.shape ---> (100, 100) z3_2d.shape ---> (100, 100)

Based on what I read, Numba can fasten the process quite much, especially if you are using NumPy arrays, you pre-allocate output and have for loops which is exactly my case.根据我读到的内容, Numba可以大大加快这个过程,特别是如果你使用 NumPy arrays,你预先分配 output 和 for 循环。 So I tried the same thing by just adding njit before the function所以我通过在function 之前添加 njit来尝试同样的事情

@njit
for idxf0, f0 in enumerate(f0src):

    for idxf1, f1 in enumerate(f1src):

        phase=my_phase(mytime,f0,f1)
        z3=z_n(phase, n=3, norm=1)
        z3_2d[idxf0, idxf1]=np.copy(z3)

However, I am getting the following error that I don't fully understand.但是,我收到以下我不完全理解的错误。

File "/Users/sara/opt/miniconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3444, in run_code exec(code_obj, self.user_global_ns, self.user_ns)文件“/Users/sara/opt/miniconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py”,第 3444 行,在 run_code exec(code_obj, self.user_global_ns, self.user_ns)

File "/var/folders/y9/nvl5y5_15v7cx8wb6nv3lzxc0000gn/T/ipykernel_17771/4021014180.py", line 1, in get_ipython().run_cell_magic('time', '', '\n@njit\nfor idxf0, f0 in enumerate(f0src):\n\n for idxf1, f1 in enumerate(f1src):\n\n文件“/var/folders/y9/nvl5y5_15v7cx8wb6nv3lzxc0000gn/T/ipykernel_17771/4021014180.py”,第 1 行,在 get_ipython().run_cell_magic('time', '', '\n@njit\nfor idxf0, f0 in enumerate( f0src):\n\n for idxf1, f1 in enumerate(f1src):\n\n
phase=pulse_phase(timemerged,f0,f1)\n z3=z_n(phase, n=3, norm=1)\n z3_2d[idxf0, idxf1]=np.copy(z3)\n') phase=pulse_phase(timemerged,f0,f1)\n z3=z_n(phase, n=3, norm=1)\n z3_2d[idxf0, idxf1]=np.copy(z3)\n')

File "/Users/sara/opt/miniconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 2406, in run_cell_magic result = fn(*args, **kwargs)文件“/Users/sara/opt/miniconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py”,第 2406 行,在 run_cell_magic 结果 = fn(*args, **kwargs)

File "/Users/sara/opt/miniconda3/lib/python3.9/site-packages/decorator.py", line 232, in fun return caller(func, *(extras + args), **kw)文件“/Users/sara/opt/miniconda3/lib/python3.9/site-packages/decorator.py”,第 232 行,在 fun return caller(func, *(extras + args), **kw)

File "/Users/sara/opt/miniconda3/lib/python3.9/site-packages/IPython/core/magic.py", line 187, in call = lambda f, *a, **k: f(*a, **k)文件“/Users/sara/opt/miniconda3/lib/python3.9/site-packages/IPython/core/magic.py”,第 187 行,调用中 = lambda f, *a, **k: f(*a , **k)

File "/Users/sara/opt/miniconda3/lib/python3.9/site-packages/IPython/core/magics/execution.py", line 1280, in time expr_ast = self.shell.compile.ast_parse(expr)文件“/Users/sara/opt/miniconda3/lib/python3.9/site-packages/IPython/core/magics/execution.py”,第 1280 行,及时 expr_ast = self.shell.compile.ast_parse(expr)

File "/Users/sara/opt/miniconda3/lib/python3.9/site-packages/IPython/core/compilerop.py", line 101, in ast_parse return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)文件“/Users/sara/opt/miniconda3/lib/python3.9/site-packages/IPython/core/compilerop.py”,第 101 行,在 ast_parse 返回编译(源,文件名,符号,self.flags | PyCF_ONLY_AST, 1)

File "", line 2 for idxf0, f0 in enumerate(f0src): ^ SyntaxError: invalid syntax文件“”,第 2 行 for idxf0, f0 in enumerate(f0src): ^ SyntaxError: invalid syntax

I appreciate it if you could point me in the right direction.如果您能指出正确的方向,我将不胜感激。

@njit is a function decorator. @njit 是一个 function 装饰器。 This means that it should wrap some function, and, thus, you should provide it a function, not loop.这意味着它应该包装一些 function,因此,您应该为其提供 function,而不是循环。 Use the following:使用以下内容:

@njit    
def foo(mytime, f0src, f1src, z3_2d):
   for idxf0, f0 in enumerate(f0src):

      for idxf1, f1 in enumerate(f1src):

         phase=my_phase(mytime,f0,f1)
         z3=z_n(phase, n=3, norm=1)
         z3_2d[idxf0, idxf1]=np.copy(z3)
foo(mytime, f0src, f1src, z3_2d)

This code creates some function foo with operations you need, and uses @njit decorator to wrap it.此代码使用您需要的操作创建一些 function foo,并使用 @njit 装饰器来包装它。 The last argument is mutable, so it will store the result as expected.最后一个参数是可变的,因此它将按预期存储结果。 Notice that you should also use @njit decorator for other functions in foo (my_phase, z_n).请注意,您还应该对 foo (my_phase, z_n) 中的其他函数使用 @njit 装饰器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM