TensorFlow权重随机初始化详解:正态分布与截断正态分布

橘子海
橘子海
发布时间:2026-10-13| 2人浏览过
原创

本文深入解析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中权重初始化的主要方法,旨在为相关学习提供辅助,欢迎继续支持本站获取更多技术资源。

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。