首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python中随机向量的再生

Python中随机向量的再生
EN

Stack Overflow用户
提问于 2016-04-10 22:03:36
回答 1查看 223关注 0票数 4

我正在尝试生成和再生随机法线的向量。

我想通过生成大小为100x3的随机法线矩阵和平均值为0和sd 1的随机法线来实现以下目标:

代码语言:javascript
运行
复制
seed1 = '123'
seed2 = 'asd'
randMatrixrows = 100
randMatrixcols = 3
mu = 0
sd = 1

normRand1 = rekina_normRandomGenerator( seed1, randMatrixrows, randMatrixcols, mu, sd ) #normRand1 is of size 100x3
normRand2 = rekina_normRandomGenerator( seed2, randMatrixrows, randMatrixcols, mu, sd )
normRand3 = rekina_normRandomGenerator( seed1, randMatrixrows, randMatrixcols, mu, sd )
normRand4 = rekina_normRandomGenerator( seed2, randMatrixrows, randMatrixcols, mu, sd )

err1 = normRand1 - normRand3
err2 = normRand2 - normRand4

err1和err2的每个元素应该是0。

我也尝试过阅读,但是作为Python新手,我对这个实现完全失去了兴趣。我希望有一个简单的实现来演示如何使用CBRNG。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-19 09:01:09

您最初认为必须使用reikna.cbrng.CBRNG的假设是正确的。它似乎提供了大量基于反式RNG的伪随机源分布,这可能会让人感到困惑。它还提供了创建具有给定分布的随机数生成器的快捷键

代码语言:javascript
运行
复制
from reikna.core import Type
from reikna.cbrng import CBRNG
from reikna.cluda import any_api
import numpy as np

# Get any supported API for this system, or an exception    
_api = any_api()
# The global CLUDA Thread, wrapping context, shared by all
# reikna_norm_rng's
_thr = _api.Thread.create()


def reikna_norm_rng(seed, rows, cols, mean, std,
                    dtype=np.float32,
                    generators_dim=1):
    """
    A do-all generator function for creating a new Computation
    returning a stream of pseudorandom number arrays.
    """
    # The Type of the output array
    randoms_arr = Type(dtype, (rows, cols))
    # Shortcut for creating a Sampler for normally distributed
    # random numbers
    rng = CBRNG.normal_bm(randoms_arr=randoms_arr,
                          generators_dim=generators_dim,
                          sampler_kwds=dict(mean=mean, std=std),
                          # Reikna expects a positive integer. This is a 
                          # quick and *dirty* solution.
                          seed=abs(hash(seed)))
    compiled_comp = rng.compile(_thr)
    # RNG "state"
    counters = _thr.to_device(rng.create_counters())
    # Output array
    randoms = _thr.empty_like(compiled_comp.parameter.randoms)

    while True:
        compiled_comp(counters, randoms)
        yield randoms.get()

若要在行动中看到它,请添加:

代码语言:javascript
运行
复制
if __name__ == '__main__':
    seed1 = '123'
    seed2 = 'asd'
    rows, cols = 100, 3
    mu, sd = 0, 1

    # This will create a new RNG, take 1 result and throw the RNG away
    r1 = next(reikna_norm_rng(seed1, rows, cols, mu, sd))
    r2 = next(reikna_norm_rng(seed2, rows, cols, mu, sd))
    r3 = next(reikna_norm_rng(seed1, rows, cols, mu, sd))
    r4 = next(reikna_norm_rng(seed2, rows, cols, mu, sd))

    err1 = r1 - r3
    err2 = r2 - r4

    print("all(err1 == 0):", np.all(err1 == 0))
    print("all(err2 == 0):", np.all(err2 == 0))

我还想确保用这两种不同的种子生成的随机画没有相关性。

这取决于执行的质量。以下是2组种子和1组数字的绘制方法:

代码语言:javascript
运行
复制
rng1 = reikna_norm_rng(0, 100, 10000, 0, 1)
rng2 = reikna_norm_rng(1, 100, 10000, 0, 1)
A = next(rng1)
B = next(rng2)
A_r = A.ravel()
B_r = B.ravel()
for i in range(0, A_r.size, 1000):
    plot(A_r[i:i+1000], B_r[i:i+1000], 'b.')

免责声明

这是我第一次和雷克纳一起跑。上面的代码可能不会像筛子一样及时释放资源和/或泄漏。它使用的是全局Thread,在更大的应用程序中这可能不是您想要的。

PS

代码语言:javascript
运行
复制
np.random.seed(seed)
np.random.normal(0, 1, (100, 3))

还生成形状为(100,3)的正态分布随机数数组,尽管它不使用GPU。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36536062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档