gpt4 book ai didi

python-3.x - numpy 实现自定义 RNG

转载 作者:行者123 更新时间:2023-12-04 03:53:10 34 4
gpt4 key购买 nike

出于一致性原因,我试图让 numpy 使用我自己的 RNG 实现。我的理解,基于我能找到的小文档,来自 numpy 文档 herehere是我需要提供一个自定义的 BitGenerator 类来实现 random_raw方法,然后使用 np.random.Generator 初始化,所以我试过这个:

import numpy as np

class TestBitGenerator(np.random.BitGenerator):
def __init__(self):
super().__init__(0)
self.counter = 0
def random_raw(self, size=None):
self.counter += 1
if size is None:
return self.counter
return np.full(n, self.counter)

mc_adapter = TestBitGenerator()
npgen = np.random.Generator(mc_adapter)

print(npgen.random())
这会导致段错误:
$ python bitgen.py 
Segmentation fault (core dumped)
我想我在这里遗漏了一些东西(来自 TestBitGenerator?),有人能指出我正确的方向吗?
我尝试不继承 np.random.BitGenerator,并得到一个不同的错误: object has no attribute 'capsule'我使用 numpy 1.19.2 和 python 3.8.2

最佳答案

实际上,如果您使用库 RandomGen 包装它,则可以在纯 python 中完成它。 (这是当前 np.random.Generator 的孵化器)。所以有一个 UserBitGenerator只允许你使用 python:https://bashtage.github.io/randomgen/bit_generators/userbitgenerator.html
遗憾的是,这在 numpy 中没有成功(或者如果是我还没有找到它......)。

关于python-3.x - numpy 实现自定义 RNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64139525/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com