tensorflow学习之tf.truncated_normal和tf.random_noraml的区别
tf版本1.13.1,CPU
最近在tf里新学了一个函数,一查发现和tf.random_normal差不多,于是记录一下。。
1、首先是tf.truncated_normal函数
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
shape是张量维度,mean是正态分布是均值,stddev是正态分布的标准差;
它是从截断的正态分布中输出随机值,虽然同样是输出正态分布,但是它生成的值是在距离均值两个标准差范围之内的,也就是说,在tf.truncated_normal中如果x的取值在区间(μ-2σ,μ+2σ)之外则重新进行选择。这样保证了生成的值都在均值附近。
2、接下去是
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
参数设置和上一个函数是一样的;而tf.random_normal是没有要求一定在(μ-2σ,μ+2σ)之内的
栗子如下:
r1 = tf.random_normal(shape=[10,10], mean=0, stddev=1) r2 = tf.truncated_normal(shape=[10,10], mean=0, stddev=1) with tf.Session() as sess: print(sess.run(r1)) print('-----------------------------') print(sess.run(r2)) ### [[-0.9133466 -0.2193664 0.9871885 0.98474556 0.6795205 0.5485084 -0.5377412 -0.74770564 -1.1065788 -0.6537805 ] [-0.05684219 -0.40211347 0.29256898 0.05646685 -0.09572198 -0.5329077 0.09256991 0.5098056 -0.56273437 3.0135465 ] [-0.76685655 0.80393404 1.4993162 -0.97593653 -0.02311829 0.18108685 -0.10103939 -0.1788411 0.70498335 0.7432045 ] [ 1.0753849 -1.552068 -0.82677794 -1.0526472 0.35524416 1.109456 -0.46477854 -0.94029546 0.4074978 -0.6275004 ] [ 1.1464692 -0.7329785 1.3993146 -1.3458818 -1.0205774 -1.3928136 0.31929192 -0.19847174 0.24104206 -0.30365643] [-0.56316936 0.35070175 -1.7900109 0.18886246 0.70969003 0.61488485 1.0273159 0.08935942 0.31060082 0.46601346] [-2.2100964 -0.5587107 0.23800382 0.58263725 0.44721082 0.17937267 -1.6707375 -0.69823587 -0.624195 0.7153361 ] [-0.8212442 -0.4100253 -0.29756552 1.6561007 0.22083926 0.97440094 -0.08767799 0.07237837 1.5110539 1.7204924 ] [ 0.7634979 -0.38169494 -0.07168189 -1.0445783 -0.4177571 -0.06731904 0.13163103 0.73196214 -0.32269892 -1.9275837 ] [ 0.73937386 -0.08055622 1.209047 -0.41532582 0.11017569 0.7899525 -1.8012363 -0.84279794 1.6240916 0.73638594]] ----------------------------- [[-0.14283383 1.0008699 1.5809501 0.82382834 -0.6668856 0.59398574 -1.0554414 0.87018394 0.2878338 -0.4894875 ] [ 1.3494765 -0.13940284 0.38545245 -0.16343059 0.37798592 -1.119075 -0.9302422 -1.1171802 -0.28318515 1.5838846 ] [ 0.01883612 1.0887331 -0.18458353 -0.48704016 -0.84166986 1.2319418 0.6718625 -0.7222486 0.88431233 -0.31767374] [-0.3927616 0.42229542 0.29394206 0.636135 -0.9557136 -0.14583842 0.09705613 -1.5379425 0.6139084 0.36891633] [-1.0034062 -0.7085579 0.05415478 -0.26299214 -1.4239995 0.24866848 -0.08754523 0.9556532 -0.70344573 0.5501471 ] [-0.36907005 -0.18001153 -1.7696055 -0.8719723 0.00751158 1.4784805 1.2351555 0.5596131 0.9836762 1.3182775 ] [-0.68398184 -0.74510956 1.5121812 -0.5380244 -0.2752701 0.03485487 1.495663 -1.2325596 0.94225466 -1.386867 ] [-0.00377628 -0.13128701 0.02556802 1.1236848 1.139232 -0.53410244 0.87148935 1.9706047 1.2066965 0.9876827 ] [-0.3875238 -0.06041284 0.44305998 1.193 0.6871842 0.2273079 1.6827972 1.1394504 -1.4383765 -0.21280776] [ 1.5706499 1.6732877 -1.1468259 0.3528784 1.8091112 1.2832314 -0.32502323 0.34072635 1.5236534 0.89802533]]
可见r1里面有一个数是在(-2,2)范围以外的,而r2则全在(-2,2)范围内
人生苦短,何不用python