TensorFlow中权重的随机初始化的方法

yipeiwu_com6年前Python基础

一开始没看懂stddev是什么参数,找了一下,在tensorflow/python/ops里有random_ops,其中是这么写的:

def random_normal(shape, mean=0.0, stddev=1.0, dtype=types.float32,
         seed=None, name=None):
 """Outputs random values from a normal distribution.

 Args:
  shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
  mean: A 0-D Tensor or Python value of type `dtype`. The mean of the normal
   distribution.
  stddev: A 0-D Tensor or Python value of type `dtype`. The standard deviation
   of the normal distribution.
  dtype: The type of the output.
  seed: A Python integer. Used to create a random seed for the distribution.
   See
   [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)
   for behavior.
  name: A name for the operation (optional).

 Returns:
  A tensor of the specified shape filled with random normal values.
 """

也就是按照正态分布初始化权重,mean是正态分布的平均值,stddev是正态分布的标准差(standard deviation),seed是作为分布的random seed(随机种子,我百度了一下,跟什么伪随机数发生器还有关,就是产生随机数的),在mnist/concolutional中seed赋值为66478,挺有意思,不知道是什么原理。

后面还有truncated_normal的定义:

def truncated_normal(shape, mean=0.0, stddev=1.0, dtype=types.float32,
           seed=None, name=None):
 """Outputs random values from a truncated normal distribution.

 The generated values follow a normal distribution with specified mean and
 standard deviation, except that values whose magnitude is more than 2 standard
 deviations from the mean are dropped and re-picked.

 Args:
  shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
  mean: A 0-D Tensor or Python value of type `dtype`. The mean of the
   truncated normal distribution.
  stddev: A 0-D Tensor or Python value of type `dtype`. The standard deviation
   of the truncated normal distribution.
  dtype: The type of the output.
  seed: A Python integer. Used to create a random seed for the distribution.
   See
   [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed)
   for behavior.
  name: A name for the operation (optional).

 Returns:
  A tensor of the specified shape filled with random truncated normal values.
 """

截断正态分布,以前都没听说过。

TensorFlow还提供了平均分布等。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python用win32gui遍历窗口并设置窗口位置的方法

最近电脑打开某个软件却看不见窗口,在任务栏上看到软件明明已经运行,猜想一定是什么原因造成软件窗口位置偏离屏幕的有效坐标太远。尝试重启电脑,重装软件,都没有解决,看来是在注册表存储了位置信...

78行Python代码实现现微信撤回消息功能

78行Python代码实现现微信撤回消息功能

Python曾经对我说:"时日不多,赶紧用Python"。于是看到了一个基于python的微信开源库:itchat,玩了一天,做了一个程序,把私聊撤回的信息可以收集起来并发送到个人微信的...

Python项目 基于Scapy实现SYN泛洪攻击的方法

Python项目 基于Scapy实现SYN泛洪攻击的方法

python3版本的Scapy--Scapy3k来实现一个简单的DDos。 首先实现SYN泛洪攻击(SYN Flood,是一直常用的DOS方式之一,通过发送大量伪造的TCP连接请求,使被...

python删除文件示例分享

删除文件复制代码 代码如下:os.remove(   filename )   # filename: "要删除的文件名" 产生异常的可能原因:...

python实现高斯投影正反算方式

使用Python实现了一下我们同事的C++高斯投影正反算,实际跑通,可用。 #!/ usr/bin/python # -*- coding:utf-8 -*- import mat...