我有一个看起来如下的模块:
import numba as nb
import numpy as np
@nb.njit
def random_numba_function(rng):
print(rng.choice([0,1,2]))
seed = 1
rng = np.random.default_rng(seed)
random_numba_function(rng)
这将返回装饰函数的错误消息 random_numba_function
:
argument 0: Cannot determine Numba type of <class 'numpy.random._generator.Generator'>
documentation 函数未列出为受支持的函数,这可能意味着您不能以这种方式传递随机数生成器。
在我的特殊情况下,我还具有装饰功能的有条件,该功能决定是否从随机数生成器中“绘制”一个数字。这意味着我不仅可以提前计算随机选择的索引并将其作为整数传递到功能,因为我只知道一旦我掌握了功能,我才知道我是否会使用它。是否有一种有效的方法可以替代Numba生成随机种子数的方式?
I have a module that looks as following:
import numba as nb
import numpy as np
@nb.njit
def random_numba_function(rng):
print(rng.choice([0,1,2]))
seed = 1
rng = np.random.default_rng(seed)
random_numba_function(rng)
This returns the error message for the decorated function random_numba_function
:
argument 0: Cannot determine Numba type of <class 'numpy.random._generator.Generator'>
The documentation states that the numpy.random.seed
function is supported, but the np.random.default_rng
function is not listed as a supported function which probably means that you cannot pass a random number generator this way.
In my particular situation, I also have a conditional inside of the decorated function which decides whether or not to "draw" a number from the random number generator. This means that I can't just calculate the index of the random choice ahead of time and pass it to the function as an integer, since I only know if I am going to be using it once I am inside of the function. Is there an efficient way to substitute the way random seed numbers are generated with numba?
发布评论