tf.truncated_normal与tf.random_normal的区别

作为tensorflow里的正态分布产生函数,这两个函数的输入参数几乎完全一致,

而其主要的区别在于,tf.truncated_normal的输出如字面意思是截断的,而截断的标准是2倍的stddev。

举例,当输入参数mean = 0 , stddev =1时,

使用tf.truncated_normal的输出是不可能出现[-2,2]以外的点的,

而如果shape够大的话,tf.random_normal却会产生2.2或者2.4之类的输出。

下面的代码从tensorflow官网的代码做修改,然后结果看图。

#-*- coding: utf-8 -*-
import tensorflow as tf    
import numpy as np  
 
 
k = tf.placeholder(tf.float32)
 
# Make a normal distribution, with a shifting mean
mean_random_normal = tf.random_normal(shape=[1000],mean=(5*k),stddev=1)
mean_moving_normal = tf.truncated_normal(shape=[1000], mean=(5*k), stddev=1)
# Record that distribution into a histogram summary
tf.summary.histogram("normal/random_output", mean_random_normal)
tf.summary.histogram("normal/truncated_output", mean_moving_normal)
 
# Setup a session and summary writer
sess = tf.Session()
writer = tf.summary.FileWriter("/tmp/histogram_example2")
 
summaries = tf.summary.merge_all()
 
# Setup a loop and write the summaries to disk
N = 400
for step in range(N):
  k_val = step/float(N)
  summ = sess.run(summaries, feed_dict={k: k_val})
  writer.add_summary(summ, global_step=step)

在shell中输入命令      tensorboard  --logdir=/tmp/histogram_example2
然后在browser中输入127.0.0.1:6006得到下图:


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值