就地在float32/16中生成普通随机数 [英] Generate normal random numbers in float32/16 in-place
问题描述
在Numpy/Scipy中,如何从具有指定(浮动)dtype
的正态分布生成随机数?就我而言,我需要float32
和float16
.
In Numpy/Scipy, how can I generate random numbers from a normal distribution with a specified (float) dtype
? In my case I need float32
and float16
.
由于数组很大,因此我不希望在采样后转换.
Since the array is quite large, I don't want to convert the array after the sampling.
例如:
a = np.random.normal(1e7).astype('float16')
可以完成这项工作,但是由于它需要一个临时的float64数组,因此它使用的RAM是直接float16
采样的4倍.
does the job but since it need a temporary float64 array it uses 4x the RAM than a direct float16
sampling.
推荐答案
我不知道numpy或scipy中的随机数生成器会本地生成16或32位浮点数.
I don't know of a random number generator in numpy or scipy that generates 16 or 32 bit floats natively.
为避免产生较大的临时值,可以分批生成值.例如,下面的代码创建一个包含10000000个float16
值样本的数组.
To avoid the large temporary, you could generate the values in batches. For example, the following creates an array of 10000000 samples of float16
values.
In [125]: n = 10000000 # Number of samples to generate
In [126]: k = 10000 # Batch size
In [127]: a = np.empty(n, dtype=np.float16)
In [128]: for i in range(0, n, k):
.....: a[i:i+k] = np.random.normal(loc=0, scale=1, size=k)
.....:
这篇关于就地在float32/16中生成普通随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!