Reference: [Deep Learning from scratch (p98 4.3.1)](https://www.amazon.co.jp/%E3%82%BC%E3%83%AD%E3%81%8B%E3%82 % 89% E4% BD% 9C% E3% 82% 8BDeep-Learning-% E2% 80% 95Python% E3% 81% A7% E5% AD% A6% E3% 81% B6% E3% 83% 87% E3% 82% A3% E3% 83% BC% E3% 83% 97% E3% 83% A9% E3% 83% BC% E3% 83% 8B% E3% 83% B3% E3% 82% B0% E3% 81% AE% E7% 90% 86% E8% AB% 96% E3% 81% A8% E5% AE% 9F% E8% A3% 85-% E6% 96% 8E% E8% 97% A4-% E5% BA% B7% E6% AF% 85 / dp / 4873117585)
rand simply outputs a random number within the range of 0 to + 1 (called a uniform distribution). randn outputs a random numerical value with a standard normal distribution (Gaussian distribution). The standard normal distribution (Gaussian distribution) is a normal distribution with a mean of 0 and a standard deviation of 1. That is, it outputs a random numerical value in the range where the average becomes 0. A value close to 0 is likely to be output, and the value is biased between -1 and +1.
6 You can't really tell the difference if you generate a number value. (If it is randn, -1.8 is also output.)
tmp.py
import numpy as np
print("-----rand-----")
#print(np.random.rand(2,3))
print(np.random.rand(2,3).flatten().sum()/6)
print("-----randn-----")
#print(np.random.randn(2,3))
print(np.random.randn(2,3).flatten().sum()/6)
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
[[ 0.93180533 0.57683396 0.61127319]
[ 0.85445375 0.02708778 0.59547601]]
0.548895695538
-----randn-----
[[-0.26519086 -0.30503921 1.38052841]
[ 0.78893855 1.17725959 1.80882169]]
0.31519809212
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
[[ 0.60780629 0.20632273 0.29124602]
[ 0.30784594 0.70772679 0.01821361]]
0.59935565743
-----randn-----
[[ 0.71569157 -0.52234049 -1.59936154]
[-0.82040641 -1.40208476 0.89450808]]
0.669739423807
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
[[ 0.490528 0.77907488 0.7239194 ]
[ 0.85538536 0.92002825 0.67559426]]
0.495670703747
-----randn-----
[[-0.15695285 -0.34244922 -1.44827391]
[-1.36062357 -0.49387177 -1.81863075]]
-0.431866047467
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
[[ 0.84705902 0.58580408 0.31309435]
[ 0.59569691 0.24460075 0.45989438]]
0.392091814221
-----randn-----
[[-0.35680625 -2.18199977 0.54243995]
[-0.29317688 -0.43111914 -1.47586303]]
-0.0274650256491
However, when 1000 number values are generated, the difference becomes noticeable. randn converges to 0. The reason why the average of rand converges to 0.5 is that it is generated on average between 0 and +1. It does not tend to have a normal distribution with an average of 0.5.
tmp.py
import numpy as np
print("-----rand-----")
print(np.random.rand(1000).flatten().sum()/1000)
print("-----randn-----")
print(np.random.randn(1000).flatten().sum()/1000)
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
0.496111761085
-----randn-----
0.0156004004458
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
0.499698237009
-----randn-----
-0.00664557647709
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
0.494677814946
-----randn-----
0.0301994725782
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
0.515244265326
-----randn-----
-0.00605375728391
mbp-2:deep-learning-from-scratch-master shiraki$ python tmp.py
-----rand-----
0.500510191675
-----randn-----
0.00764750739818
Recommended Posts