TensorFlow权重随机初始化详解:正态分布与截断正态分布
本文深入解析TensorFlow中权重的随机初始化机制,重点介绍random_normal与truncated_normal两种核心方法。通过解析源码,阐明mean、stddev及seed参数的具体作用,并对比截断正态分布与普通正态分布在处理异常值时的差异,帮助开发者理解模型训练初始化的底层逻辑。
在初次接触TensorFlow时,对于`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`则用于设定随机种子,这与伪随机数生成器的算法机制相关。例如,在mnist卷积网络示例中,`seed`被赋值为66478,这种特定取值背后的具体原理尚不明确,但体现了初始化过程中的可复现性设计。
此外,TensorFlow还定义了截断正态分布函数`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框架还提供了均匀分布等其他初始化选项供选择。
以上内容涵盖了TensorFlow中权重初始化的主要方法,旨在为相关学习提供辅助,欢迎继续支持本站获取更多技术资源。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









