将np.random.generator对象传递给NJIT装修函数时,Python numba错误

发布于 2025-01-29 12:28:27 字数 769 浏览 3 评论 0 原文

我有一个看起来如下的模块:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文