truncated_normal()的中文名是截断正态分布。
以标准正态分布为例,首先要搞明白的概念是:
1.曲线下的面积为1,也就是f(x)在负无穷到正无穷上对x积分结果为1,所有事件的概率和当然为1
2. 3q原则:
正态曲线下,横轴区间(μ-σ,μ+σ)内的面积为68.268949%。
横轴区间(μ-1.96σ,μ+1.96σ)内的面积为95.449974%。
横轴区间(μ-2.58σ,μ+2.58σ)内的面积为99.730020%。
3,σ对曲线的影响:
σ越大,代表数据与期望的差越大,也就是数据越分散,分散会导致曲线扁平
σ越小,代表数据与期望的差越小,数据集中在期望附近,曲线越瘦高
truncated_normal取区间(μ-1.96σ,μ+1.96σ)区间选择,如果值落在(μ-1.96σ,μ+1.96σ),则重新取值
我们用代码验证标准差分别为10和0.1,期望为0时,生成的数据的区别
标准差为10,期望为0:
import tensorflow as tf
in_w = tf.Variable(tf.truncated_normal(shape=[100],stddev=10))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(in_w))
print((sess.run(tf.reduce_mean(in_w))))
输出:
[ 15.264693 5.605786 -10.607137 -8.992271 6.2410884
-6.3943005 12.87504 1.6434954 -0.08021436 14.6423
-5.7843676 -2.6846519 -16.232351 -4.7292757 5.353074
-0.8682578 -4.889186 2.85577 -1.4690969 10.427742
3.885746 3.3024988 -9.544989 -6.3758845 3.638872
-15.31946 -5.894518 -14.554941 -3.3702407 -2.2352414
1.3337572 -3.84598 18.100552 7.053831 -2.56785
4.9129243 12.2830105 9.024238 3.4606113 -15.407189
8.67856 6.089006 -12.975464 -12.327808 5.2479563
13.670704 -14.960556 0.16157159 -11.865211 3.4655285
-8.238024 -3.761511 8.591464 14.442082 6.536665
8.992389 -2.9644334 -9.006163 -5.994816 11.333315
-2.8606877 12.958926 4.3612 -5.415127 16.07208
-3.5357084 6.2845635 -4.348878 -6.12752 -12.226945
-11.842737 5.6698017 -0.41170892 0.34950134 -0.33401674
1.9953703 1.6670101 -4.9404354 -1.1057891 -0.649768
-16.54144 -2.1462455 -13.811441 5.3038363 1.7725132
-1.5898563 -6.2327332 10.687341 14.243831 -2.2322078
8.340572 3.8591108 4.8313303 6.897096 -9.8767185
-1.4173822 -10.929442 -7.253949 -4.426125 5.379071 ]
-0.14410812
标准差为0.1,期望为0:
import tensorflow as tf
in_w = tf.Variable(tf.truncated_normal(shape=[100],stddev=0.1))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(in_w))
print((sess.run(tf.reduce_mean(in_w))))
输出结果:
[-0.07562008 -0.11738521 0.00126518 0.00654667 0.06146542 0.1274802
-0.07882587 -0.07322701 -0.10550299 -0.13134663 -0.08668221 -0.13011587
-0.00634123 0.11761832 -0.15559179 0.16280039 0.00677737 -0.13258608
-0.15251096 0.1105137 0.05175411 0.06398679 -0.01425287 0.00178337
0.03511551 0.065492 0.04070082 0.07649365 -0.00438356 -0.10822525
-0.08198652 0.12050386 -0.18869711 0.11418968 -0.04707202 -0.02375514
0.02433945 -0.06546775 0.04469329 -0.08831641 -0.00345935 0.09103863
-0.05663515 0.02931254 -0.04379479 0.12777077 -0.08051455 0.13746393
-0.1122907 -0.010817 0.1264553 -0.0804055 -0.09331721 -0.08387195
0.07598593 -0.00964227 -0.0126845 -0.0680997 -0.04646983 -0.06502448
0.02778335 -0.08299793 -0.04246726 -0.08744942 0.08259027 0.0512216
0.125033 -0.03794092 -0.07904857 -0.01495275 0.03500177 -0.06184981
-0.01372355 0.15633321 -0.10084256 -0.0023646 -0.12707259 0.03698065
-0.05184708 0.12137643 0.02537341 -0.10431447 -0.03203743 0.04947854
0.11925892 -0.07037317 0.03640224 -0.09015492 0.04775707 -0.0625208
-0.04265056 -0.13869947 -0.03584487 0.15474908 0.07224867 0.02143565
-0.12387611 -0.12429041 0.06149822 -0.03040524]
-0.011465751
对比可看出,同样的期望,当标准差更小时,数据更集中在期望附近