Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中重新生成随机向量_Python_Random - Fatal编程技术网

在Python中重新生成随机向量

在Python中重新生成随机向量,python,random,Python,Random,我正在尝试生成并重新生成随机法线的向量 我希望通过生成大小为100x3的随机法线矩阵和平均值为0和sd为1的随机法线,能够实现以下目标: seed1 = '123' seed2 = 'asd' randMatrixrows = 100 randMatrixcols = 3 mu = 0 sd = 1 normRand1 = rekina_normRandomGenerator( seed1, randMatrixrows, randMatrixcols, mu, sd ) #normRand1

我正在尝试生成并重新生成随机法线的向量

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

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。

您最初的假设是正确的。它似乎提供了大量基于计数器的RNG和,这可能令人困惑。它还提供了创建具有给定分布的随机数生成器的功能

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()
要查看它的运行情况,请添加:

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))
我还想确保使用两个不同种子生成的随机数没有相关性

这取决于实施的质量。下面是如何绘制种子为0和1的两组数字:

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.')

免责声明


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

PS

np.random.seed(seed)
np.random.normal(0, 1, (100, 3))

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

我对reikna不太了解,但每次生成一个随机数时,会不会有所不同?为什么需要err1和err3为0?@Sagar,种子设定控件给定“随机”数,这是完全确定的。拥有可重复的“随机”数字在测试等方面非常有用。谢谢@Ilja。它将通过测试。“上面的代码可能不会及时释放资源和/或像筛子一样泄漏。它使用全局线程,这可能不是您在更大的应用程序中想要的。”这句话的意思是什么。一般来说,我相信python对象在获得gc的时候会释放资源等,但是这个GPU的东西对我来说有点陌生,所以我不完全确定,是否应该手动释放这些缓冲区。可能不会。在更大的应用程序中,您可能会重构
reikna\u norm\u rng
,以获取应用程序提供的线程实例,而不是模块作用域全局。使测试更容易,而且总体上更模块化。@Zanam关于
seed=abs(hash(seed))
的一个注意事项:numpy 1.11更改了种子处理,而不是静默地减少给定的种子模
2**32
,它将引发一个异常(刚刚运行到此)。因此,必须将种子设置更改为从给定种子正确生成32位种子,或者自己执行
seed=abs(hash(seed))%2**32
,如果不是那么关键的话。Reikna使用numpy的PRNG为CBRNG创建密钥。