numba @guvectorize([([((float64 [:],int64)]],'(n),() - >(n)')indexError:列表index out Range
我尝试将numba应用于我的代码以提高速度。但是我遇到了一个错误“ indexError:列表以外的索引”和警告。我如何使它起作用?
我按照下面的方式运行代码:
import numpy as np
from numpy import linalg as LA
from numba import guvectorize, int64, float64, vectorize
@guvectorize([(float64[:], int64)], '(n),()->(n)')
def func(main, n):
nprime = n-1
off = np.random.normal(size=(nprime, nprime))
tril = np.tril(off, -1)
W_n = tril + tril.T
np.fill_diagonal(W_n, main)
eigenvalues = LA.eigvals(W_n)
w = np.flip(np.sort(eigenvalues))
# GOE_L12_dist[:,i] = w[0:2]
return w
main = np.sqrt(2) * np.random.normal(size=(3-1))
func(main, 3)
然后我会遇到错误,
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Input In [16], in <cell line: 1>()
1 @guvectorize([(float64[:], int64)], '(n),()->(n)')
----> 2 def func(main, n):
3 nprime = n-1
4 off = np.random.normal(size=(nprime, nprime))
File /opt/anaconda3/lib/python3.9/site-packages/numba/np/ufunc/decorators.py:197, in guvectorize.<locals>.wrap(func)
195 if len(ftylist) > 0:
196 guvec.disable_compile()
--> 197 return guvec.build_ufunc()
File /opt/anaconda3/lib/python3.9/site-packages/numba/np/ufunc/gufunc.py:66, in GUFunc.build_ufunc(self)
65 def build_ufunc(self):
---> 66 self.ufunc = self.gufunc_builder.build_ufunc()
67 return self
File /opt/anaconda3/lib/python3.9/site-packages/numba/core/compiler_lock.py:35, in _CompilerLock.__call__.<locals>._acquire_compile_lock(*args, **kwargs)
32 @functools.wraps(func)
33 def _acquire_compile_lock(*args, **kwargs):
34 with self:
---> 35 return func(*args, **kwargs)
File /opt/anaconda3/lib/python3.9/site-packages/numba/np/ufunc/ufuncbuilder.py:363, in GUFuncBuilder.build_ufunc(self)
360 nout = len(self.sout)
362 # Pass envs to fromfuncsig to bind to the lifetime of the ufunc object
--> 363 ufunc = _internal.fromfunc(
364 self.py_func.__name__, self.py_func.__doc__,
365 func_list, type_list, nin, nout, datalist,
366 keepalive, self.identity, self.signature,
367 )
368 return ufunc
IndexError: list index out of range
并显示警告,
/var/folders/9_/y3v35tk14nl7_b9ks8h4l79c0000gn/T/ipykernel_4300/1986471478.py:1: NumbaWarning:
Compilation is falling back to object mode WITHOUT looplifting enabled because Function "func" failed type inference due to: No implementation of function Function(<built-in method normal of numpy.random.mtrand.RandomState object at 0x7f7c4ed6cc40>) found for signature:
>>> normal(size=UniTuple(int64 x 2))
There are 4 candidate implementations:
- Of which 4 did not match due to:
Overload in function '_OverloadWrapper._build.<locals>.ol_generated': File: numba/core/overload_glue.py: Line 131.
With argument(s): '(size=UniTuple(int64 x 2))':
Rejected as the implementation raised a specific error:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<intrinsic stub>) found for signature:
>>> stub(UniTuple(int64 x 2))
There are 2 candidate implementations:
- Of which 2 did not match due to:
Intrinsic in function 'stub': File: numba/core/overload_glue.py: Line 35.
With argument(s): '(UniTuple(int64 x 2))':
Rejected as the implementation raised a specific error:
TypingError: unsupported call signature
raised from /opt/anaconda3/lib/python3.9/site-packages/numba/core/typing/templates.py:439
During: resolving callee type: Function(<intrinsic stub>)
During: typing of call at <string> (3)
File "<string>", line 3:
<source missing, REPL/exec in use?>
raised from /opt/anaconda3/lib/python3.9/site-packages/numba/core/typeinfer.py:1086
During: resolving callee type: Function(<built-in method normal of numpy.random.mtrand.RandomState object at 0x7f7c4ed6cc40>)
During: typing of call at /var/folders/9_/y3v35tk14nl7_b9ks8h4l79c0000gn/T/ipykernel_4300/1986471478.py (4)
File "../../../../../../../../var/folders/9_/y3v35tk14nl7_b9ks8h4l79c0000gn/T/ipykernel_4300/1986471478.py", line 4:
<source missing, REPL/exec in use?>
@guvectorize([(float64[:], int64)], '(n),()->(n)')
/opt/anaconda3/lib/python3.9/site-packages/numba/core/object_mode_passes.py:151: NumbaWarning: Function "func" was compiled in object mode without forceobj=True.
File "../../../../../../../../var/folders/9_/y3v35tk14nl7_b9ks8h4l79c0000gn/T/ipykernel_4300/1986471478.py", line 1:
<source missing, REPL/exec in use?>
warnings.warn(errors.NumbaWarning(warn_msg,
/opt/anaconda3/lib/python3.9/site-packages/numba/core/object_mode_passes.py:161: NumbaDeprecationWarning:
Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour.
For more information visit https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit
File "../../../../../../../../var/folders/9_/y3v35tk14nl7_b9ks8h4l79c0000gn/T/ipykernel_4300/1986471478.py", line 1:
<source missing, REPL/exec in use?>
warnings.warn(errors.NumbaDeprecationWarning(msg,
但是当我按照下面的内容编写代码时,它可以与警告一起使用。但是重复输入,因此没有使用main_2。
@guvectorize([(float64[:], int64, float64[:])], '(n),()->(n)')
def func(main, n, main_2):
nprime = n-1
off = np.random.normal(size=(nprime, nprime))
tril = np.tril(off, -1)
W_n = tril + tril.T
np.fill_diagonal(W_n, main)
eigenvalues = LA.eigvals(W_n)
w = np.flip(np.sort(eigenvalues))
# GOE_L12_dist[:,i] = w[0:2]
return w
main = np.sqrt(2) * np.random.normal(size=(3-1))
func(main, 3, main)
I try to apply Numba to my code to improve the speed. But I get an error " IndexError: list index out of range" and warnings. How can I make it works?
I run the code as below:
import numpy as np
from numpy import linalg as LA
from numba import guvectorize, int64, float64, vectorize
@guvectorize([(float64[:], int64)], '(n),()->(n)')
def func(main, n):
nprime = n-1
off = np.random.normal(size=(nprime, nprime))
tril = np.tril(off, -1)
W_n = tril + tril.T
np.fill_diagonal(W_n, main)
eigenvalues = LA.eigvals(W_n)
w = np.flip(np.sort(eigenvalues))
# GOE_L12_dist[:,i] = w[0:2]
return w
main = np.sqrt(2) * np.random.normal(size=(3-1))
func(main, 3)
Then I get error as
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Input In [16], in <cell line: 1>()
1 @guvectorize([(float64[:], int64)], '(n),()->(n)')
----> 2 def func(main, n):
3 nprime = n-1
4 off = np.random.normal(size=(nprime, nprime))
File /opt/anaconda3/lib/python3.9/site-packages/numba/np/ufunc/decorators.py:197, in guvectorize.<locals>.wrap(func)
195 if len(ftylist) > 0:
196 guvec.disable_compile()
--> 197 return guvec.build_ufunc()
File /opt/anaconda3/lib/python3.9/site-packages/numba/np/ufunc/gufunc.py:66, in GUFunc.build_ufunc(self)
65 def build_ufunc(self):
---> 66 self.ufunc = self.gufunc_builder.build_ufunc()
67 return self
File /opt/anaconda3/lib/python3.9/site-packages/numba/core/compiler_lock.py:35, in _CompilerLock.__call__.<locals>._acquire_compile_lock(*args, **kwargs)
32 @functools.wraps(func)
33 def _acquire_compile_lock(*args, **kwargs):
34 with self:
---> 35 return func(*args, **kwargs)
File /opt/anaconda3/lib/python3.9/site-packages/numba/np/ufunc/ufuncbuilder.py:363, in GUFuncBuilder.build_ufunc(self)
360 nout = len(self.sout)
362 # Pass envs to fromfuncsig to bind to the lifetime of the ufunc object
--> 363 ufunc = _internal.fromfunc(
364 self.py_func.__name__, self.py_func.__doc__,
365 func_list, type_list, nin, nout, datalist,
366 keepalive, self.identity, self.signature,
367 )
368 return ufunc
IndexError: list index out of range
And the warning shows
/var/folders/9_/y3v35tk14nl7_b9ks8h4l79c0000gn/T/ipykernel_4300/1986471478.py:1: NumbaWarning:
Compilation is falling back to object mode WITHOUT looplifting enabled because Function "func" failed type inference due to: No implementation of function Function(<built-in method normal of numpy.random.mtrand.RandomState object at 0x7f7c4ed6cc40>) found for signature:
>>> normal(size=UniTuple(int64 x 2))
There are 4 candidate implementations:
- Of which 4 did not match due to:
Overload in function '_OverloadWrapper._build.<locals>.ol_generated': File: numba/core/overload_glue.py: Line 131.
With argument(s): '(size=UniTuple(int64 x 2))':
Rejected as the implementation raised a specific error:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<intrinsic stub>) found for signature:
>>> stub(UniTuple(int64 x 2))
There are 2 candidate implementations:
- Of which 2 did not match due to:
Intrinsic in function 'stub': File: numba/core/overload_glue.py: Line 35.
With argument(s): '(UniTuple(int64 x 2))':
Rejected as the implementation raised a specific error:
TypingError: unsupported call signature
raised from /opt/anaconda3/lib/python3.9/site-packages/numba/core/typing/templates.py:439
During: resolving callee type: Function(<intrinsic stub>)
During: typing of call at <string> (3)
File "<string>", line 3:
<source missing, REPL/exec in use?>
raised from /opt/anaconda3/lib/python3.9/site-packages/numba/core/typeinfer.py:1086
During: resolving callee type: Function(<built-in method normal of numpy.random.mtrand.RandomState object at 0x7f7c4ed6cc40>)
During: typing of call at /var/folders/9_/y3v35tk14nl7_b9ks8h4l79c0000gn/T/ipykernel_4300/1986471478.py (4)
File "../../../../../../../../var/folders/9_/y3v35tk14nl7_b9ks8h4l79c0000gn/T/ipykernel_4300/1986471478.py", line 4:
<source missing, REPL/exec in use?>
@guvectorize([(float64[:], int64)], '(n),()->(n)')
/opt/anaconda3/lib/python3.9/site-packages/numba/core/object_mode_passes.py:151: NumbaWarning: Function "func" was compiled in object mode without forceobj=True.
File "../../../../../../../../var/folders/9_/y3v35tk14nl7_b9ks8h4l79c0000gn/T/ipykernel_4300/1986471478.py", line 1:
<source missing, REPL/exec in use?>
warnings.warn(errors.NumbaWarning(warn_msg,
/opt/anaconda3/lib/python3.9/site-packages/numba/core/object_mode_passes.py:161: NumbaDeprecationWarning:
Fall-back from the nopython compilation path to the object mode compilation path has been detected, this is deprecated behaviour.
For more information visit https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit
File "../../../../../../../../var/folders/9_/y3v35tk14nl7_b9ks8h4l79c0000gn/T/ipykernel_4300/1986471478.py", line 1:
<source missing, REPL/exec in use?>
warnings.warn(errors.NumbaDeprecationWarning(msg,
But when I write my code as below, it works with warning. But the input is repeated so there is no use of main_2.
@guvectorize([(float64[:], int64, float64[:])], '(n),()->(n)')
def func(main, n, main_2):
nprime = n-1
off = np.random.normal(size=(nprime, nprime))
tril = np.tril(off, -1)
W_n = tril + tril.T
np.fill_diagonal(W_n, main)
eigenvalues = LA.eigvals(W_n)
w = np.flip(np.sort(eigenvalues))
# GOE_L12_dist[:,i] = w[0:2]
return w
main = np.sqrt(2) * np.random.normal(size=(3-1))
func(main, 3, main)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@guvectorize
期望在功能定义中否返回
。来自@guvectorize 文档
最后一个参数不必包含在函数调用中。
np.random.normal
如果提供了所有参数,则不会引起警告。输出
如果函数仅取决于 n (请注意,此实施模仿了期望 n 的决定),并且无需推广到更多不需要两个维度,
@guvectorize
不需要。输出
@guvectorize
expects noreturn
in the function definition.From the @guvectorize documentation
The last argument does not have to be included in the function call.
np.random.normal
doesn't cause a warning if all arguments are provided.Output
If the function only depends on n (please note that this implementation mimics the decision to expect n to be off-by-one) and does not need to generalize to more than two dimensions,
@guvectorize
is not needed.Output