繁体   English   中英

在 numba 中使用 numpy 随机选择

[英]Using numpy random choice in numba

我正在尝试使用 numba 重写我的一些代码(版本 0.52,在 windows 10、64 位上),但是使用 numpy 随机选择时出现我不理解的错误。 如果我不使用概率选项,Numpy 随机选择应该与 numba 一起使用。 这是代码:

import numpy as np
from numba import jit

@jit(nopython=True)

def Calc():
    a = np.array([1, -1])
    size = [3, 3, 3]
    values = np.random.choice(a, size=size)

Calc()

我收到以下错误:

Traceback (most recent call last):
  File "\\uni.au.dk\Users\au684834\Documents\Python\Aarhus\Simulations\Ising\Tests\Numbaerrors.py", line 10, in <module>
    Calc()
  File "C:\Users\au684834\Miniconda3\lib\site-packages\numba\core\dispatcher.py", line 414, in _compile_for_args
    error_rewrite(e, 'typing')
  File "C:\Users\au684834\Miniconda3\lib\site-packages\numba\core\dispatcher.py", line 357, in error_rewrite
    raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
[1m[1m[1mNo implementation of function Function(<built-in method choice of numpy.random.mtrand.RandomState object at 0x000002B05203B340>) found for signature:
 
 >>> choice(array(int64, 1d, C), size=list(int64)<iv=[3, 3, 3]>)
 
There are 2 candidate implementations:
[1m  - Of which 1 did not match due to:
  Overload in function 'choice': File: numba\cpython\randomimpl.py: Line 1346.
    With argument(s): '(array(int64, 1d, C), size=list(int64)<iv=None>)':[0m
[1m   Rejected as the implementation raised a specific error:
     TypingError: Failed in nopython mode pipeline (step: nopython frontend)
   [1m[1m[1mNo implementation of function Function(<built-in function empty>) found for signature:
    
    >>> empty(list(int64)<iv=None>, class(int64))
    
   There are 2 candidate implementations:
   [1m      - Of which 2 did not match due to:
         Overload of function 'empty': File: numba\core\typing\npydecl.py: Line 507.
           With argument(s): '(list(int64)<iv=None>, class(int64))':[0m
   [1m       No match.[0m
   [0m
   [0m[1mDuring: resolving callee type: Function(<built-in function empty>)[0m
   [0m[1mDuring: typing of call at C:\Users\au684834\Miniconda3\lib\site-packages\numba\cpython\randomimpl.py (1403)
   [0m
   [1m
   File "..\Users\au684834\Miniconda3\lib\site-packages\numba\cpython\randomimpl.py", line 1403:[0m
   [1m        def choice_impl(a, size=None, replace=True):
               <source elided>
               if replace:
   [1m                out = np.empty(size, dtype)
   [0m                [1m^[0m[0m
[0m
  raised from C:\Users\au684834\Miniconda3\lib\site-packages\numba\core\typeinfer.py:1071
[1m  - Of which 1 did not match due to:
  Overload in function 'choice': File: numba\cpython\randomimpl.py: Line 1346.
    With argument(s): '(array(int64, 1d, C), size=list(int64)<iv=[3, 3, 3]>)':[0m
[1m   Rejected as the implementation raised a specific error:
     TypingError: Failed in nopython mode pipeline (step: nopython frontend)
   [1m[1m[1mNo implementation of function Function(<built-in function empty>) found for signature:
    
    >>> empty(list(int64)<iv=[3, 3, 3]>, class(int64))
    
   There are 2 candidate implementations:
   [1m      - Of which 2 did not match due to:
         Overload of function 'empty': File: numba\core\typing\npydecl.py: Line 507.
           With argument(s): '(list(int64)<iv=None>, class(int64))':[0m
   [1m       No match.[0m
   [0m
   [0m[1mDuring: resolving callee type: Function(<built-in function empty>)[0m
   [0m[1mDuring: typing of call at C:\Users\au684834\Miniconda3\lib\site-packages\numba\cpython\randomimpl.py (1403)
   [0m
   [1m
   File "..\Users\au684834\Miniconda3\lib\site-packages\numba\cpython\randomimpl.py", line 1403:[0m
   [1m        def choice_impl(a, size=None, replace=True):
               <source elided>
               if replace:
   [1m                out = np.empty(size, dtype)
   [0m                [1m^[0m[0m
[0m
  raised from C:\Users\au684834\Miniconda3\lib\site-packages\numba\core\typeinfer.py:1071
[0m
[0m[1mDuring: resolving callee type: Function(<built-in method choice of numpy.random.mtrand.RandomState object at 0x000002B05203B340>)[0m
[0m[1mDuring: typing of call at \\uni.au.dk\Users\au684834\Documents\Python\Aarhus\Simulations\Ising\Tests\Numbaerrors.py (8)
[0m
[1m
File "\\uni.au.dk\Users\au684834\Documents\Python\Aarhus\Simulations\Ising\Tests\Numbaerrors.py", line 8:[0m
[1mdef Calc():
    <source elided>
    size = [3, 3, 3]
[1m    values = np.random.choice(a, size=size)
[0m    [1m^[0m[0m

不知道我做错了什么。

使用tuple作为size ,而不是list

@njit
def calc():
    a = np.array([1, -1])
    size = (3, 3, 3)
    values = np.random.choice(a, size=size)
    return values

现在:

>>> calc()  # doctest: +SKIP
array([[[-1,  1,  1],
        [-1, -1,  1],
        [-1, -1, -1]],

       [[-1, -1,  1],
        [-1, -1, -1],
        [-1, -1, -1]],

       [[ 1,  1,  1],
        [ 1,  1,  1],
        [ 1, -1,  1]]])

但请注意,并非所有numpy函数都受numba支持。 其中一个例子是np.clip()

暂无
暂无

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

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