批量标准化层的函数接口from_config(Ioffe 等人,2015)。
用法
tf.compat.v1.layers.batch_normalization(
inputs, axis=-1, momentum=0.99, epsilon=0.001, center=True, scale=True,
beta_initializer=tf.compat.v1.zeros_initializer(),
gamma_initializer=tf.compat.v1.ones_initializer(),
moving_mean_initializer=tf.compat.v1.zeros_initializer(),
moving_variance_initializer=tf.compat.v1.ones_initializer(),
beta_regularizer=None, gamma_regularizer=None, beta_constraint=None,
gamma_constraint=None, training=False, trainable=True, name=None, reuse=None,
renorm=False, renorm_clipping=None, renorm_momentum=0.99, fused=None,
virtual_batch_size=None, adjustment=None
)参数
-
inputs张量输入。 -
axis一个int,应该标准化的轴(通常是特征轴)。例如,在带有data_format="channels_first"的Convolution2D层之后,在BatchNormalization中设置axis=1。 -
momentum移动平均线的动量。 -
epsilon小浮点数添加到方差中以避免除以零。 -
center如果为 True,则将beta的偏移量添加到归一化张量。如果为 False,则忽略beta。 -
scale如果为真,乘以gamma。如果为 False,则不使用gamma。当下一层是线性的(例如nn.relu)时,可以禁用此函数,因为缩放可以由下一层完成。 -
beta_initializerBeta 权重的初始化程序。 -
gamma_initializer伽马权重的初始化器。 -
moving_mean_initializer移动均值的初始化器。 -
moving_variance_initializer移动方差的初始化程序。 -
beta_regularizerbeta 权重的可选正则化器。 -
gamma_regularizer伽马权重的可选正则化器。 -
beta_constraint由Optimizer更新后应用于beta权重的可选投影函数(例如,用于实现层权重的范数约束或值约束)。该函数必须将未投影变量作为输入,并且必须返回投影变量(必须具有相同的形状)。在进行异步分布式训练时使用约束是不安全的。 -
gamma_constraint在被Optimizer更新后,将应用于gamma权重的可选投影函数。 -
trainingPython 布尔值或 TensorFlow 布尔标量张量(例如占位符)。是在训练模式(使用当前批次的统计数据归一化)还是在推理模式(使用移动统计数据归一化)返回输出。NOTE:确保正确设置此参数,否则您的训练/推理将无法正常工作。 -
trainable布尔值,如果True还将变量添加到图形集合GraphKeys.TRAINABLE_VARIABLES(请参阅 tf.Variable)。 -
name字符串,图层的名称。 -
reuse布尔值,是否重用前一层同名的权重。 -
renorm是否使用 Batch Renormalization (Ioffe, 2017)。这在训练期间增加了额外的变量。对于此参数的任一值,推断都是相同的。 -
renorm_clipping可以将键 'rmax'、'rmin'、'dmax' 映射到标量Tensors的字典,用于裁剪 renorm 校正。校正(r, d)用作corrected_value = normalized_value * r + d,r被剪裁为 [rmin, rmax],d被剪裁为 [-dmax, dmax]。缺少的rmax、rmin、dmax分别设置为 inf, 0、 inf,。 -
renorm_momentum用于使用 renorm 更新移动均值和标准差的动量。与momentum不同,这会影响训练,并且既不能太小(会增加噪音)也不能太大(会给出过时的估计)。请注意,momentum仍用于获取推理的均值和方差。 -
fused如果None或True,请尽可能使用更快的融合实现。如果False,使用系统推荐的实现。 -
virtual_batch_size一个int。默认情况下,virtual_batch_size是None,这意味着在整个批次中执行批次标准化。当virtual_batch_size不是None时,改为执行“Ghost Batch Normalization”,它会创建虚拟的 sub-batches,每个都分别进行标准化(使用共享的 gamma、beta 和移动统计信息)。执行过程中必须除以实际的batch size。 -
adjustment一个函数采用包含输入张量的(动态)形状的Tensor并返回一对(比例,偏差)以应用于归一化值(在 gamma 和 beta 之前),仅在训练期间。例如,如果 axis==-1,adjustment = lambda shape:( tf.random.uniform(shape[-1:], 0.93, 1.07), tf.random.uniform(shape[-1:], -0.1, 0.1))会将归一化值向上或向下缩放最多 7%,然后将结果最多移动 0.1(每个特征都有独立的缩放和偏差,但在所有示例中共享) ,最后应用 gamma 和/或 beta。如果None,则不应用任何调整。如果指定了virtual_batch_size,则无法指定。
返回
- 输出张量。
抛出
-
ValueError如果启用了即刻执行。
迁移到 TF2
警告:这个 API 是为 TensorFlow v1 设计的。继续阅读有关如何从该 API 迁移到本机 TensorFlow v2 等效项的详细信息。见TensorFlow v1 到 TensorFlow v2 迁移指南有关如何迁移其余代码的说明。
此 API 是一个遗留 api,仅与 Eager Execution 和 tf.function 兼容,如果您将其与 tf.compat.v1.keras.utils.track_tf1_style_variables 结合使用
请参阅迁移指南的 tf.layers 模型映射部分,了解如何在 TF2 中将 TensorFlow v1 模型与 Keras 一起使用。
对应的 TensorFlow v2 层是 tf.keras.layers.BatchNormalization 。
tf.control_dependencies(tf.GraphKeys.UPDATE_OPS) 的批量更新模式不应在原生 TF2 中使用。有关详细信息,请参阅 tf.keras.layers.BatchNormalization 文档。
到原生 TF2 的结构映射
支持的参数均未更改名称。
前:
x_norm = tf.compat.v1.layers.batch_normalization(x)
后:
要使用 TF1 函数层迁移代码,请使用 Keras 函数 API:
x = tf.keras.Input(shape=(28, 28, 1),)
y = tf.keras.layers.BatchNormalization()(x)
model = tf.keras.Model(x, y)
如何映射参数
| TF1 参数名称 | TF2 参数名称 | 注意 |
|---|---|---|
name |
name |
图层基类 |
trainable |
trainable |
图层基类 |
axis |
axis |
- |
momentum |
momentum |
- |
epsilon |
epsilon |
- |
center |
center |
- |
scale |
scale |
- |
beta_initializer |
beta_initializer |
- |
gamma_initializer |
gamma_initializer |
- |
moving_mean_initializer |
moving_mean_initializer |
- |
beta_regularizer |
`beta_regularizer' | - |
gamma_regularizer |
`gamma_regularizer' | - |
beta_constraint |
`beta_constraint' | - |
gamma_constraint |
`gamma_constraint' | - |
renorm |
不支持 | - |
renorm_clipping |
不支持 | - |
renorm_momentum |
不支持 | - |
fused |
不支持 | - |
virtual_batch_size |
不支持 | - |
adjustment |
不支持 | - |
注意:训练时,moving_mean和moving_variance需要更新。默认情况下,更新操作放置在 tf.GraphKeys.UPDATE_OPS 中,因此它们需要与 train_op 一起执行。此外,请务必在获取 update_ops 集合之前添加任何 batch_normalization 操作。否则,update_ops 将为空,训练/推理将无法正常工作。例如:
x_norm = tf.compat.v1.layers.batch_normalization(x, training=training)
# ...
update_ops = tf.compat.v1.get_collection(tf.GraphKeys.UPDATE_OPS)
train_op = optimizer.minimize(loss)
train_op = tf.group([train_op, update_ops])
参考:
Batch Normalization - 通过减少内部协变量偏移来加速深度网络训练:Ioffe 等人,2015 (pdf) Batch Renormalization - Towards Reducing Minibatch Dependence in Batch-Normalized 模型:Ioffe,2017 (pdf)
相关用法
- Python tf.compat.v1.layers.conv3d用法及代码示例
- Python tf.compat.v1.layers.Conv3D用法及代码示例
- Python tf.compat.v1.layers.dense用法及代码示例
- Python tf.compat.v1.layers.AveragePooling3D用法及代码示例
- Python tf.compat.v1.layers.Conv2DTranspose用法及代码示例
- Python tf.compat.v1.layers.max_pooling3d用法及代码示例
- Python tf.compat.v1.layers.average_pooling1d用法及代码示例
- Python tf.compat.v1.layers.experimental.keras_style_scope用法及代码示例
- Python tf.compat.v1.layers.flatten用法及代码示例
- Python tf.compat.v1.layers.conv1d用法及代码示例
- Python tf.compat.v1.layers.experimental.set_keras_style用法及代码示例
- Python tf.compat.v1.layers.conv2d_transpose用法及代码示例
- Python tf.compat.v1.layers.dropout用法及代码示例
- Python tf.compat.v1.layers.average_pooling2d用法及代码示例
- Python tf.compat.v1.layers.MaxPooling1D用法及代码示例
- Python tf.compat.v1.layers.conv2d用法及代码示例
- Python tf.compat.v1.layers.Conv2D用法及代码示例
- Python tf.compat.v1.layers.AveragePooling1D用法及代码示例
- Python tf.compat.v1.layers.BatchNormalization用法及代码示例
- Python tf.compat.v1.layers.max_pooling1d用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.layers.batch_normalization。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
