Tensorflow 为什么tf.layers.batch_规范化仅在is_训练为真时才能在推理模式下工作?
这是我的批次代码。当我训练模型时,我会用Tensorflow 为什么tf.layers.batch_规范化仅在is_训练为真时才能在推理模式下工作?,tensorflow,Tensorflow,这是我的批次代码。当我训练模型时,我会用True设置参数is_training,但是当我想推断模型时,如果我用False设置它,它会输出错误的值。但是,如果我将其设置为True,它将在推理模式下工作,有人知道为什么吗?请参见 您需要运行更新操作(例如,通过将它们传递到您的会话),否则移动平均值/方差不会得到更新,因此推断不正确。我对训练和可训练的参数有疑问,您能否给出详细解释。同意这是一个令人困惑的区别可培训确定变量是否标记为可培训(并添加到可培训变量集合)。这是β和γ。它不会为移动平均值或移动
True
设置参数is_training
,但是当我想推断模型时,如果我用False
设置它,它会输出错误的值
。但是,如果我将其设置为True
,它将在推理模式下工作,有人知道为什么吗?请参见
您需要运行更新操作(例如,通过将它们传递到您的
会话
),否则移动平均值/方差不会得到更新,因此推断不正确。我对训练
和可训练
的参数有疑问,您能否给出详细解释。同意这是一个令人困惑的区别<代码>可培训确定变量是否标记为可培训(并添加到可培训变量集合)。这是β和γ。它不会为移动平均值或移动方差打开或关闭更新操作,也不会在使用批处理统计信息和移动统计信息之间切换。大多数人可以只设置training
而忘记trainiable
。因此,如果我们使用bn层,那么training
和trainiable
必须是相同的?如果您正在优化(False
),我会离开trainiable=True
,并将training
设置为True
)。
def _batch_norm(self, name, x, is_training=True):
"""Batch normalization.
Considering the performance, we use batch_normalization in contrib/layers/python/layers/layers.py
instead of tf.nn.batch_normalization and set fused=True
Args:
x: input tensor
is_training: Whether to return the output in training mode or in inference mode, use the argment
in finetune
"""
with tf.variable_scope(name):
return tf.layers.batch_normalization(
inputs=x,
axis=1 if self._data_format == 'NCHW' else 3,
momentum = 0.997,
epsilon = 1e-5,
center=True,
scale=True,
training=is_training,
fused=True
)